unit GridToXls;
interface
uses Classes, cxGrid, cxTL, cxExport, cxTLExportLink;
function SaveGridToXlsFile(AOwner:TComponent; grid:TcxGrid; defaultDirectory:string) : boolean;
function SaveTreeListToXlsFile(AOwner:TComponent; tree_list:TcxCustomTreeList; defaultDirectory:string) : boolean;
implementation
uses Dialogs, SysUtils, Forms, Windows, Controls, cxGridExportLink, {Variables,} AppMessages;
function AskExcelFileName(AOwner:TComponent; defaultDirectory:string; var fileName:string) : boolean;
var openDlg : TOpenDialog;
begin
openDlg:=Nil;
try
try
fileName:='';
openDlg:=TOpenDialog.Create(AOwner);
openDlg.DefaultExt:='xls';
openDlg.Filter:='Microsoft Excel Workbook (*.xls)|*.xls';
openDlg.Options:=[ofOverwritePrompt,ofHideReadOnly,ofPathMustExist,ofNoReadOnlyReturn,ofEnableSizing];
openDlg.InitialDir:=defaultDirectory;
if openDlg.Execute then
fileName:=openDlg.FileName;
except
end;
finally
if openDlg<>Nil then
try
openDlg.Free;
except
end;
end;
result:=fileName <> '';
end;
function SaveGridToXlsFile(AOwner:TComponent; grid:TcxGrid; defaultDirectory:string) : boolean;
var xlsFileName : string;
begin
result:=false;
if AskExcelFileName(AOwner,defaultDirectory,xlsFileName) then
begin
if FileExists(xlsFileName) then
if AppMsg_Warning_YesNo('Файл '+xlsFileName+' существует, перезаписать?') <> mrYes then
exit;
try
ExportGridToExcel(xlsFileName,grid,false,true,true,'xls');
result:=true;
except
on E
xception do
AppMsg_Error('Ошибка записи Excel файла '+xlsFileName);
end;
end;
end;
function SaveTreeListToXlsFile(AOwner:TComponent; tree_list:TcxCustomTreeList; defaultDirectory:string) : boolean;
var xlsFileName : string;
begin
result:=false;
if AskExcelFileName(AOwner,defaultDirectory,xlsFileName) then
begin
if FileExists(xlsFileName) then
if AppMsg_Warning_YesNo('Файл '+xlsFileName+' существует, перезаписать?') <> mrYes then
exit;
try
cxExportTLToExcel(xlsFileName,tree_list,true,true,true,'xls');
result:=true;
except
on E
xception do
AppMsg_Error('Ошибка записи Excel файла '+xlsFileName);
end;
end;
end;
end.