Ver Mensaje Individual
  #2  
Antiguo 20-09-2008
toni.vi toni.vi is offline
Miembro
 
Registrado: may 2003
Ubicación: Sant Fost(Barcelona)
Posts: 102
Reputación: 22
toni.vi Va por buen camino
//Dataset son los registros a pasar a excel
//Datos son los campos que queremos pasar y en que posicion
// Valor es la posicion
// Descripcion es el campo a pasar
//Excel Exportacion
//------------------------------------------------------------------------------
procedure TFormLoadExcel.RutEnviarExcel(DataSet, Datos: TDataSet; vFileName, vHoja: String);
var
i, j : Integer;
vCol : Integer;

ExcelApp, ExcelLibro, ExcelHoja : Variant;

begin

try
ExcelApp := CreateOleObject('Excel.Application');
ExcelLibro := ExcelApp.Workbooks.open(vFileName); //abro un excel vacio solo con los titulos y el formato que quiero
ExcelHoja := ExcelLibro.Worksheets[vHOJA]; //nombre de la hoja

with DataSet do
begin

j := StrToIntDef(edPrimeraFila.text,1);

First;
while not EOF do
begin

Datos.First;
While not Datos.Eof do
begin
vCol := Datos.FieldByname('Valor').AsInteger;
vCampo := Datos.FieldByname('Descripcion').AsString;

ExcelHoja.Cells(j, vCol) := FieldByName(Datos.FieldByname('Descripcion').AsString).AsString;
if (FieldByName(vCampo) is TFloatField) then ExcelHoja.Cells(j, vCol) := FieldByName(vCampo).AsFloat;
if (FieldByName(vCampo) is TIntegerField) then ExcelHoja.Cells(j, vCol) := FieldByName(vCampo).AsInteger;
if (FieldByName(vCampo) is TDateField) then ExcelHoja.Cells(j, vCol) := FieldByName(vCampo).AsDateTime;
if (FieldByName(vCampo) is TBooleanField) then ExcelHoja.Cells(j, vCol) := FieldByName(vCampo).AsBoolean;

Datos.Next;
end;
Next;

Inc(j);
end;
end;

ExcelApp.ActiveSheet.SaveAs(vFileName);
except
ExcelApp.Quit;
end;

ExcelApp.Quit;

end;
Responder Con Cita