本文整理自网络,侵删。
function DBGridExport(lpGrid:TDBGridEh) : Boolean;var ExpClass: TDBGridEhExportClass; Ext: string; SaveDlg: TSaveDialog; ExportAll: Boolean;begin ExportAll := True; //缺省情况下导出所有数据 SaveDlg := TSaveDialog.Create(Application); SaveDlg.Filter := 'Excel表格(*.XLS)|*.XLS|HTML文档(*.HTM)|*.HTM|RTF格式(*.RTF)|*.RTF|纯文本文件(*.TXT)|*.TXT|逗号分隔文本格式(*.CSV)|*.CSV'; SaveDlg.FileName := '明细表' + '_' + FormatDateTime('YYMMDDHHmmSS', now); SaveDlg.Options := [ofOverwritePrompt, ofHideReadOnly, ofEnableSizing]; if SaveDlg.Execute then begin case SaveDlg.FilterIndex of 1: begin ExpClass := TDBGridEhExportAsXLS; Ext := 'xls'; end; 2: begin ExpClass := TDBGridEhExportAsHTML; Ext := 'htm'; end; 3: begin ExpClass := TDBGridEhExportAsRTF; Ext := 'rtf'; end; 4: begin ExpClass := TDBGridEhExportAsText; Ext := 'txt'; end; 5: begin ExpClass := TDBGridEhExportAsCSV; Ext := 'csv'; end; else ExpClass := nil; Ext := ''; end;
if ExpClass <> nil then begin if UpperCase(Copy(SaveDlg.FileName, Length(SaveDlg.FileName) - 2, 3)) <> UpperCase(Ext) then SaveDlg.FileName := SaveDlg.FileName + '.' + Ext; //如果grid中有选择的区域,则只导出所选择的区域 if lpGrid.Selection.SelectionType <> gstNon then ExportAll := False; try SaveDBGridEhToExportFile(ExpClass, lpGrid, SaveDlg.FileName, ExportAll); ShellExecute(0,'open',PWideChar( SaveDlg.FileName ),nil,nil,SW_SHOW); except
on E: Exception do begin MessageBox(0, LPCTSTR('保存文件发生异常,请检查是否有重名文件并且该文件没有被打开!'), '注意', MB_OK or MB_ICONWARNING); Exit; end;
end;
end;
end;
end;
相关阅读 >>
Delphi 从indy9升级到indy10时idtcpserver的变化
更多相关阅读请进入《Delphi》频道 >>