delphi 高手帮忙改一下代码(DBGRID数据导入Excel)

hfut_yutian 2009-05-20 11:56:46
该程序用的是spSkinStringGrid,我想改成用DBGrid的unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Excel2000, OleServer, SkinCtrls, SkinGrids, StdCtrls, ExcelXP;

type
TForm1 = class(TForm)
Button1: TButton;
spSkinStringGrid1: TspSkinStringGrid;
ExcelApplication1: TExcelApplication;
ExcelWorkbook1: TExcelWorkbook;
ExcelWorksheet1: TExcelWorksheet;
SaveDialog1: TSaveDialog;
procedure Button1Click(Sender: TObject);
procedure FormShow(Sender: TObject);
private
procedure toexcel(grid:TspSkinStringGrid;excelapp:TExcelApplication;excelbook:TExcelWorkbook;excelsheet:TExcelWorksheet;save:TSaveDialog);
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
toexcel(spSkinStringGrid1,ExcelApplication1,ExcelWorkbook1,ExcelWorksheet1,SaveDialog1);

end;


procedure TForm1.toexcel(grid:TspSkinStringGrid;excelapp:TExcelApplication;excelbook:TExcelWorkbook;excelsheet:TExcelWorksheet;save:TSaveDialog);
var

i,j,row,nall:integer; //定义变量,i用以储存记录的数目, j是记录的列数 row是EXCEL的行数 nall是记录的总数

begin
if save.Execute then // 打开保存窗口,让用户选择保存的文件名

begin //inintial excel 初始化EXCEL

ExcelApp.Connect;

ExcelApp.Workbooks.Add(Null,0);

ExcelBook.ConnectTo(ExcelApp.Workbooks[1]);

ExcelSheet.ConnectTo(ExcelBook.Sheets[1] as _WorkSheet);

begin

nall:=spSkinStringGrid1.RowCount; //保存记录的数量

row:=0;
for j:=0 to Grid.ColCount-1 do //初始化行,定位在第一 行
begin
Excelsheet.Cells.item[1, j+1] := Grid.Cells[j,0];
end;
for i:=0 to nall-1 do // total rows 正规记录,从第一条记录一直到最后循环

//if i=nall then exit;
begin
for j:=0 to Grid.ColCount-1 do // column逐列导出动作
begin
ExcelSheet.Cells.Item[row+2,j+1]:=Grid.Cells[j,row+1];
end;
row:=row+1;
end;

end;
//以下是导出完毕后的动作,

ExcelBook.SaveCopyAs(Save.FileName+'.xls'); //加后缀名,确保最后是EXCEL文件

ExcelBook.Close(false);

ExcelApp.Disconnect; //断开与EXCEL的连接

Screen.Cursor:=crDefault; //指针随记录而动

showmessage('成功导出 '+inttostr(nall-1)+' 条数据!');

end;

end;

procedure TForm1.FormShow(Sender: TObject);
begin
spSkinStringGrid1.Cells[0,0]:='学号';
spSkinStringGrid1.Cells[1,0]:='姓名';
spSkinStringGrid1.Cells[2,0]:='性别';
spSkinStringGrid1.Cells[3,0]:='年龄';
spSkinStringGrid1.Cells[0,1]:='1234';
spSkinStringGrid1.Cells[0,2]:='123';
spSkinStringGrid1.Cells[0,3]:='12';
spSkinStringGrid1.Cells[0,4]:='1';
spSkinStringGrid1.Cells[1,1]:='张三';
spSkinStringGrid1.Cells[1,2]:='李四';
spSkinStringGrid1.Cells[1,3]:='王五';
spSkinStringGrid1.Cells[1,4]:='赵六';
spSkinStringGrid1.Cells[2,1]:='男';
spSkinStringGrid1.Cells[2,2]:='男';
spSkinStringGrid1.Cells[2,3]:='男';
spSkinStringGrid1.Cells[2,4]:='男';
spSkinStringGrid1.Cells[3,1]:='22';
spSkinStringGrid1.Cells[3,2]:='22';
spSkinStringGrid1.Cells[3,3]:='22';
spSkinStringGrid1.Cells[3,4]:='22';

end;

end.
...全文
61 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
de410 2009-05-21
  • 打赏
  • 举报
回复
呵呵~~这么快啊~
hfut_yutian 2009-05-21
  • 打赏
  • 举报
回复
已经解决~

2,498

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧