在窗体uses中,添加新的引用单元:DBGridEhImpExp
procedure TFrmdbGridEh.ToolButton2Click(Sender: TObject);
var
ExpClass:TDBGridEhExportClass;
Ext:String;
lssavedialog: tsavedialog;
begin
try
lssavedialog := tsavedialog.Create(nil);
lssavedialog.Filter := '*.xls|*.xls|*.csv|*.csv|*.htm|*.htm|*.rtf|*.rtf|*.txt|*.txt';
lssavedialog.FileName := 'file1';
if lssavedialog.Execute then
begin
case lssavedialog.FilterIndex of
1: begin ExpClass := TDBGridEhExportAsXLS; Ext := 'xls'; end;
2: begin ExpClass := TDBGridEhExportAsCSV; Ext := 'csv'; end;
3: begin ExpClass := TDBGridEhExportAsHTML; Ext := 'htm'; end;
4: begin ExpClass := TDBGridEhExportAsRTF; Ext := 'rtf'; end;
5: begin ExpClass := TDBGridEhExportAsText; Ext := 'txt'; end;
else
ExpClass := nil; Ext := '';
end;
if ExpClass <> nil then
begin
if UpperCase(Copy(lssavedialog.FileName, Length(lssavedialog.FileName) - 2, 3)) <> UpperCase(Ext) then
lssavedialog.FileName := lssavedialog.FileName + '.' + Ext;
if FileExists(lssavedialog.FileName) then
begin
if application.MessageBox('文件名已存在,是否覆盖', '提示', MB_ICONASTERISK or MB_OKCANCEL) <> idok then
begin
exit;
end;
end;
Screen.Cursor := crHourGlass;
try
SaveDBGridEhToExportFile(ExpClass, DBGridEh1, lssavedialog.FileName, true);
except
ShowMessage('NO');
end;
Screen.Cursor := crDefault;
end;
end;
finally
lssavedialog.Free;
end;
end;