大家好,我是考100分的小小码 ,祝大家学习进步,加薪顺利呀。今天说一说DbgridEh数据导出到Excel的方法(Delphi),希望您对编程的造诣更进一步.
本文内容严重参考了网上其他牛人的代码,并做了一些修改,使得参数更加简单,结构更清晰,所以就厚着脸皮说是原创。
uses DBGridEhImpExp;
procedure TFrm_func.DbGridEhToExcel(ADgEh: TDBGridEh);
var
ExpClass: TDBGridEhExportclass;
Ext: string;
FSaveDialog: TSaveDialog;
begin
try
if ADgEh.DataSource.DataSet.IsEmpty then
begin
Application.MessageBox(PChar(‘没有可导出的数据‘), PChar(‘提示‘), MB_OK +
MB_ICONINFORMATION);
exit;
end;
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, ADgEh, FSaveDialog.FileName, true);
Screen.Cursor := crDefault;
MessageBox(Handle, ‘导出成功 ‘, ‘提示‘, MB_OK +
MB_ICONINFORMATION);
end;
end;
FSaveDialog.Destroy;
except
on e: exception do
begin
Application.MessageBox(PChar(e.message), ‘错误‘, MB_OK + MB_ICONSTOP);
end;
end;
end;
DbgridEh数据导出到Excel的方法(Delphi)
原文:https://www.cnblogs.com/jijm123/p/13452075.html
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/6806.html