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
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
const
xlOpenXMLWorkbook = 51;
FileExcel : Array[1..6] of String = ('TestCopy-1.xls', 'TestCopy-2.xls',
'TestCopy-3.xls', 'TestCopy-4.xls',
'TestCopy-5.xls', 'TestCopy-6.xls');
var
Excel, WrkS : OleVariant;
Sheet, Range : OleVariant;
FileExcelInput : String;
FileExcelOutput : String;
i : Integer;
begin
FileExcelOutput := ExtractFilePath(Application.ExeName) + 'TestCopyAll.xlsx';
try
if FileExists(FileExcelOutput) then
DeleteFile(FileExcelOutput);
Excel := CreateOleObject('Excel.Application');
Excel.DisplayAlerts := False;
Excel.Visible := False;
Excel.Workbooks.Add(-4167);
WrkS := Excel.Worksheets[1];
WrkS.Name := 'None';
WrkS.SaveAs(FileExcelOutput,xlOpenXMLWorkbook);
for i := High(FileExcel) downto Low(FileExcel) do
begin
Excel.Workbooks.Open(FileExcelOutput);
WrkS := Excel.Worksheets.Add();
WrkS.Name := 'Sheet-' + IntToStr(i);
FileExcelInput := ExtractFilePath(Application.ExeName) + FileExcel[i];
Sheet := Excel.Workbooks.Open(FileExcelInput);
Range := Sheet.Worksheets[1].UsedRange.Copy;
WrkS.Range['A1'].PasteSpecial(Range);
WrkS.SaveAs(FileExcelOutput,xlOpenXMLWorkbook);
end;
MessageDlg('Consolidado de Hojas de Cálculo de Excel Creado Satisfactoriamente', mtinformation, [mbok], 0);
finally
Excel.Quit;
Excel := Unassigned;
WrkS := Unassigned;
Sheet := Unassigned;
end;
end;
end.