通过这种方式在 delphi 中使用 excel 有点困难,因为您无法清楚地看到建议。
我将粘贴一些我拥有的代码Variant
,Excel
以便您使用它。
uses
System.Win.ComObj, Excel2010
...
procedure GerarExcel(ADBGrid: TDBGrid; AQuery: TFDQuery);
var
Excel: Variant;
linha, coluna, I, X: Integer;
begin
{Here you create the Excel Application Object}
Excel := CreateOleObject('Excel.Application');
{If you want to open the excel you can set this line to True.
In case you want to save directly without user intervention you can set to False.}
Excel.Visible :=True;
Excel.Workbooks.Add;
{In this case
Titles begin on the (4) line and different columns (1-7)}
Excel.WorkBooks[1].Sheets[1].Cells[4,1].Interior.ColorIndex := 55;
Excel.WorkBooks[1].Sheets[1].Cells[4,1].Font.ColorIndex := 2;
Excel.WorkBooks[1].Sheets[1].Cells[4,1].Font.Bold := True;
Excel.WorkBooks[1].Sheets[1].Cells[4,1].font.size := 10;
Excel.WorkBooks[1].Sheets[1].Cells[4,1] := 'Your Title 2';
Excel.WorkBooks[1].Sheets[1].Cells[4,1].ColumnWidth := 15;
...
Excel.WorkBooks[1].Sheets[1].Cells[4,7].Interior.ColorIndex := 55;
Excel.WorkBooks[1].Sheets[1].Cells[4,7].Font.ColorIndex := 2;
Excel.WorkBooks[1].Sheets[1].Cells[4,7].Font.Bold := True;
Excel.WorkBooks[1].Sheets[1].Cells[4,7].font.size := 10;
Excel.WorkBooks[1].Sheets[1].Cells[4,7] := 'Your Title 7';
Excel.WorkBooks[1].Sheets[1].Cells[4,7].ColumnWidth := 17;
Excel.WorkBooks[1].Sheets[1].Cells[4,7].HorizontalAlignment := 4;
Excel.WorkBooks[1].Sheets[1].Cells[4,7].verticalAlignment := 1;
I := 1;
{In case you want to loop through a TClientDateSet to fill in the cells}
with AQuery do
begin
First;
while not Eof do
begin
{To change to a specific format}
Excel.WorkBooks[1].Sheets[1].Cells[I + 4, 1].NumberFormat := 'dd/mm/aaaa';
Excel.WorkBooks[1].Sheets[1].Cells[I + 4, 1] := FieldByName(C_DATA).AsDateTime;
Excel.WorkBooks[1].Sheets[1].Cells[I + 4, 2] := FieldByName(C_CONTA_CREDITO).AsString;
Excel.WorkBooks[1].Sheets[1].Cells[I + 4, 3] := FieldByName(C_CONTA_DEBITO).AsString;
Excel.WorkBooks[1].Sheets[1].Cells[I + 4, 4].NumberFormat := 'R$#.##0,00';
Excel.WorkBooks[1].Sheets[1].Cells[I + 4, 4] := FieldByName(C_VALOR).AsString;
Excel.WorkBooks[1].Sheets[1].Cells[I + 4, 5] := FieldByName(C_DESCRICAO).AsString;
Excel.WorkBooks[1].Sheets[1].Cells[I + 4, 6] := FieldByName(C_NOME).AsString;
Excel.WorkBooks[1].Sheets[1].Cells[I + 4, 7] := FieldByName(C_NOME_MOVIMENTO).AsString;
Inc(I);
Next;
end;
end;
{All these variables configure the for landscape printing.}
Excel.ActiveSheet.PageSetup.RightFooter := 'Página &P de &N';
Excel.ActiveSheet.PageSetup.LeftFooter := '&D';
Excel.ActiveSheet.PageSetup.Orientation := 2;
Excel.ActiveSheet.PageSetup.PaperSize := xlPaperA4;
Excel.ActiveSheet.PageSetup.FitToPagesTall := False;
Excel.ActiveSheet.PageSetup.Zoom := False;
Excel.ActiveSheet.PageSetup.FitToPagesWide := 1;
{If you want the user to see the excel being opened and save it wherever the user wants
You are done here.}
{Save the file automatically
If you want the user to NOT see the excel being opened and save the file in a specific folder automatically
In the beginning you need to set Visible := False and then these lines}
FilePath := 'C:\...\my_excel_file.xlsx';
if FileExists(FilePath) then
DeleteFile(FilePath);
Excel.WorkBooks[1].Sheets[1].SaveAs(FilePath);
Excel.Quit;
end;
在VBA API Microsoft 文档中,您可以找到更多信息。