用DBGridEh
function DBGridehExport(const sFileName:string;const DBGridEh :TDBGridEh;IsAllRecord:boolean) : String;
var ExpClass:TDBGridEhExportClass;
Ext:String;
SaveDialog1 : TSaveDialog;
begin
Result := '';
SaveDialog1 := TSaveDialog.Create (nil);
SaveDialog1.Filter := 'Microsoft Excel Workbook (*.xls)|*.XLS | Text files (*.txt)|*.TXT|Comma separated values (*.csv)|*.CSV|HTML file (*.htm)|*.HTM|Rich Text Format (*.rtf)|*.RTF';
SaveDialog1.Options :=[ofHideReadOnly,ofEnableSizing];
try
SaveDialog1.FileName := sFileName;
if SaveDialog1.Execute then
begin
case SaveDialog1.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;
else
ExpClass := nil;
Ext := '';
end;
if ExpClass <> nil then
begin
if UpperCase(Copy(SaveDialog1.FileName,Length(SaveDialog1.FileName)-2,3)) <> UpperCase(Ext) then
SaveDialog1.FileName := SaveDialog1.FileName + '.' + Ext;
SaveDBGridEhToExportFile(ExpClass,DBGridEh,SaveDialog1.FileName,IsAllRecord);
end;
end;
Result := SaveDialog1.FileName ;
finally
SaveDialog1.Free;
end;
end;