Delphi 将DBGrid中的内容导出到Excel
Delphi 将DBGrid中的内容导出到Excel
function Tfbalance.ExportDBGrid(DBGrid: TDBGrid; SheetName: string): boolean;//直接保存,不显示EXCEL
var
c, r, i, j: integer;
app: Olevariant;
TempFileName, ResultFileName: string;
begin
try
result := True;
app := CreateOLEObject(‘Excel.application’);
// app.WorkBooks.Add(xlWBatWorkSheet);
except
Application.MessageBox(‘Excel没有正确安装!’,‘警告’,MB_OK);
result := False;
exit;
end;
SaveDialog1.DefaultExt := ‘xls’;
SaveDialog1.FileName := SheetName;
if SaveDialog1.Execute then
TempFileName := SaveDialog1.FileName
else
Exit;
app.Workbooks.add;
app.Visible := false;
Screen.Cursor := crHourGlass;
DBGrid.DataSource.DataSet.First;
c := DBGrid.DataSource.DataSet.FieldCount;
r := DBGrid.DataSource.DataSet.RecordCount;
Application.ProcessMessages;
for i := 0 to c - 1 do
app.cells(1, 1 + i) := DBGrid.DataSource.DataSet.Fields[i].DisplayLabel;
for j := 1 to r do
begin
for i := 0 to c - 1 do
app.cells(j + 1, 1 + i) := DBGrid.DataSource.DataSet.Fields[i].AsString;
DBGrid.DataSource.DataSet.Next;
end;
ResultFileName := TempFileName;
if ResultFileName = ‘’ then
ResultFileName := ‘数据导出’;
if FileExists(TempFileName) then
DeleteFile(TempFileName);
app.Activeworkbook.saveas(TempFileName);
app.Activeworkbook.close(false);
app.quit;
app := unassigned;
end;
procedure Tfbalance.Image6Click(Sender: TObject);
begin
try
Screen.Cursor := crHourGlass;
ExportDBGrid(DBGrid2, ‘导出数据’); //暂时将导出的文件名称为“导出数据”(的execl文件)
finally
Screen.Cursor := crDefault;
end;
end;
- 点赞
- 收藏
- 分享
- 文章举报
- Delphi把窗体设为bsnone时如何拖动窗体
- Delphi向QQ2011窗口发送登录消息
- delphi编程中的棘手问题;求指教!
- 在DELPHI程序中自动设置ODBC数据源
- Delphi粗边框
- 由Delphi程序改写Sql(2000)的存储过程
- Delphi发email
- 用Delphi 向量算法 判断点是否在线上
- Delphi 删除控件问题
- 继续Delphi调用Wcf
- Delphi 调用WCF 搞定啦
- Quick Report 说明 for delphi 7
- DELPHI7下用superobject读取中国天气网的JSON数据的天气预报
- 刚学 Delphi 请多指教
- delphi学习总结
- Delphi中的记录
- Delphi中的Free和Nil之理解
- delphi简单字符的加密与解密
- Delphi控件开发基础篇
- 关于DELPHI XE4 开发 IOS APP问题