dbgrid导出excl表


本文整理自网络,侵删。

 procedure TForm1.Button1Click(Sender: TObject);
var
Data : TDataSet;
ADBGrid: TDBGrid;
i, j : integer;
ExcelApp:Variant;

begin
DBGrid1.Enabled:=false;
ExcelApp := CreateOleObject('Excel.Application');


ExcelApp.WorkBooks.add;
ExcelApp.Columns[5].ColumnWidth:=20;
ExcelApp.Columns[6].ColumnWidth:=20;
ADBGrid := DBGrid1;
Data := ADBGrid.DataSource.DataSet;
with ADBGrid do { Insert Table Header }
for i := 1 to Columns.Count do
if Columns[i - 1].Visible then
ExcelApp.Cells[1,i].Value :=Columns[i - 1].Title.Caption;

Data.First;
i:=2;
while not Data.Eof do { Process All record }
begin
with ADBGrid do { Process one record }
for j := 1 to Columns.Count do
if Columns[j - 1].Visible then
ExcelApp.Cells[i,j].Value := Columns[j - 1].Field.DisplayText;
Inc(i);
Data.Next;
end;
ExcelApp.Visible:=true;
DBGrid1.Enabled:=true;
end;

uses comobj;

相关阅读 >>

Delphi idpop3收邮件

decodedate:从一个tdatetime变量中提取年、月、日数值

Delphi 判断素数的简单例子

Delphi 判断一个数组的长度用 length 还是 sizeof ?

Delphi 固定保持datetime日期格式

Delphi 官方stylebook大全使用

Delphi 配置驱动程序(firedac)

Delphi 游戏是如何检测到有od等调试工具的

Delphi idhttp post 支持 gzip 解压缩

Delphi 的内存操作函数(6): 跨进程的内存分配

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...