delphi 中如何将dbgrid中的数据导出为excel文件 (在没有安装excel的系统中完成),写下具体代码
展开全部
建议用dbgrided控件,带导出功能,需要use DBGridEhImpExp 代码如下
procedure ExportGridEh(DBGridEh:TDBGridEh);
var
ExpClass:TDBGridEhExportClass;
Ext:String;
dlgsave:TSaveDialog;
begin
dlgsave:= TSaveDialog.Create(nil);
begin
dlgsave.Filter:='Microsoft Excel|*.xls|CSV (Comma delimited)|*.csv|Text|*.txt|Web' +
' Page|*.htm|Rich Text Format(RTF)|*.rft';
if not dlgsave.Execute then Exit;
case dlgsave.FilterIndex of
1: begin ExpClass := TDBGridEhExportAsXLS; Ext := 'xls'; end;
2: begin ExpClass := TDBGridEhExportAsCSV; Ext := 'csv'; end;
3: begin ExpClass := TDBGridEhExportAsText; Ext := 'txt'; end;
4: begin ExpClass := TDBGridEhExportAsHTML; Ext := 'htm'; end;
5: begin ExpClass := TDBGridEhExportAsRTF; Ext := 'rtf'; end;
else
ExpClass := nil; Ext := '';
end;
if ExpClass <> nil then
begin
if UpperCase(Copy(dlgsave.FileName,Length(dlgsave.FileName)-2,3)) <> UpperCase(Ext) then
dlgsave.FileName := dlgsave.FileName + '.' + Ext;
SaveDBGridEhToExportFile(ExpClass,DBGridEh,dlgsave.FileName,true);
end;
end;
end;
procedure ExportGridEh(DBGridEh:TDBGridEh);
var
ExpClass:TDBGridEhExportClass;
Ext:String;
dlgsave:TSaveDialog;
begin
dlgsave:= TSaveDialog.Create(nil);
begin
dlgsave.Filter:='Microsoft Excel|*.xls|CSV (Comma delimited)|*.csv|Text|*.txt|Web' +
' Page|*.htm|Rich Text Format(RTF)|*.rft';
if not dlgsave.Execute then Exit;
case dlgsave.FilterIndex of
1: begin ExpClass := TDBGridEhExportAsXLS; Ext := 'xls'; end;
2: begin ExpClass := TDBGridEhExportAsCSV; Ext := 'csv'; end;
3: begin ExpClass := TDBGridEhExportAsText; Ext := 'txt'; end;
4: begin ExpClass := TDBGridEhExportAsHTML; Ext := 'htm'; end;
5: begin ExpClass := TDBGridEhExportAsRTF; Ext := 'rtf'; end;
else
ExpClass := nil; Ext := '';
end;
if ExpClass <> nil then
begin
if UpperCase(Copy(dlgsave.FileName,Length(dlgsave.FileName)-2,3)) <> UpperCase(Ext) then
dlgsave.FileName := dlgsave.FileName + '.' + Ext;
SaveDBGridEhToExportFile(ExpClass,DBGridEh,dlgsave.FileName,true);
end;
end;
end;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询