1. 程式人生 > >StringGrid數據導出到Excel

StringGrid數據導出到Excel

source question exce urg 圖片 文章 file box ssa

1、控件:

  StringGrid1: TRzStringGrid;
  ADOConnection1: TADOConnection;
  ADOTable1: TADOTable;
  DataSource1: TDataSource;
  Button1: TButton;//導出數據
  Button2: TButton;//連接數據庫
  Button3: TButton;//初始化列表

技術分享圖片

2、雙擊連接數據庫按鈕:

 1   //使用之前先關閉
 2   ADOConnection1.Close;
 3   with ADOConnection1 do
4 begin 5 ConnectionString :=Provider=SQLOLEDB.1;Password=123;Persist Security Info=True;User ID=sa;Initial Catalog=Test_DBNAME;Data Source=127.0.0.1; 6 try 7 Connected := True; 8 ShowMessage(數據庫連接成功!); 9 except 10 ShowMessage(數據庫連接失敗!); 11 end;
12 //登錄時不需要在輸入密碼了 13 LoginPrompt := False; 14 end; 15 with ADOTable1 do 16 begin 17 //這三個順序不能亂 18 Connection := ADOConnection1; 19 TableName := TableName; 20 Active := True; 21 end; 22 DataSource1.DataSet := ADOTable1;

3、雙擊初始化列表按鈕:

 1   //記得在此過程前聲明i,j變量哦
2 //初始劃列表 3 ADOTable1.First; 4 StringGrid1.RowCount := ADOTable1.RecordCount + 1; 5 StringGrid1.ColCount := ADOTable1.FieldCount + 1; 6 for i := 0 to ADOTable1.RecordCount - 1 do 7 begin 8 for j := 0 to ADOTable1.FieldCount - 1 do 9 begin 10 StringGrid1.Cells[j + 1, 0] := ADOTable1.Fields.Fields[j].FieldName; 11 StringGrid1.Cells[0, i + 1] := IntToStr(i + 1); 12 if ADOTable1.Fields.Fields[j].Value = null then 13 StringGrid1.Cells[j + 1, i + 1] := ‘‘ 14 else 15 StringGrid1.Cells[j + 1, i + 1] := ADOTable1.Fields.Fields[j].Value; 16 end; 17 ADOTable1.Next; 18 end; 19 StringGrid1.FixedCols := 0;

4、新寫一個導出數據的過程:

 1 //把他寫成了一個過程,這樣其他用到這個StringGrid的控件都可以刁永紅這個來導出了
 2 procedure TForm6.ExportToExcel(RzStrGrid: TRzStringGrid);
 3 const
 4   xlNormal = -4143;
 5 var
 6   i, j, k, l: integer;
 7   filename: string;
 8   excel, Vrange: OleVariant;
 9   savedialog: tsavedialog;
10 begin
11   filename := ‘‘;
12   if RzStrGrid.RowCount > 65536 then
13   begin
14     if application.messagebox(需要導出的數據過大,Excel最大只能容納65536行,是否還要繼續?, 詢問, mb_yesno + mb_iconquestion) = idno then
15       exit;
16   end;
17   screen.Cursor := crHourGlass;
18 
19   try
20     excel := CreateOleObject(Excel.Application);
21     excel.workbooks.add;
22   except
23     screen.cursor := crDefault;
24     showmessage(無法調用Excel!);
25     exit;
26   end;
27   savedialog := tsavedialog.Create(nil);
28   savedialog.Filter := Excel文件(*.xls)|*.xls;
29   if savedialog.Execute then
30   begin
31     if FileExists(savedialog.FileName) then
32     try
33       if application.messagebox(該文件已經存在,要覆蓋嗎?, 詢問, mb_yesno + mb_iconquestion) = idyes then
34         DeleteFile(PChar(savedialog.FileName))
35       else
36       begin
37         excel.Quit;
38         savedialog.free;
39         screen.cursor := crDefault;
40         Exit;
41       end;
42     except
43       excel.Quit;
44       savedialog.free;
45       screen.cursor := crDefault;
46       Exit;
47     end;
48     filename := savedialog.FileName;
49   end;
50   savedialog.free;
51   if filename = ‘‘ then
52   begin
53     excel.Quit;
54     screen.cursor := crDefault;
55     exit;
56   end;
57   //設置字體
58   excel.Cells.Font.Size := 10;
59   //導出數據到Excel
60   for i := 0 to RzStrGrid.RowCount - 1 do
61   begin
62     for j := 0 to RzStrGrid.ColCount - 1 do
63     begin
64       excel.Cells[i + 1, j + 1] := RzStrGrid.Cells[j, i];
65     end;
66   end;
67   //設置列寬
68   excel.ActiveSheet.Columns[3].ColumnWidth := 15;
69   //需要合並的單元格
70   Vrange := excel.range[excel.cells[RzStrGrid.RowCount + 1, 1], excel.cells[RzStrGrid.RowCount + 1, RzStrGrid.ColCount]];
71   Vrange.Select;
72   //合並單元格
73   Vrange.Merge(True);
74   //合並後單元格內容的字體大小
75   Vrange.Font.Size := 15;
76   Vrange.Font.Bold := True;
77   //合並單元格後的背景色
78   Vrange.Interior.ColorIndex := 6;
79   //最後一行加一個註解
80   excel.cells[RzStrGrid.RowCount + 1, 1] := 註意:導出Excel和上一篇文章導出的Excel模板一樣,可以進行導入導出;
81   //整頁的文本全都居中
82   excel.columns.HorizontalAlignment := 3;
83   try
84     //判斷文件名後綴是不是.xls
85     if copy(filename, length(filename) - 3, 4) <> .xls then
86       filename := filename + .xls;
87     //保存Excel
88     excel.ActiveWorkbook.SaveAs(filename, xlNormal, ‘‘, ‘‘, False, False);
89   except
90     excel.Quit;
91     screen.cursor := crDefault;
92     exit;
93   end;
94   excel.Visible := true;
95   screen.cursor := crDefault;
96 end;

雙擊導入數據按鈕,來調用導出StringGrid數據的過程:

1 procedure TForm6.Button1Click(Sender: TObject);
2 begin
3   ExportToExcel(StringGrid1);
4 end;

好了,這樣就可以了

StringGrid數據導出到Excel