| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1081 人关注过本帖
标题:[原创]dbgridtoexcel
只看楼主 加入收藏
sgliuxiu
Rank: 5Rank: 5
等 级:贵宾
威 望:15
帖 子:291
专家分:0
注 册:2007-2-2
收藏
 问题点数:0 回复次数:4 
[原创]dbgridtoexcel

因为需要,做了个将数据从dbgrid转到excel的文件,有需要的拿去用,只是起个示范作用,在网上看到的都是将数据库字段及内容转到excel,而我们需要将dbgrid的column的列标题放到电子表格里面.大家可以通过这个例子写出自己需要的转换程序.
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls, DB, ADODB, Grids, DBGrids,ComObj;

type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
ADOTable1: TADOTable;
DataSource1: TDataSource;
Button1: TButton;
ListView1: TListView;
Edit1: TEdit;
sd: TSaveDialog;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
eclapp,workbook:variant;
row,col,i:integer;
begin
eclapp:=createoleobject('excel.application');
workbook:=createoleobject('excel.sheet');
workbook:=eclapp.workbooks.add;
row:=2;
eclapp.workbooks.item[1].activate;
eclapp.cells.font.colorindex:=5;
eclapp.activesheet.cells(1,3):='用户操作记录表';
datasource1.DataSet.First;
for i:=0 to dbgrid1.Columns.Count-1 do
eclapp.activesheet.cells(2,i+1):=dbgrid1.Columns[i].Title.caption;
while not (datasource1.DataSet.eof) do
begin
col:=1;
for i:=0 to dbgrid1.Columns.Count-1 do
begin
eclapp.activesheet.cells(row+1,col):=dbgrid1.Fields[i].AsString;
col:=col+1;
end;
datasource1.DataSet.Next;
row:=row+1;
end;

if sd.Execute then
workbook.saveas(sd.FileName);
workbook.saved:=true;
workbook.close;
eclapp.quit;
eclapp:=unassigned;

end;

end.//unit1

form文档:


object Form1: TForm1 //form
Left = 192
Top = 114
Width = 870
Height = 450
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object DBGrid1: TDBGrid
Left = 160
Top = 32
Width = 320
Height = 120
DataSource = DataSource1
TabOrder = 0
TitleFont.Charset = DEFAULT_CHARSET
TitleFont.Color = clWindowText
TitleFont.Height = -11
TitleFont.Name = 'MS Sans Serif'
TitleFont.Style = []
Columns = <
item
Expanded = False
FieldName = 'userid'
Title.Caption = '用户编号'
Visible = True
end
item
Expanded = False
FieldName = 'usename'
Title.Caption = '用户名称'
Visible = True
end
item
Expanded = False
FieldName = 'intime'
Title.Caption = '登陆时间'
Visible = True
end
item
Expanded = False
FieldName = 'outtime'
Title.Caption = '退出时间'
Visible = True
end>
end
object Button1: TButton
Left = 184
Top = 200
Width = 75
Height = 25
Caption = '输出到excel'
TabOrder = 1
OnClick = Button1Click
end
object ListView1: TListView
Left = 568
Top = 64
Width = 250
Height = 150
Columns = <>
TabOrder = 2
end
object Edit1: TEdit
Left = 352
Top = 184
Width = 169
Height = 21
TabOrder = 3
Text = 'Edit1'
end
object ADOTable1: TADOTable
Active = True
ConnectionString =
'Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initi' +
'al Catalog=shoufei;Data Source=BENQ-473FEFW9SQ'
CursorType = ctStatic
TableName = 'oper'
Left = 40
Top = 144
end
object DataSource1: TDataSource
DataSet = ADOTable1
Left = 96
Top = 168
end
object sd: TSaveDialog
FileName =
'C:\Documents and Settings\aa\桌面\6330333307846875004\学生成绩登记表\成绩表0' +
'4级\数04(2)\数控04(2)(考查)(请填科目及教师名).xls'
Left = 96
Top = 248
end
end //form








图片附件: 游客没有浏览图片的权限,请 登录注册

[此贴子已经被作者于2007-2-21 13:48:01编辑过]

搜索更多相关主题的帖子: 数据库 excel dbgrid TDB 
2007-02-21 13:34
sgliuxiu
Rank: 5Rank: 5
等 级:贵宾
威 望:15
帖 子:291
专家分:0
注 册:2007-2-2
收藏
得分:0 
有个问题请斑竹帮忙,在转换的时候,dbgrid数据一直在刷新一样,怎么样才可以让它数据不动呢?

小猫说:给我10元钱
2007-02-21 13:37
xu2000
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:51
帖 子:3890
专家分:56
注 册:2006-4-8
收藏
得分:0 
没有什么好办法,因为游标滚动的,

我会拿出我全部的钱财,以保你衣食无忧。我会献出我所有的智慧,以助你一帆风顺。我会想到所有的笑语,以令你展眉开颜。我会挤出最长的时间,以使你终生幸福。        [本人原创的结婚宣言]
2007-02-21 18:59
sgliuxiu
Rank: 5Rank: 5
等 级:贵宾
威 望:15
帖 子:291
专家分:0
注 册:2007-2-2
收藏
得分:0 
我已经解决了那问题,在while前面加 datasource1.DataSet.DisableControls;在后面加 datasource1.DataSet.EnableControls;
这两个事件一个是临时断开同数据表的连接,另外一个是恢复连接.还没有用sql试

小猫说:给我10元钱
2007-02-21 22:24
sgliuxiu
Rank: 5Rank: 5
等 级:贵宾
威 望:15
帖 子:291
专家分:0
注 册:2007-2-2
收藏
得分:0 

用adoquery控件也试过了,可以用这两种方法.


小猫说:给我10元钱
2007-02-21 22:38
快速回复:[原创]dbgridtoexcel
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.037365 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved