delphi数据导出到wps表格
delphi 导出数据到WPS表格的⽅法和导出到Excel⽅法雷同,都涉及使⽤Ole。导出到Wps表格时需要创建这样的Ole组件实例:var
etapp:olevariant;
myworkbook: OleVariant; //定义⾦⼭表格的⼯作簿对象
etapp:=createoleobject('et.application');//启动wps表格
现在⽹上有⼀段现成的代码,它就是实现将⼀个表格⾥的数据导出到 wps的电⼦表格⽂档中。具体代码如下:
procedure TfrmMain.Button10Click(Sender: TObject);
var
i,row,column,j:integer;
etapp:olevariant;
delphi app
myworkbook: OleVariant; //定义⾦⼭表格的⼯作簿对象
begin
etapp:=createoleobject('et.application');//启动wps表格
etApp.Visible := true;
if dbgrid1.DataSource.DataSet.RecordCount =0 then exit;
etapp.Workbooks.Close;
myworkbook :=etapp.Workbooks.add;
myworkbook.WorkSheets['sheet1'].Activate;
column:=1;
for j:=0 to dbgrid1.FieldCount-1 do
begin
IF dbgrid1.Columns[j].Visible =true then
begin
myworkbook.worksheets['sheet1'].cells[1,column].value:=lumns.Items[j].Title.caption ;
column:=column+1;
end;
end;
row:=2;
dbgrid1.DataSource.DataSet.First;
While Not (dbgrid1.DataSource.DataSet.Eof) do
begin
column:=1;
for i:=0 to dbgrid1.Columns.Count-1 do
begin
IF dbgrid1.Columns[I].Visible =true then
begin
myworkbook.worksheets['sheet1'].cells[row,column].value:=dbgrid1.Columns[i].Field.AsString ;
column:=column+1;
end;
end;
dbgrid1.DataSource.DataSet.Next;
row:=row+1;
end;
showmessage('导出完毕,请在wps表格中进⾏编辑、排版、打印等操作!');
end;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。