需要引用uses cxGridExportLink;
procedure TForm1.dxBarLargeButton5Click(Sender: TObject);
var
SaveDialog: TSaveDialog;
path: string; //路径信息
ExcelAPP: Variant; //变体变量
begin
SaveDialog := TSaveDialog.Create(nil);
path := '';
try
with SaveDialog do
begin
FileName := '供应商信息' + FormatDateTime('YYYYMMDD', NOW()); //默认文件名
Filter := '*.xls|*.xls|*.xlsx|*.xlsx|';
if Execute then
begin
case SaveDialog.FilterIndex of
1:
ExportGridToExcel(SaveDialog.FileName, Self.cxGrid1, true, true, true, 'xls');
2:
ExportGridToXLSX(SaveDialog.FileName, self.cxGrid1, true, true, true, 'xlsx'); //2007
end;
end;
end;
finally
path := SaveDialog.FileName;
SaveDialog.Free;
end;
if path <> '' then //打开文件
begin
ExcelAPP := CreateOleObject('Excel.Application'); //创建EXCEL对象
ExcelAPP.Visible := false;
ExcelAPP.WorkBooks.Open(path); //用EXCEL的方法打开文件
ExcelAPP.WorkSheets[1].Activate; //文件操作,到这里就跟VBA操作没什么差别了
ExcelAPP.Visible := True; //显示出来
end;
end;
标签:SaveDialog,ExcelAPP,end,D11,cxGrid,begin,path,OK,true From: https://www.cnblogs.com/raozhonghua/p/18017630