DBGRIDEH最完美导出数据到excel记录总结 【转】

uses DBGridEhImpExp;
 {-------------------------------------------------------------------------------
  过程名:    TfrmEmplyExport.OutToFile
  作者:      han
  日期:      2007.08.08
  参数:      IADO : TADOQuery; DgEh : TDBGridEh
  返回值:    无
  说明:      导出查询结果到文件
-------------------------------------------------------------------------------}
procedure TfrmEmplyExport.OutToFile(IADO : TADOQuery; DgEh : TDBGridEh);
var
  ExpClass:TDBGridEhExportclass;
  Ext:String;
  FSaveDialog: TSaveDialog;
begin
  try
    if not IADO.IsEmpty then
    begin
      FSaveDialog := TSaveDialog.Create(Self);
      FSaveDialog.Filter:='Excel 文档 (*.xls)|*.XLS|Text files (*.txt)|*.TXT|Comma separated values (*.csv)|*.CSV|HTML file (*.htm)|*.HTM|Word 文档 (*.rtf)|*.RTF';
      if FSaveDialog.Execute and (trim(FSaveDialog.FileName)<>'') then
      begin
        case FSaveDialog.FilterIndex of
            1: begin ExpClass := TDBGridEhExportAsXLS; Ext := 'xls'; end;
            2: begin ExpClass := TDBGridEhExportAsText; Ext := 'txt'; end;
            3: begin ExpClass := TDBGridEhExportAsCSV; Ext := 'csv'; end;
            4: begin ExpClass := TDBGridEhExportAsHTML; Ext := 'htm'; end;
            5: begin ExpClass := TDBGridEhExportAsRTF; Ext := 'rtf'; end;
        end;
        if ExpClass <> nil then
        begin
          if UpperCase(Copy(FSaveDialog.FileName,Length(FSaveDialog.FileName)-2,3)) <> UpperCase(Ext) then
            FSaveDialog.FileName := FSaveDialog.FileName + '.' + Ext;
            if FileExists(FSaveDialog.FileName) then
            begin
              if application.MessageBox('文件名已存在,是否覆盖   ', '提示', MB_ICONASTERISK or MB_OKCANCEL)<>idok then
                exit;
            end;
           Screen.Cursor := crHourGlass;
           SaveDBGridEhToExportFile(ExpClass,DgEh,FSaveDialog.FileName,true);
           Screen.Cursor := crDefault;
           MessageBox(Handle, '导出成功  ', '提示', MB_OK +
             MB_ICONINFORMATION);
          end;
      end;
      FSaveDialog.Destroy;
    end;
  except
    on e: exception do
    begin
      Application.MessageBox(PChar(e.message), '错误', MB_OK + MB_ICONSTOP);
    end;
  end;                       
end;

参考链接:http://www.cnblogs.com/del88/archive/2011/12/29/2305678.html

posted @ 2017-06-22 10:12  delphi学习交流吧  阅读(4073)  评论(2编辑  收藏  举报