Ver Mensaje Individual
  #2  
Antiguo 10-12-2013
Avatar de nlsgarcia
[nlsgarcia] nlsgarcia is offline
Miembro Premium
 
Registrado: feb 2007
Ubicación: Caracas, Venezuela
Posts: 2.206
Reputación: 21
nlsgarcia Tiene un aura espectacularnlsgarcia Tiene un aura espectacular
Yun-i,

Cita:
Empezado por Yun-i
...estoy exportando a Excel desde Delphi 7...no puedo acceder a la hoja llamada Jose ni a las que siguen...me dice que el indice no es valido...
Revisa este código:
Código Delphi [-]
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComObj;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

type
    TMeses = (Ene = 2, Feb, Mrz, Abr, May, Jun, Jul, Ago, Sep, Oct, Nov, Dic);
    TDias = (Lun = 2, Mar, Mie, Jue, Vie);

const
   xlOpenXMLWorkbook =  51; // Open XML Workbook
   Sheets : Array[0..2] of String = ('Sheet1', 'Sheet2', 'Sheet3');

procedure TForm1.Button1Click(Sender: TObject);
var
   Excel, Range : OleVariant;
   WrkB, WrkS : OleVariant;
   FileExcel, Msg : String;
   i : Integer;
   Meses : TMeses;
   Dias : TDias;

begin

  try

     try
        Excel := GetActiveOleObject('Excel.Application');
     except
        Excel := CreateOleObject('Excel.Application');
     end;

     Excel.DisplayAlerts := False;
     Excel.Visible := False;

     FileExcel := ExtractFilePath(Application.ExeName) + 'TestExcel.xlsx';

     WrkB := Excel.Workbooks.Open(FileExcel);

     for i := Low(Sheets) to High(Sheets) do
     begin

        WrkS := WrkB.Worksheets.Item[Sheets[i]];

        WrkS.Range['B2:F13'].Value := EmptyStr;

        Randomize;

        for Meses := Ene to Dic do
           for Dias := Lun to Vie do
              WrkS.Cells[Meses, Dias].Value := Random(1000);

     end;

     WrkS.SaveAs(FileExcel,xlOpenXMLWorkbook);

     Msg := 'Actualización de Libro de Trabajo de Excel Finalizada Exitosamente';
                                                                                 
     MessageDlg(Msg, mtinformation, [mbOk],0);

  finally

     Excel.Quit;
     Excel := Unassigned;
     WrkS := Unassigned;
     WrkB := Unassigned;

  end;

end;

end.
El código anterior actualiza tres hojas de cálculo en un libro de trabajo por medio de Automatización en Excel 2010 con Delphi 7 sobre Windows 7 Professional x32, según se muestra en la siguiente imagen:



El ejemplo esta disponible en : http://terawiki.clubdelphi.com/Delph...SheetExcel.rar

Revisa estos links:
Cita:
Unir archivos Excel : http://www.clubdelphi.com/foros/show...reateOleObject

Eliminar Fila en Hoja de Excel : http://www.clubdelphi.com/foros/show...reateOleObject

Exportar imágenes a Excel : http://www.clubdelphi.com/foros/show...reateOleObject

Excel := CreateOleObject ('Excel.Application') : http://www.clubdelphi.com/foros/show...reateOleObject

Importar datos de un Excel Delphi : http://www.clubdelphi.com/foros/show...reateOleObject

Guardar archivo de texto como Excel 2007-2010 : http://www.clubdelphi.com/foros/show...reateOleObject

Delphi and Microsoft Office Automating Excel and Word : http://edn.embarcadero.com/article/10126
La información anterior te dará bases para manejar hojas de cálculo en Excel con Delphi.

Espero sea útil

Nelson.
Responder Con Cita