我來幫你!
procedure Tform.BitBtn1Click(Sender: TObject);
begin
if SaveDialog1.Execute then
begin
ExcelApplication1.Connect;
ExcelApplication1.Workbooks.Add(null, 0);
ExcelWorkBook1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelWorkSheet1.ConnectTo(ExcelWorkBook1.Sheets[1] as _WorkSheet);
ExcelWOrkSheet1.Cells.Item[1, 1] := '字段一';
ExcelWOrkSheet1.Cells.Item[1, 2] := '字段二';
ExcelWOrkSheet1.Cells.Item[1, 3] := '字段三';
row := 2;
while not query1.Eofdo
begin
for i := 0 to query1.Fields.Count - 1do
begin
ExcelWOrkSheet1.cells.item[row,i+1].NumberFormatLocal:='@';
ExcelWOrkSheet1.Cells.Item[row, i + 1] := query1.Fields.AsString;
end;
row := row + 1;
query1.Next;
end;
ExcelWorkBook1.SaveCopyAs(SaveDialog1.FileName);
ExcelWOrkBook1.Close(false);
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
Screen.Cursor := crDefault;
Application.MessageBox('EXCEL轉換完畢!', '提示', 0);
end;
end;