急需把数据导出到excel,看了一段代码,便参考写了,可编译运行时说CreateOleObject没有声明(参见代码,有标记)。请高手指点
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, StdCtrls, DBCtrls, Mask, ExtCtrls, ComCtrls, Grids,
DBGrids, Buttons, ImgList, DBActns, ActnList;procedure TfrmAsk.Button2Click(Sender: TObject);
begin
frmAsk.PUBWriteExcel(DBGrid1, datetostr(date),screen.ActiveCustomForm.Caption);
end;procedure TfrmAsk.PUBWriteExcel(DBGrid1: TDBGrid; sName,Title1: string);
var
ExcelID: Variant;
k, i, j: integer;
filename: string;
begin
if DBGrid1.DataSource.DataSet.IsEmpty then
begin
beep;
showmessage('没有可导出的数据!');
exit;
end; filename:= concat(extractfilepath(application.exename), sName, '.xls'); try
ExcelID:=CreateOleObject('Excel.Application' ); //创建 Excel 对象
ExcelID.Caption :='运动会报名系统'+'--调用 Microsoft Excel 打开导出文件'; //更改 Excel 标题栏
except
Application.Messagebox(pchar('Excel 没有安装!'),pchar(trim('system_name')), MB_ICONERROR + mb_Ok);
Abort;
end; try
ExcelID.Workbooks.Add;
DBGrid1.DataSource.DataSet.DisableControls; //禁止dbgrid记录移动
DBGrid1.DataSource.DataSet.First;
for j := 0 to DBGrid1.Columns.Count - 1 do //插入字段名
begin
ExcelID.Worksheets[1].Cells[3, j + 1] := DBGrid1.Columns[j].Title.Caption;
ExcelID.Worksheets[1].Cells.item[3, j + 1].font.size := '10';
end; k:=2;
for i := 4 to DBGrid1.DataSource.DataSet.RecordCount + 3 do //插入记录(记录循环)
begin
for j := 0 to DBGrid1.FieldCount - 1 do //DBGrid列循环
begin
ExcelID.Worksheets[1].Cells.item[i, j + 1] :=
DBGrid1.Fields[j].Asstring;
ExcelID.Worksheets[1].Cells.item[i, j + 1].font.size := '10';
end;
DBGrid1.DataSource.DataSet.Next;
k:=k+1;
end; ExcelID.Worksheets[1].Columns.AutoFit; //宽度自适应
ExcelID.Worksheets[1].Cells.item[1, 2] := Title1; //标题内容
ExcelID.Worksheets[1].Cells.Item[1, 2].font.size := '14'; //标题大小
finally
DBGrid1.DataSource.DataSet.EnableControls;
ExcelID.Visible := true; //显示EXCEL窗口
end;
end;
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, StdCtrls, DBCtrls, Mask, ExtCtrls, ComCtrls, Grids,
DBGrids, Buttons, ImgList, DBActns, ActnList;procedure TfrmAsk.Button2Click(Sender: TObject);
begin
frmAsk.PUBWriteExcel(DBGrid1, datetostr(date),screen.ActiveCustomForm.Caption);
end;procedure TfrmAsk.PUBWriteExcel(DBGrid1: TDBGrid; sName,Title1: string);
var
ExcelID: Variant;
k, i, j: integer;
filename: string;
begin
if DBGrid1.DataSource.DataSet.IsEmpty then
begin
beep;
showmessage('没有可导出的数据!');
exit;
end; filename:= concat(extractfilepath(application.exename), sName, '.xls'); try
ExcelID:=CreateOleObject('Excel.Application' ); //创建 Excel 对象
ExcelID.Caption :='运动会报名系统'+'--调用 Microsoft Excel 打开导出文件'; //更改 Excel 标题栏
except
Application.Messagebox(pchar('Excel 没有安装!'),pchar(trim('system_name')), MB_ICONERROR + mb_Ok);
Abort;
end; try
ExcelID.Workbooks.Add;
DBGrid1.DataSource.DataSet.DisableControls; //禁止dbgrid记录移动
DBGrid1.DataSource.DataSet.First;
for j := 0 to DBGrid1.Columns.Count - 1 do //插入字段名
begin
ExcelID.Worksheets[1].Cells[3, j + 1] := DBGrid1.Columns[j].Title.Caption;
ExcelID.Worksheets[1].Cells.item[3, j + 1].font.size := '10';
end; k:=2;
for i := 4 to DBGrid1.DataSource.DataSet.RecordCount + 3 do //插入记录(记录循环)
begin
for j := 0 to DBGrid1.FieldCount - 1 do //DBGrid列循环
begin
ExcelID.Worksheets[1].Cells.item[i, j + 1] :=
DBGrid1.Fields[j].Asstring;
ExcelID.Worksheets[1].Cells.item[i, j + 1].font.size := '10';
end;
DBGrid1.DataSource.DataSet.Next;
k:=k+1;
end; ExcelID.Worksheets[1].Columns.AutoFit; //宽度自适应
ExcelID.Worksheets[1].Cells.item[1, 2] := Title1; //标题内容
ExcelID.Worksheets[1].Cells.Item[1, 2].font.size := '14'; //标题大小
finally
DBGrid1.DataSource.DataSet.EnableControls;
ExcelID.Visible := true; //显示EXCEL窗口
end;
end;
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货