因为需要,做了个将数据从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编辑过]