控件:
ExcelApplication1
ExcelWorkbook1
ExcelWorksheet1
SaveDialog1
代码:
procedure TForm1.BitBtn1Click(Sender: TObject);
var i,row:integer;
begin
if SaveDialog1.Execute then
begin
Screen.Cursor:=crHourGlass; //设置鼠标形状为沙漏状
ExcelApplication1.Connect; //和excel连接
ExcelApplication1.Workbooks.Add(NULL,0); //添加工作薄
ExcelApplication1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelApplication1.ConnectTo(ExcelWorkBook1.Sheets[1] as _WorkSheet);
if not ADOTable1.Active then
begin
ADOTable1.Open;
end;
for i:=0 to ADOTable1.Fields.Count-1 do
ExcelWorkSheet1.Cells.Item[1,i+1]:=dbgrid1.Columns.Items[i].Title.Caption;//把DBGRID的各字段名写入Excel第一行
row:=0;
while not ADOTable1.Eof do
begin
for i:=0 to ADOTable1.Fields.Count-1 do
begin
ExcelWorkSheet1.Cells.Item[row,i+1]:=ADOTable1.Fields[i].AsString;
end; //将查询结果存入到Excel中
ADOTable1.Next;
end;
ExcelWorkBook1.SaveCopyAs(SaveDialog1.FileName);
ExcelWorkBook1.Close(false);
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
Screen.Cursor:=crDefault;
Application.MessageBox('成功保存文件!','提示',0); end;
end;
执行的时候出现错误提示:
[错误] Unit1.pas(42): Incompatible types: '_Workbook' and '_Application'
[错误] Unit1.pas(43): Incompatible types: '_Worksheet' and '_Application'
[严重错误] Project1.dpr(5): Could not compile used unit 'Unit1.pas'如果注销掉这两句
ExcelApplication1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelApplication1.ConnectTo(ExcelWorkBook1.Sheets[1] as _WorkSheet);
提示我找不到接口了我第一次接触数据导出,请高手帮忙讲解一下
ExcelApplication1
ExcelWorkbook1
ExcelWorksheet1
SaveDialog1
代码:
procedure TForm1.BitBtn1Click(Sender: TObject);
var i,row:integer;
begin
if SaveDialog1.Execute then
begin
Screen.Cursor:=crHourGlass; //设置鼠标形状为沙漏状
ExcelApplication1.Connect; //和excel连接
ExcelApplication1.Workbooks.Add(NULL,0); //添加工作薄
ExcelApplication1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelApplication1.ConnectTo(ExcelWorkBook1.Sheets[1] as _WorkSheet);
if not ADOTable1.Active then
begin
ADOTable1.Open;
end;
for i:=0 to ADOTable1.Fields.Count-1 do
ExcelWorkSheet1.Cells.Item[1,i+1]:=dbgrid1.Columns.Items[i].Title.Caption;//把DBGRID的各字段名写入Excel第一行
row:=0;
while not ADOTable1.Eof do
begin
for i:=0 to ADOTable1.Fields.Count-1 do
begin
ExcelWorkSheet1.Cells.Item[row,i+1]:=ADOTable1.Fields[i].AsString;
end; //将查询结果存入到Excel中
ADOTable1.Next;
end;
ExcelWorkBook1.SaveCopyAs(SaveDialog1.FileName);
ExcelWorkBook1.Close(false);
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
Screen.Cursor:=crDefault;
Application.MessageBox('成功保存文件!','提示',0); end;
end;
执行的时候出现错误提示:
[错误] Unit1.pas(42): Incompatible types: '_Workbook' and '_Application'
[错误] Unit1.pas(43): Incompatible types: '_Worksheet' and '_Application'
[严重错误] Project1.dpr(5): Could not compile used unit 'Unit1.pas'如果注销掉这两句
ExcelApplication1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelApplication1.ConnectTo(ExcelWorkBook1.Sheets[1] as _WorkSheet);
提示我找不到接口了我第一次接触数据导出,请高手帮忙讲解一下
interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, DBGrids, DB, StdCtrls, ADODB,comobj;
procedure TForm1.Button1Click(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('没有安装EXCEL2000','提示',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 dprec');
open;
first;
end;
//填入数据标题
for i:=0 to ADOquery1.Fields.Count-1 do
xlsheet.Cells.Item[2,i+1]:=dbgrid1.Columns.Items[i].Title.Caption;
//数据从每三行开始,每二行写标题,每一行放表名
//此处可合并每一行。并写上标题。要合并的单元格数要等于表格的列数。请高人补充。我都是做模板。
//写入数据
row:=0;
while not adoquery1.Eof do
begin
for i:=0 to adoquery1.Fields.Count-1 do
begin
xlsheet.Cells.Item[row+3,i+1]:=adoquery1.Fields[i].AsString;
end;
adoquery1.Next;
inc(row);
end;
xlapp.Disconnect;
finally
xlapp.Free;
xlbook.Free;
xlsheet.Free;
end;
end;
xlbook:texcelworkbook;
xlsheet:texcelworksheet;
这三个是什么东东?