乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > <em>DELPHI</em>如何将数据导出到指定格式的<em>EXCEL</

<em>DELPHI</em>如何将数据导出到指定格式的<em>EXCEL</

作者:乔山办公网日期:

返回目录:excel表格制作


Eclapp:= createoleobject('Excel.Application');
Application.ProcessMessages;
try
Eclapp.workbooks.add;
eclapp.cells[1, 1]:= Caption;
n:= 2;
for i:= 3 to 你数据集e799bee5baa6e997aee7ad94e59b9ee7ad94333的行数 - 1 do
begin
for j:= 1 to 你数据集的列数 - 1 do
begin
eclapp.cells[n, j]:= 根据行列得到你数据集中的值; Application.ProcessMessages;
end;
inc(n);
end;
eclapp.Activeworkbook.saveas(FileName);
eclapp.visible:= true;
except
on E: Exception do
begin
raise Exception.Create(E.Message);
end;
end;


关于操纵EXCEL增加行并保持设定好的格式的问题,我有个解决办法,不知道是不是符合你的意思:其实我只要事先在模板文件中将两行的格式设定好就行了,比如我把第7和第8行的底色设成灰色,你在DELPHI中就可以通过在第8行前插入行就OK了。varExcelApp, MyWorkBook, sheet: OLEVariant;f1: string;i: integer;beginExcelApp := createoleobject('excel.application');ExcelApp.visible := false;ExcelApp.ScreenUpdating := false;f1 :='c:\模板文件.xls';ExcelApp.WorkBooks.Open(f1);MyWorkBook := ExcelApp.workbooks[1];sheet := MyWorkBook.worksheets[1];for i := 0 to table1.RecordCount - 1 dobeginsheet.rows[8 + i].insert;sheet.cells[8 + i, 1] := 'A';sheet.cells[8 + i, 2] := 'B';table1.next;end;sheet.rows[7].delete; //删除仅为提7a686964616fe58685e5aeb9333供格式样板的多余空行

你可以手动写一个导出函数,以后e69da5e6ba90e799bee5baa6e79fa5e98193363直接使用它。
//===================================================
//===================ADOQ数据导出到Excel=========
//===================================================
Procedure ADOQOutPutToExcel(MyADOQ:TADOQuery;MyDBG:TDBGrid;SaveFileName:String;Gauge:TGauge);
Var Excel,WorkBook:OleVariant;
i,j,row:integer;
Begin
//检测计算机中是否安装了Excel,是否可以建立对象
Try
Excel:=CreateOleObject('Excel.Application');
Except
Begin
Application.MessageBox('你的计算机中没有安装Excel,无法导出到Excel!','警告',0);
Exit;
end;
end;
//====将数据写入到Excel
WorkBook:=Excel.WorkBooks.Add;//建立工作簿
//==写入标题
Gauge.MaxValue :=MyDBG.Columns.Count-1;
Gauge.Progress :=0;
For i:=0 to MyDBG.Columns.Count-1 DO
Begin
Excel.Cells[1,i+1].value:=MyDBG.Columns.Items[i].Title.Caption;
Gauge.Progress :=i;
End;
Gauge.Progress :=0;
//==写入记录
Row:=2;
MyADOQ.First;
Gauge.MinValue :=0;
Gauge.MaxValue :=MyADOQ.RecordCount;
Gauge.Progress :=0;
For i:=0 to MyADOQ.RecordCount-1 DO
Begin
For j:=0 to MyDBG.Columns.Count-1 DO
Begin
Excel.cells[row,j+1].value:=MyDBG.Fields[j].Value;
end;
Inc(Row);
Gauge.Progress :=Gauge.Progress +1;
MyADOQ.Next;
End;
//==保存到文件
Try
Begin
Excel.ActiveWorkBook.SaveAs(SaveFileName);
Excel:=UnAssigned;
WorkBook.Close;
Application.MessageBox(PChar('模板创建成功!模板已成功保存到:'+#13 +SaveFileName+'.xls'),'操作提示',0);
End;
Except
Begin
Application.MessageBox('模板创建失败,指定文件可能正在使用当中,请关闭文件再试一次!','操作提示',0);
Exit;
End;
End;
End;
调用时:
Var ExcelFileName:String;
begin
SaveDialog1.Filter:='数据表|*.xls|';
If SaveDialog1.Execute then
Begin
ExcelFileName:=SaveDialog1.FileName;
ADOQOutPutToExcel(ADOQ_yfd,DBG_yfd,ExcelFileName,Gauge);
End;

相关阅读

关键词不能为空
极力推荐
  • Handsontable--在线Excel表格-excel在线

  • excel在线,在npm安装后npminstallhandsontable@handsontable/react在项目中引入css和组件直接使用关键的配置如下:width:'100%',//表格的宽度height:470,//表格的高度设置了才会出现scrollclassName。

ppt怎么做_excel表格制作_office365_word文档_365办公网