程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 從DBGrid,StringGrid導出Excel

從DBGrid,StringGrid導出Excel

編輯:Delphi
procedure TDM.CopyDbDataToExcel(Target: TDBGridEh;mb,FileName: string);
  var
  iCount, jCount: Integer;
  XLApp: Variant;
  Sheet: Variant;
  begin
  Screen.Cursor := crHourGlass;
  if not VarIsEmpty(XLApp) then
  begin
   XLApp.DisplayAlerts := False;
   XLApp.Quit;
   VarClear(XLApp);
  end;
  //--------------------------選擇模板------
  // CopyFile(pChar(Trim(ExtractFilePath(Application.ExeName))+mb+'.xls'),pChar(FileName+'1.xls'),false);
  //------------------------
  //通過ole創建Excel對象
  try
   XLApp := CreateOleObject('Excel.Application');
  except
   Screen.Cursor := crDefault;
   Exit;
  end;
  if mb = '統計-項目信息前' then
    XLApp.WorkBooks.Add[Trim(ExtractFilePath(Application.ExeName))+mb+'.xls']    //你要把數據放在那裡啊,先生成個文件在e:1.xls
  else
   XLApp.WorkBooks.Add;
  XLApp.WorkBooks[1].WorkSheets[1].Name := 'sheet1';
  Sheet := XLApp.Workbooks[1].WorkSheets['sheet1'];
  if not Target.DataSource.DataSet.Active then
  begin
   Screen.Cursor := crDefault;
   Exit;
  end;
  Target.DataSource.DataSet.first;

  for iCount := 0 to Target.Columns.Count - 1 do
  begin
   Sheet.cells[1, iCount + 1] := trim(Target.Columns.Items[iCount].Title.Caption);
  end;

  jCount := 1;
  while not Target.DataSource.DataSet.Eof do
  begin

   for iCount := 0 to Target.Columns.Count - 1 do   //
   begin
    if iCount = 0 then
     Sheet.cells[jCount + 1, iCount + 1] := IntToStr(jCount)
    else
     Sheet.cells[jCount + 1, iCount + 1] := trim(Target.Columns.Items[iCount].FIEld.AsString);
   end;
  Inc(jCount);
  Target.DataSource.DataSet.Next;
  end;

  //--------------------可以在此添加Excel的宏-----------

  XLApp.ActiveWorkbook.SaveAs(FileName:=FileName);                                                                                                            ;
  Screen.Cursor := crDefault;
  XLApp.ActiveWorkbook.Close;
  end;
  -----------------------------------------StringGridToExcel

  procedure TDM.CopySGDataToExcel(Target: TStringGrid;FileName: string);
  var
  iCount, jCount: Integer;
  XLApp: Variant;
  Sheet: Variant;
  begin
  Screen.Cursor := crHourGlass;
  if not VarIsEmpty(XLApp) then
  begin
  XLApp.DisplayAlerts := False;
  XLApp.Quit;
  VarClear(XLApp);
  end;
  //通過ole創建Excel對象
  try
  XLApp := CreateOleObject('Excel.Application');
  except
  Screen.Cursor := crDefault;
  Exit;
  end;
  XLApp.WorkBooks.Add;    //你要把數據放在那裡啊,先生成個文件在e:1.xls
  XLApp.WorkBooks[1].WorkSheets[1].Name := 'sheet1';
  Sheet := XLApp.Workbooks[1].WorkSheets['sheet1'];
  Target.Row := 0;
  for iCount := 0 to Target.RowCount - 1 do
  begin
   for jCount := 0 to Target.ColCount - 1 do
     Sheet.cells[iCount + 1, jCount + 1] := Target.Cells[jCount,iCount];
   Target.Row := iCount;
  end;
  XLApp.ActiveWorkbook.SaveAs(FileName:=FileName);
  Screen.Cursor := crDefault;
  XLApp.ActiveWorkbook.Close;
  end;

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved