给你一段例子,自己看吧procedure Tfrm_main.MnuItm_ExcelClick(Sender: TObject); var Temp_Worksheet: _WorkSheet; Dialog : TSaveDialog; Row : integer; i : integer; filePath : string; begin Row := 3;//在第三行导入数据 i := 1; //计数器 Dialog := TSaveDialog.Create(nil); try Dialog.filter:='EXCEL文件 (*.xls)|*.XLS'; if Dialog.Execute then filePath := Dialog.FileName else exit; finally Dialog.Free; end; //连接EXCEL with ExcelApplication1 do begin Connect; Caption := '标题; Visible[0] := false;//控制是否显示 end; with ExcelWorkbook1 do begin ConnectTo(ExcelApplication1.Workbooks.Add(EmptyParam,0)); Temp_Worksheet := Worksheets.add(EmptyParam,EmptyParam,EmptyParam,EmptyParam,0) as _workSheet; end; ExcelWorksheet1.ConnectTo(Temp_Worksheet); ExcelWorksheet1.Name := '某某谋; //单元格初始化 with ExcelApplication1 do begin Cells.Item[1,1].value := '某某数据'; Cells.Item[2,1].value := '索号'; Cells.Item[2,2].value := '区域'; Cells.Item[2,3].value := '日期'; Cells.Item[2,4].value := '索力'; end; //初始化查询 DtMd_Data.QueryClose; DtMd_Data.Query_Excel.Close; DtMd_Data.Query_Excel.Open; DtMd_Data.Query_Excel.First; frm_excel := Tfrm_excel.Create(nil); try frm_excel.Show; BlockInput(true); frm_excel.SeSkinProgressBar1.Max := DtMd_Data.Query_Excel.RecordCount; frm_excel.lab_caption.Caption:= '正在导出数据,请等待'; while not DtMd_Data.Query_Excel.Eof do begin with ExcelApplication1 do begin Cells.Item[row,1].value := DtMd_Data.Query_Excel.FieldByName('sh').AsString; Cells.Item[row,2].value := DtMd_Data.Query_Excel.FieldByName('qy').AsString; Cells.Item[row,3].value := FormatDateTime('yyyy-mm-dd',DtMd_Data.Query_Excel.FieldByName('rq').value); Cells.Item[row,4].value := DtMd_Data.Query_Excel.FieldByName('sl').AsString; end; Row := Row + 1; frm_excel.SeSkinProgressBar1.Position := i; i := i +1; DtMd_Data.Query_Excel.Next; end; DtMd_Data.Query_Excel.Close; BlockInput(false); ExcelWorksheet1.SaveAs(filePath);//保存 //断开EXCEL连接 ExcelWorkbook1.Close; ExcelApplication1.Quit; ExcelApplication1.Disconnect; frm_excel.Close; finally frm_excel.Free; BlockInput(false); end; end;
对Excel的操作,可以参考wu_di或者去网上搜索
保存为ExcelButton.pas,然后安装这个控件即可,我用了四年了. unit ExcelButton; {$R TExcelButton.dcr}interfaceuses Windows, Messages, SysUtils, Classes, Controls, StdCtrls, Buttons, DB, OleServer, Excel2000;type TExcelButton = class(TBitBtn) private fSheetName,fFilePath : string; fDataSource : TDataSource; protected procedure SetDataSource(Value : TDataSource); procedure ExportToExcel(SheetName : string;FilePath : string); procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override; public { Public declarations } procedure ExportFile; published property SheetName : string read fSheetName write fSheetName; property FilePath : string read fFilePath write fFilePath; property DataSource : TDataSource read fDataSource write SetDataSource; end;procedure Register;implementationprocedure Register; begin RegisterComponents('Additional', [TExcelButton]); end;procedure TExcelButton.SetDataSource(Value : TDataSource); begin fDataSource := Value; end;procedure TExcelButton.ExportFile; begin ExportToExcel(fSheetName,fFilePath); end;procedure TExcelButton.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin inherited; ExportToExcel(fSheetName,fFilePath); end;procedure TExcelButton.ExportToExcel(SheetName : string; FilePath : string); var col, row: Integer; WorkBk : _WorkBook; //Define a WorkBook WorkSheet : _WorkSheet; //Define a WorkSheet IIndex : OleVariant; ExcelApp : TExcelApplication; begin if (fDataSource.DataSet.Active) AND (SheetName <> '') AND (FilePath <> '') then begin IIndex := 1; Try ExcelApp := TExcelApplication.Create(Self); // create the Excel object ExcelApp.Connect; ExcelApp.WorkBooks.Add(xlWBatWorkSheet,0); // Select the first WorkBook WorkBk := ExcelApp.WorkBooks.Item[IIndex]; // Define the first WorkSheet WorkSheet := WorkBk.WorkSheets.Get_Item(1) as _WorkSheet; //WorkSheet.Name := SheetName; Worksheet.Columns.HorizontalAlignment := xlLeft; WorkSheet.Columns.ColumnWidth := 14; WorkSheet.Columns.RowHeight := 16; // add the info for the column names for col := 1 to fDataSource.DataSet.FieldCount do WorkSheet.Cells.Item[1,col] := fDataSource.DataSet.Fields[col-1].DisplayLabel; // get the data into the memo with fDataSource do begin DataSet.First; row := 2; while not DataSet.Eof do begin for col := 1 to DataSet.FieldCount do begin WorkSheet.Cells.Item[row,col] := DataSet.Fields[col-1].AsString; end; INC(row,1); DataSet.Next; end; end; ExcelApp.DisplayAlerts[0] := False; WorkBk.SaveCopyAs(FilePath,0); ExcelApp.Workbooks.Close(0); ExcelApp.Quit; Finally ExcelApp.Free; end; end; end;end.
procedure TForm1.Button3Click(Sender: TObject); var xlapp:texcelapplication; xlbook:texcelworkbook; xlsheet:texcelworksheet; i,j,row:integer; begin xlapp:=texcelapplication.Create(self); xlbook:=texcelworkbook.Create(self); xlsheet:=texcelworksheet.Create(self); try xlapp.ConnectKind:=cknewinstance; xlapp.Visible[1]:=true; try xlapp.Connect; except application.MessageBox('没有安装EXCEL','提示',mb_ok or mb_iconstop); exit; end; xlapp.Workbooks.Add(emptyparam,0); xlapp.Caption:='文件导出'; xlbook.ConnectTo(xlapp.Workbooks[1]); xlsheet.ConnectTo(xlapp.Worksheets[1] as _worksheet); 初始化数据集 with adoquery1 do begin //写上连接串 close; sql.Clear; sql.Add('select * from ckmx'); open; first; end; adoquery2.First; //填入数据标题 for i:=0 to ADOquery2.Fields.Count-1 do xlsheet.Cells.Item[2,i+1]:=dbgrid1.Columns.Items[i].Title.Caption; //数据从每三行开始,每二行写标题,每一行放表名 //此处可合并每一行。并写上标题。要合并的单元格数要等于表格的列数。请高人补充。我都是做模板。 //写入数据 row:=0; while not adoquery2.Eof do begin for i:=0 to adoquery2.Fields.Count-1 do begin xlsheet.Cells.Item[row+3,i+1]:=adoquery2.Fields[i].AsString; end; adoquery2.Next; inc(row); end; xlapp.Disconnect; finally xlapp.Free; xlbook.Free; xlsheet.Free; end; end;
var
Temp_Worksheet: _WorkSheet;
Dialog : TSaveDialog;
Row : integer;
i : integer;
filePath : string;
begin
Row := 3;//在第三行导入数据
i := 1; //计数器 Dialog := TSaveDialog.Create(nil);
try
Dialog.filter:='EXCEL文件 (*.xls)|*.XLS';
if Dialog.Execute then
filePath := Dialog.FileName
else
exit;
finally
Dialog.Free;
end;
//连接EXCEL
with ExcelApplication1 do
begin
Connect;
Caption := '标题;
Visible[0] := false;//控制是否显示
end;
with ExcelWorkbook1 do
begin
ConnectTo(ExcelApplication1.Workbooks.Add(EmptyParam,0));
Temp_Worksheet := Worksheets.add(EmptyParam,EmptyParam,EmptyParam,EmptyParam,0) as _workSheet;
end;
ExcelWorksheet1.ConnectTo(Temp_Worksheet);
ExcelWorksheet1.Name := '某某谋; //单元格初始化
with ExcelApplication1 do
begin
Cells.Item[1,1].value := '某某数据';
Cells.Item[2,1].value := '索号';
Cells.Item[2,2].value := '区域';
Cells.Item[2,3].value := '日期';
Cells.Item[2,4].value := '索力';
end;
//初始化查询
DtMd_Data.QueryClose;
DtMd_Data.Query_Excel.Close;
DtMd_Data.Query_Excel.Open;
DtMd_Data.Query_Excel.First; frm_excel := Tfrm_excel.Create(nil);
try
frm_excel.Show;
BlockInput(true);
frm_excel.SeSkinProgressBar1.Max := DtMd_Data.Query_Excel.RecordCount;
frm_excel.lab_caption.Caption:= '正在导出数据,请等待';
while not DtMd_Data.Query_Excel.Eof do
begin
with ExcelApplication1 do
begin
Cells.Item[row,1].value := DtMd_Data.Query_Excel.FieldByName('sh').AsString;
Cells.Item[row,2].value := DtMd_Data.Query_Excel.FieldByName('qy').AsString;
Cells.Item[row,3].value := FormatDateTime('yyyy-mm-dd',DtMd_Data.Query_Excel.FieldByName('rq').value);
Cells.Item[row,4].value := DtMd_Data.Query_Excel.FieldByName('sl').AsString;
end;
Row := Row + 1;
frm_excel.SeSkinProgressBar1.Position := i;
i := i +1;
DtMd_Data.Query_Excel.Next;
end;
DtMd_Data.Query_Excel.Close;
BlockInput(false);
ExcelWorksheet1.SaveAs(filePath);//保存
//断开EXCEL连接
ExcelWorkbook1.Close;
ExcelApplication1.Quit;
ExcelApplication1.Disconnect;
frm_excel.Close;
finally
frm_excel.Free;
BlockInput(false);
end;
end;
unit ExcelButton;
{$R TExcelButton.dcr}interfaceuses
Windows, Messages, SysUtils, Classes, Controls, StdCtrls, Buttons,
DB, OleServer, Excel2000;type
TExcelButton = class(TBitBtn)
private
fSheetName,fFilePath : string;
fDataSource : TDataSource;
protected
procedure SetDataSource(Value : TDataSource);
procedure ExportToExcel(SheetName : string;FilePath : string);
procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
X, Y: Integer); override;
public
{ Public declarations }
procedure ExportFile;
published
property SheetName : string read fSheetName write fSheetName;
property FilePath : string read fFilePath write fFilePath;
property DataSource : TDataSource read fDataSource write SetDataSource;
end;procedure Register;implementationprocedure Register;
begin
RegisterComponents('Additional', [TExcelButton]);
end;procedure TExcelButton.SetDataSource(Value : TDataSource);
begin
fDataSource := Value;
end;procedure TExcelButton.ExportFile;
begin
ExportToExcel(fSheetName,fFilePath);
end;procedure TExcelButton.MouseDown(Button: TMouseButton; Shift: TShiftState;
X, Y: Integer);
begin
inherited;
ExportToExcel(fSheetName,fFilePath);
end;procedure TExcelButton.ExportToExcel(SheetName : string; FilePath : string);
var
col, row: Integer;
WorkBk : _WorkBook; //Define a WorkBook
WorkSheet : _WorkSheet; //Define a WorkSheet
IIndex : OleVariant;
ExcelApp : TExcelApplication;
begin
if (fDataSource.DataSet.Active) AND
(SheetName <> '') AND
(FilePath <> '') then
begin IIndex := 1;
Try
ExcelApp := TExcelApplication.Create(Self); // create the Excel object
ExcelApp.Connect;
ExcelApp.WorkBooks.Add(xlWBatWorkSheet,0); // Select the first WorkBook
WorkBk := ExcelApp.WorkBooks.Item[IIndex]; // Define the first WorkSheet
WorkSheet := WorkBk.WorkSheets.Get_Item(1) as _WorkSheet; //WorkSheet.Name := SheetName;
Worksheet.Columns.HorizontalAlignment := xlLeft;
WorkSheet.Columns.ColumnWidth := 14;
WorkSheet.Columns.RowHeight := 16;
// add the info for the column names
for col := 1 to fDataSource.DataSet.FieldCount do
WorkSheet.Cells.Item[1,col] := fDataSource.DataSet.Fields[col-1].DisplayLabel; // get the data into the memo
with fDataSource do
begin
DataSet.First;
row := 2;
while not DataSet.Eof do
begin
for col := 1 to DataSet.FieldCount do
begin
WorkSheet.Cells.Item[row,col] := DataSet.Fields[col-1].AsString;
end;
INC(row,1);
DataSet.Next;
end;
end; ExcelApp.DisplayAlerts[0] := False;
WorkBk.SaveCopyAs(FilePath,0);
ExcelApp.Workbooks.Close(0);
ExcelApp.Quit;
Finally
ExcelApp.Free;
end;
end;
end;end.
var
xlapp:texcelapplication;
xlbook:texcelworkbook;
xlsheet:texcelworksheet;
i,j,row:integer;
begin
xlapp:=texcelapplication.Create(self);
xlbook:=texcelworkbook.Create(self);
xlsheet:=texcelworksheet.Create(self);
try
xlapp.ConnectKind:=cknewinstance;
xlapp.Visible[1]:=true;
try
xlapp.Connect;
except
application.MessageBox('没有安装EXCEL','提示',mb_ok or mb_iconstop);
exit;
end;
xlapp.Workbooks.Add(emptyparam,0);
xlapp.Caption:='文件导出';
xlbook.ConnectTo(xlapp.Workbooks[1]);
xlsheet.ConnectTo(xlapp.Worksheets[1] as _worksheet);
初始化数据集
with adoquery1 do
begin
//写上连接串
close;
sql.Clear;
sql.Add('select * from ckmx');
open;
first;
end;
adoquery2.First;
//填入数据标题
for i:=0 to ADOquery2.Fields.Count-1 do
xlsheet.Cells.Item[2,i+1]:=dbgrid1.Columns.Items[i].Title.Caption;
//数据从每三行开始,每二行写标题,每一行放表名
//此处可合并每一行。并写上标题。要合并的单元格数要等于表格的列数。请高人补充。我都是做模板。
//写入数据
row:=0;
while not adoquery2.Eof do
begin
for i:=0 to adoquery2.Fields.Count-1 do
begin
xlsheet.Cells.Item[row+3,i+1]:=adoquery2.Fields[i].AsString;
end;
adoquery2.Next;
inc(row);
end;
xlapp.Disconnect;
finally
xlapp.Free;
xlbook.Free;
xlsheet.Free;
end;
end;