该程序用的是spSkinStringGrid,我想改成用DBGrid的unit Unit1;interfaceuses
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.
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.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货