....
implementation
uses
forms, windows, comObj, excel2000, DB, Math, sysUtils;
.....
procedure exportatExcel(facturas,recibos:TMDOQuery;direccion:string);
var
Excel, libro, hoja, hojaAnulada: Variant;
i, iAnulado: integer;
begin
try
Excel:=CreateOleObject('Excel.Application');
Excel.Visible := False;
Excel.DisplayAlerts:= False;
Excel.SheetsInNewWorkbook := 4;
libro:=Excel.WorkBooks.Add;
Excel.WorkBooks[1].WorkSheets[1].Name := 'Facturas';
Excel.WorkBooks[1].WorkSheets[2].Name := 'Facturas Anuladas';
Excel.WorkBooks[1].WorkSheets[3].Name := 'Recibos';
Excel.WorkBooks[1].WorkSheets[4].Name := 'Recibos Anulados';
hoja:=Excel.WorkBooks[1].WorkSheets['Facturas'];
hojaAnulada:=Excel.WorkBooks[1].WorkSheets['Facturas Anuladas'];
with facturas do
begin
hoja.Cells[1,1]:='FECHA'; hoja.Cells[1,2]:='NÚMERO'; hoja.Cells[1,3]:='USUARIO';
hoja.Cells[1,4]:='IMPORTE';
hojaAnulada.Cells[1,1]:='FECHA'; hojaAnulada.Cells[1,2]:='NÚMERO'; hojaAnulada.Cells[1,3]:='USUARIO';
hojaAnulada.Cells[1,4]:='IMPORTE';
First;
i:= 2;
iAnulado:=2;
while not Facturas.Eof do
begin
if Fields[4].AsString = 'PAGADO' then
begin
hoja.Cells [i,1] := Fields[0].AsDateTime; hoja.Cells [i,2] := Fields[1].AsString; hoja.Cells [i,3] := Fields[2].AsString; hoja.Cells [i,4] := floatToStrF(Fields[5].AsFloat,ffFixed,10,2); inc(i);
end
else
begin
hojaAnulada.Cells [iAnulado,1] := Fields[0].AsDateTime; hojaAnulada.Cells [iAnulado,2] := Fields[1].AsString; hojaAnulada.Cells [iAnulado,3] := Fields[2].AsString; hojaAnulada.Cells [iAnulado,4] := FloatToStrF(Fields[5].AsFloat,ffFixed,10,2); inc(iAnulado);
end;
Next;
end;
hoja.Cells.Columns.AutoFit;
hojaAnulada.Cells.Columns.AutoFit
end;
hoja:=Excel.WorkBooks[1].WorkSheets['Recibos'];
hojaAnulada:=Excel.WorkBooks[1].WorkSheets['Recibos Anulados'];
with recibos do
begin
hoja.Cells[1,1]:='FECHA'; hoja.Cells[1,2]:='NÚMERO'; hoja.Cells[1,3]:='USUARIO';
hoja.Cells[1,4]:='IMPORTE';
hojaAnulada.Cells[1,1]:='FECHA'; hojaAnulada.Cells[1,2]:='NÚMERO'; hojaAnulada.Cells[1,3]:='USUARIO';
hojaAnulada.Cells[1,4]:='IMPORTE';
First;
i:= 2;
iAnulado:=2;
while not Recibos.Eof do
begin
if Fields[3].AsString = 'PAGADO' then
begin
hoja.Cells [i,1] := Fields[0].AsDateTime; hoja.Cells [i,2] := Fields[1].AsString; hoja.Cells [i,3] := Fields[2].AsString; hoja.Cells [i,4] := floatToStrF(Fields[4].AsFloat,ffFixed,10,2); inc(i);
end
else
begin
hojaAnulada.Cells [iAnulado,1] := Fields[0].AsDateTime; hojaAnulada.Cells [iAnulado,2] := Fields[1].AsString; hojaAnulada.Cells [iAnulado,3] := Fields[2].AsString; hojaAnulada.Cells [iAnulado,4] := floatToStrF(Fields[4].AsFloat,ffFixed,10,2); inc(iAnulado);
end;
Next;
end;
hoja.Cells.Columns.AutoFit;
hojaAnulada.Cells.Columns.AutoFit
end;
Libro.SaveAs(direccion);
Excel.Quit;
except
Excel.Quit;
Application.MessageBox('No se pudo crear el Archivo de Excel.',
'Backup de Comprobantes',MB_OK + MB_ICONERROR);
raise;
end;
end;