procedure OutputToExcel(Data: TStringTable);
var
Excel,WrkBook,WrkSheet: OleVariant;
SheetCount,TmpCol, TmpRow, Row: Integer;
FileName: string;
SaveExcel: TSaveDialog;
begin
SaveExcel := TSaveDialog.Create(Application);
if not SaveExcel.Execute then
Exit;
if Pos('.xls', SaveExcel.FileName) = 0 then
FileName := SaveExcel.FileName + '.xls'
else
FileName := SaveExcel.FileName;
SaveExcel.Free;
try
Excel := CreateOleObject('Excel.Application');
except
if MessageBox(GetActiveWindow, '对不起,你的机器没有安装Microsoft Excel,是否继续导出?'
+ #13#13 + '导出后在您的机器上不能直接打开,必须安装Excel到机器上才能打开!',
'注意', MB_ICONQUESTION + MB_OKCANCEL) = ID_CANCEL then
Exit;
end;
WrkBook:=Excel.WorkBooks.Add;
SheetCount := 1;
Row := 1;
for TmpRow := 0 to Data.Count - 1do
begin
for TmpCol := 0 to Data[TmpRow].Count - 1do
Excel.WorkSheets[SheetCount].Cells[Row, TmpCol + 1] := Data[TmpRow].Strings[TmpCol];
Excel.WorkSheets[SheetCount].Rows[Row].Font.Size := 9;
Excel.WorkSheets[SheetCount].Rows[Row].WrapText := True;
Inc(Row);
if Row > 50000 then
begin
SheetCount:=SheetCount + 1;
Row := 1;
if SheetCount > 3 then
begin
WrkSheet:=WrkBook.WorkSheets[WrkBook.WorkSheets.Count];
WrkBook.WorkSheets.Add(EmptyParam, WrkSheet, 1, $FFFFEFB9);
end;
end;
end;
Excel.WorkSheets[SheetCount].PageSetup.PrintGridLines := True;
try
Excel.ActiveWorkBook.SaveAs(FileName);
except
MessageBox(GetActiveWindow, '文件被其他程序打开,无法保存!', '提示', MB_OK + MB_ICONINFORMATION);
end;
WrkBook.Close;
Excel.Quit;
Excel:= unAssigned;
MessageBox(GetActiveWindow, PChar('已经导出到 '+ FileName),
'提示', MB_OK + MB_ICONINFORMATION);
end;