Is the procedure described in KB item A1113 still correct? I can't get it to work.
Now, I can't find ExportGrid4ToExcel in my source.
My code basically looks like this:
grdPriceSummary.FocusedView := dbtvPriceDetails;
ExportDetailToExcelFromGrid(grdPriceSummary,
TClientDataSet(TcxGridDBTableView(dbtvPriceDetails).DataController.DataSet),
GetFormatFileNameWithoutExtension('PDV3'),
ExportedTo);
Where ExportDetailToExcelFromGrid is this:
procedure ExportDetailToExcelFromGrid(DataGrid1: TcxGrid;
GridDataSet1: TClientDataSet;
sFileName1: string;
ExportTo: TExportType);
var
PrevCursor: TCursor;
sTempFileName: string;
begin
if ExportTo <> etCancel then
begin
PrevCursor := Screen.Cursor;
Screen.Cursor := crHourglass;
try
case ExportTo of
etExcel : begin
if DataGrid1 <> nil then
begin
sTempFileName:= sFileName1 + '.xls';
if FileExists(sTempFileName) then
DeleteFile(PChar(sTempFileName));
ExportGridToExcel(sTempFileName,DataGrid1,True,True,False);
ShellExecute(Application.Handle, 'open',PChar(sTempFileName), nil, nil, SW_SHOWNORMAL);
end;
end;
etXML : begin
if DataGrid1 <> nil then
begin
sTempFileName:= sFileName1 + '.xml';
if FileExists(sTempFileName) then
DeleteFile(PChar(sTempFileName));
ExportGridToXML(sTempFileName,DataGrid1,True,True);
PelotonShellExecute(sTempFileName,TRUE);
end;
end;
etHTML : begin
if DataGrid1 <> nil then
begin
sTempFileName:= sFileName1 + '.html';
if FileExists(sTempFileName) then
DeleteFile(PChar(sTempFileName));
ExportGridToHTML(sTempFileName,DataGrid1,True,True);
PelotonShellExecute(sTempFileName,TRUE);
end;
end;
etExcelRawData :
begin
if DataGrid1 <> nil then
begin
sTempFileName:= sFileName1 + '.xls';
if FileExists(sTempFileName) then
DeleteFile(PChar(sTempFileName));
ExportDataSetToExcel(GridDataSet1);
ShellExecute(Application.Handle, 'open',PChar(sTempFileName), nil, nil, SW_SHOWNORMAL);
end;
end;
end;
finally
Screen.Cursor := PrevCursor;
end;
end;
end;
Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.
Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.
What I end up with is master rows being exported.
Hi Philip,
It looks as if this approach can't be used anymore, since the ActiveLevel's GridView is currently used for export. The only solution we can suggest is to modify our source code. For instance, please change the ExportGridToFile procedure (cxGridExportLink.pas unit) as follows:
procedure ExportGridToFile(AFileName: string; AExportType: Integer; AGrid: TcxGrid; AExpand, ASaveAll, AUseNativeFormat: Boolean; const ASeparator, ABeginString, AEndString: string; const AFileExt: string); var AView: TcxCustomGridView; AGridExport: TcxGridCustomExport; begin if AGrid <> nil then begin if AFileExt <> '' then AFileName := ChangeFileExt(AFileName, '.' + AFileExt); AView := AGrid.FocusedView;//AGrid.ActiveLevel.GridView; <-----CHANGED ... end;
Hopefully, this suggestion will be of some help. Please keep us informed about your progress.
Thanks,
Vito
Still not working. What I noticed is that your suggestion doesn't work, the focusedview comes up with the wrong view. Here is my latest code.
function GetExportClassByDetailGridView(AGridView: TcxCustomGridView): TcxGridCustomExportClass;
begin
Result := TcxGridTableViewExport
end;
procedure ExportDetailGridToFile(AFileName: string; AExportType: Integer;
AGrid: TcxGrid;
AGridView: TcxCustomGridView;
AExpand, ASaveAll, AUseNativeFormat: Boolean;
const ASeparator, ABeginString, AEndString: string; const AFileExt: string);
var
AGridExport: TcxGridCustomExport;
begin
if AGrid <> nil then
begin
if AFileExt <> '' then
AFileName := ChangeFileExt(AFileName, '.' + AFileExt);
AGridExport := GetExportClassByDetailGridView(AGridView).Create(AFileName, AExportType, AGridView, AGrid, nil);
AGridExport.SaveAll := ASaveAll;
AGridExport.Expand := AExpand;
AGridExport.UseNativeFormat := AUseNativeFormat;
AGridExport.AddSeparators([ASeparator, ABeginString, AEndString]);
AGrid.BeginUpdate;
try
try
AGridExport.DoExport;
finally
AGridExport.Free;
end;
finally
AGrid.EndUpdate;
end;
end;
end;
procedure ExportDetailGridToExcel(const AFileName: string; AGrid: TcxGrid;
AGridView: TcxCustomGridView;
AExpand: Boolean; ASaveAll: Boolean; AUseNativeFormat: Boolean; const AFileExt: string);
begin
ExportDetailGridToFile(AFileName, 1, AGrid, AGridView, AExpand, ASaveAll, AUseNativeFormat, '', '', '', AFileExt);
end;
procedure ExportDetailToExcelFromGrid(DataGrid1: TcxGrid;
GridView1: TcxCustomGridView;
GridDataSet1: TClientDataSet;
sFileName1: string;
ExportTo: TExportType);
var
PrevCursor: TCursor;
sTempFileName: string;
begin
if ExportTo <> etCancel then
begin
PrevCursor := Screen.Cursor;
Screen.Cursor := crHourglass;
try
case ExportTo of
etExcel : begin
if DataGrid1 <> nil then
begin
if FileExists(sTempFileName) then
DeleteFile(PChar(sTempFileName));
ExportDetailGridToExcel(sFileName1,DataGrid1,GridView1,True,True,False,'xls');
ShellExecute(Application.Handle, 'open',PChar(sTempFileName), nil, nil, SW_SHOWNORMAL);
end;
end;
etXML : begin
if DataGrid1 <> nil then
begin
sTempFileName:= sFileName1 + '.xml';
if FileExists(sTempFileName) then
DeleteFile(PChar(sTempFileName));
ExportGridToXML(sTempFileName,DataGrid1,True,True);
PelotonShellExecute(sTempFileName,TRUE);
end;
end;
etHTML : begin
if DataGrid1 <> nil then
begin
sTempFileName:= sFileName1 + '.html';
if FileExists(sTempFileName) then
DeleteFile(PChar(sTempFileName));
ExportGridToHTML(sTempFileName,DataGrid1,True,True);
PelotonShellExecute(sTempFileName,TRUE);
end;
end;
etExcelRawData :
begin
if DataGrid1 <> nil then
begin
sTempFileName:= sFileName1 + '.xls';
if FileExists(sTempFileName) then
DeleteFile(PChar(sTempFileName));
ExportDataSetToExcel(GridDataSet1);
ShellExecute(Application.Handle, 'open',PChar(sTempFileName), nil, nil, SW_SHOWNORMAL);
end;
end;
end;
finally
Screen.Cursor := PrevCursor;
end;
end;
end;
Hi Philip,
As stated in the KB article, you should manually focus the required clone view before exporting its content. Attached is a small sample, illustrating this approach in action. Hopefully, it will be of some help.
Thanks,
Vito
Thank you, that worked great.