11@page " /"
22@rendermode InteractiveServer
33@inject IJSRuntime JS
4-
4+ @ using DevExpress . XtraReports . UI
55@using GridMasterDetailExport .Data
6+ @using GridMasterDetailExport .Reports
67
78<DxGrid @ref =" MasterGrid"
89 AllowColumnReorder =" false"
910 AllowSort =" false"
1011 Data =" DataProvider.GetUsers()"
1112 KeyFieldName =" UserID" >
1213 <ToolbarTemplate Context =" ctx" >
13- <form id =" @exportFormId" class =" w-100" target =" _blank" method =" post"
14- action =" api/home/@(exportFormat)" >
15- <AntiforgeryToken />
16- <input type =" hidden" name =" masterColumns" value =" @masterColumns" />
17- <input type =" hidden" name =" detailColumns" value =" @detailColumns" />
18- <input type =" hidden" name =" expandedStates" value =" @expandedStates" />
19- <DxToolbar >
20- <Items >
21- <DxToolbarItem Text =" Export as PDF"
22- Click =' async() => await ExportTo("pdf")' />
23- <DxToolbarItem Text =" Export as XLSX"
24- Click =' async() => await ExportTo("xlsx")' />
25- <DxToolbarItem Text =" Export as CSV"
26- Click =' async() => await ExportTo("csv")' />
27- </Items >
28- </DxToolbar >
29- </form >
14+ <DxToolbar >
15+ <Items >
16+ <DxToolbarItem Text =" Export as PDF" Click =" ExportToPdf" />
17+ <DxToolbarItem Text =" Export as XLSX" Click =" ExportToXlsx" />
18+ <DxToolbarItem Text =" Export as CSV" Click =" ExportToCsv" />
19+ </Items >
20+ </DxToolbar >
3021 </ToolbarTemplate >
3122 <Columns >
3223 <DxGridDataColumn FieldName =" UserID" />
5243 </DxGrid >
5344 </DetailRowTemplate >
5445</DxGrid >
55-
46+ <script >
47+ window .downloadFileFromStream = async (fileName , contentStreamReference ) => {
48+ const arrayBuffer = await contentStreamReference .arrayBuffer ();
49+ const blob = new Blob ([arrayBuffer]);
50+ const url = URL .createObjectURL (blob);
51+ const anchorElement = document .createElement (' a' );
52+ anchorElement .href = url;
53+ anchorElement .download = fileName ?? ' ' ;
54+ anchorElement .click ();
55+ anchorElement .remove ();
56+ URL .revokeObjectURL (url);
57+ }
58+ < / script>
5659@code {
57- private readonly string exportFormId = " export-form" ;
58- private string ? masterColumns ;
59- private string ? detailColumns ;
60- private string ? expandedStates ;
61- private string exportFormat = " pdf" ;
62- private DxGrid ? masterGrid ;
63- private DxGrid ? detailGrid ;
60+ public DxGrid? MasterGrid { get; set; }
6461
65- public DxGrid ? MasterGrid {
66- get => masterGrid ;
67- private set {
68- if (masterGrid is null ) {
69- masterGrid = value ;
70- masterColumns = string .Join (" ;" ,
71- GetVisibleColumnNames (masterGrid ));
72- }
73- }
62+ public DxGrid? DetailGrid { get; set; }
63+
64+ private async Task ExportToPdf () {
65+ using var report = GetReport ();
66+ using var stream = new MemoryStream ();
67+ await report .ExportToPdfAsync (stream);
68+ await DownloadStreamAsync (stream, " exportResult.pdf" );
7469 }
7570
76- public DxGrid ? DetailGrid {
77- get => detailGrid ;
78- private set {
79- if (detailGrid is null ) {
80- detailGrid = value ;
81- detailColumns = string .Join (" ;" ,
82- GetVisibleColumnNames (detailGrid ));
83- }
84- }
71+ private async Task ExportToXlsx () {
72+ using var report = GetReport ();
73+ using var stream = new MemoryStream ();
74+ await report .ExportToXlsxAsync (stream);
75+ await DownloadStreamAsync (stream, " exportResult.xlsx" );
8576 }
8677
87- private async Task ExportTo (string format ) {
88- await Task .Run (() => {
89- exportFormat = format ;
90- expandedStates = string .Join (" ;" , GetRowsExpandedStates ());
91- });
92- await JS .InvokeVoidAsync (" submitForm" , exportFormId );
78+ private async Task ExportToCsv () {
79+ using var report = GetReport ();
80+ using var stream = new MemoryStream ();
81+ await report .ExportToCsvAsync (stream);
82+ await DownloadStreamAsync (stream, " exportResult.csv" );
9383 }
9484
95- private IEnumerable <string > GetVisibleColumnNames (IGrid ? grid ) {
85+ private XtraReport GetReport () {
86+ return ReportGenerationHelpers .GetReportFromDxGrid (
87+ GetVisibleColumnNames (MasterGrid),
88+ GetVisibleColumnNames (DetailGrid),
89+ GetRowsExpandedStates ()
90+ );
91+ }
92+
93+ private static string[] GetVisibleColumnNames (IGrid ? grid) {
9694 return grid? .GetVisibleColumns ()
9795 .OfType < DxGridDataColumn> ()
98- .Select (c => c .FieldName ?? c .Name ) ?? [];
96+ .Select (c => c .FieldName ?? c .Name ). ToArray () ?? [];
9997 }
10098
10199 private Dictionary< string, bool> GetRowsExpandedStates () {
102100 Dictionary< string, bool> masterRowExpandedStates = new ();
103- for (int i = 0 ; i < masterGrid ? .GetVisibleRowCount (); i ++ ) {
104- if (masterGrid .GetDataItem (i ) is User user ) {
101+ for (int i = 0 ; i < MasterGrid ? .GetVisibleRowCount (); i++ ) {
102+ if (MasterGrid .GetDataItem (i) is User user) {
105103 masterRowExpandedStates[user .UserID .ToString ()]
106- = masterGrid .IsDetailRowExpanded (i );
104+ = MasterGrid .IsDetailRowExpanded (i);
107105 }
108106 }
109107 return masterRowExpandedStates;
110108 }
109+
110+ async Task DownloadStreamAsync (Stream stream , string fileName ) {
111+
112+ stream .Seek (0 , SeekOrigin .Begin );
113+
114+ using var streamRef = new DotNetStreamReference (stream);
115+ await JS .InvokeVoidAsync (" downloadFileFromStream" , fileName, streamRef);
116+ }
111117}
0 commit comments