ExportDBGridToExcel

来源:互联网 发布:正义联盟英雄排名 知乎 编辑:程序博客网 时间:2024/05/17 13:06
 
  1. uses DBGrids, ComObj, ActiveX, Variants;
  2. procedure ExportDBGridToExcel(F_DBGrid: TDBGrid; F_Title: string = '数据资料';
  3.   F_ScreenUpdating: Boolean = true; F_PrintPreview: Boolean = False; F_AddChart: Boolean = False);
  4. const
  5.   CLASS_ExcelApplication: TGUID = '{00024500-0000-0000-C000-000000000046}';
  6. var
  7.   ExcelApp, Sheet, Chart: OleVariant;
  8.   Unknown: IUnknown;
  9.   Bm: TBookmarkStr;
  10.   Col, Row: Integer;
  11.   I: Integer;
  12. begin
  13.   if (F_DBGrid.DataSource <> niland (F_DBGrid.DataSource.DataSet <> nilthen
  14.     with F_DBGrid.DataSource.DataSet do
  15.     begin
  16.       if not active then exit;
  17.       try
  18.         if not Succeeded(GetActiveObject(CLASS_ExcelApplication, nil, Unknown)) then
  19.           Unknown := CreateComObject(CLASS_ExcelApplication);
  20.       except
  21.         raise Exception.Create('不能启动 Microsoft Excel,请确认 Microsoft Excel 已正确安装在本机上');
  22.       end;
  23.       ExcelApp := Unknown as IDispatch;
  24.       ExcelApp.Visible := True;
  25.       ExcelApp.Workbooks.Add;
  26.       if not F_ScreenUpdating then
  27.         ExcelApp.ScreenUpdating := False;
  28.       DisableControls;
  29.       try
  30.         Bm := Bookmark;
  31.         First;
  32.         Row := 3;
  33.         Col := 1;
  34.         for I := 0 to F_DBGrid.Columns.Count - 1 do
  35.         begin
  36.           if F_DBGrid.Columns[I].Visible then
  37.             ExcelApp.Cells[Row, Col] := F_DBGrid.Columns[I].Title.Caption;
  38.           Inc(Col);
  39.         end;
  40.         Inc(Row);
  41.         while not EOF do
  42.         begin
  43.           Col := 1;
  44.           for I := 0 to F_DBGrid.Columns.Count - 1 do
  45.           begin
  46.             if F_DBGrid.Columns[I].Visible then
  47.               if F_DBGrid.Columns[I].Field <> nil then
  48.                 ExcelApp.Cells[Row, Col] := F_DBGrid.Columns[I].Field.DisplayText;
  49.             Inc(Col);
  50.           end;
  51.           Inc(Row);
  52.           Next;
  53.         end;
  54.         Col := 1;
  55.         for I := 0 to F_DBGrid.Columns.Count - 1 do
  56.         begin
  57.           if F_DBGrid.Columns[I].Visible then
  58.             ExcelApp.Columns[Col].AutoFit; ;
  59.           Inc(Col);
  60.         end;
  61.         ExcelApp.Cells[11] := F_Title;
  62.         Bookmark := Bm;
  63.       finally
  64.         EnableControls;
  65.         if not ExcelApp.ScreenUpdating then
  66.           ExcelApp.ScreenUpdating := True;
  67.       end;
  68.     end;
  69.   if F_AddChart then
  70.   begin
  71.     Sheet := ExcelApp.ActiveSheet;
  72.     Sheet.Range[Sheet.Cells[41], Sheet.Cells[Row - 1, Col - 1]].Select;
  73.     Chart := ExcelApp.ActiveWorkbook.Charts.Add;
  74.     Chart.SetSourceData(
  75.       Sheet.Range[Sheet.Cells[41], Sheet.Cells[Row - 1, Col - 1]], EmptyParam);
  76.     Chart.Location(1, EmptyParam);
  77.   end;
  78.   if F_PrintPreview then
  79.     ExcelApp.ActiveWorkbook.PrintPreview;
  80. end;
原创粉丝点击