Skip to content

Commit 8c1e052

Browse files
committed
Initial commit
1 parent 874ebfe commit 8c1e052

62 files changed

Lines changed: 290 additions & 60 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

BlazorBlankServer/Components/Pages/Index.razor

Lines changed: 0 additions & 6 deletions
This file was deleted.

BlazorBlankServer/Properties/launchSettings.json

Lines changed: 0 additions & 38 deletions
This file was deleted.

BlazorBlankServer.sln renamed to PivotTable_Export_via_SpreadsheetDocument_API.sln

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 17
44
VisualStudioVersion = 17.14.36717.8
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorBlankServer", "BlazorBlankServer\BlazorBlankServer.csproj", "{4D9213A4-F387-AF23-B44F-9A1123A994BF}"
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PivotTable_Export_via_SpreadsheetDocument_API", "PivotTable_Export_via_SpreadsheetDocument_API\PivotTable_Export_via_SpreadsheetDocument_API.csproj", "{4D9213A4-F387-AF23-B44F-9A1123A994BF}"
77
EndProject
88
Global
99
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -19,4 +19,7 @@ Global
1919
GlobalSection(SolutionProperties) = preSolution
2020
HideSolutionNode = FALSE
2121
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {082F76BE-8638-48FE-B326-3BB4FB032298}
24+
EndGlobalSection
2225
EndGlobal

BlazorBlankServer/Components/App.razor renamed to PivotTable_Export_via_SpreadsheetDocument_API/Components/App.razor

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,29 @@
1010
@DxResourceManager.RegisterTheme(FluentLight)
1111
@DxResourceManager.RegisterScripts()
1212
<link href=@AppendVersion("css/site.css") rel="stylesheet" />
13-
<link href=@AppendVersion("BlazorBlankServer.styles.css") rel="stylesheet" />
13+
<link href=@AppendVersion("PivotTable_Export_via_SpreadsheetDocument_API.styles.css") rel="stylesheet" />
1414
<HeadOutlet />
1515
</head>
1616
<body class="dxbl-theme-fluent">
1717
<Routes />
1818
<script src="_framework/blazor.web.js"></script>
19+
<script>
20+
async function downloadFileFromStream(fileName, contentStreamReference) {
21+
const arrayBuffer = await contentStreamReference.arrayBuffer();
22+
const blob = new Blob([arrayBuffer]);
23+
const url = URL.createObjectURL(blob);
24+
const a = document.createElement('a');
25+
a.href = url;
26+
a.download = fileName ?? '';
27+
a.click();
28+
URL.revokeObjectURL(url);
29+
}
30+
</script>
1931
</body>
2032
2133
</html>
2234
2335
@code {
2436
private string AppendVersion(string path) => FileVersionProvider.AddFileVersionToPath("/", path);
2537
private static readonly ITheme FluentLight = Themes.Fluent.Clone(p => p.AddFilePaths($"css/theme-fluent.css"));
26-
}
38+
}

BlazorBlankServer/Components/Layout/MainLayout.razor renamed to PivotTable_Export_via_SpreadsheetDocument_API/Components/Layout/MainLayout.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
<div class="page">
44
@Body
5-
</div>
5+
</div>

BlazorBlankServer/Components/Layout/MainLayout.razor.css renamed to PivotTable_Export_via_SpreadsheetDocument_API/Components/Layout/MainLayout.razor.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,4 @@
8181
.nav-buttons-container ::deep .dxbl-btn-icon-only {
8282
--dxbl-btn-padding-x: 0.75rem;
8383
--dxbl-btn-padding-y: 0.25rem;
84-
}
84+
}

BlazorBlankServer/Components/Pages/Error.razor renamed to PivotTable_Export_via_SpreadsheetDocument_API/Components/Pages/Error.razor

File renamed without changes.
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
@page "/"
2+
3+
@rendermode InteractiveServer
4+
@inject Services.PivotTableExportService ExportService
5+
@inject IJSRuntime JS
6+
7+
<PageTitle>Pivot Table</PageTitle>
8+
9+
<h1>Pivot Table</h1>
10+
11+
<DxButton Text="Export to Excel" Click="OnExportClick" CssClass="export-btn" />
12+
13+
<DxPivotTable Data="@PivotTableData" @ref="pivotTable" CssClass="h-auto">
14+
<Fields>
15+
<DxPivotTableField Area="PivotTableArea.Row"
16+
Field="@nameof(SaleInfo.Region)"
17+
SortOrder="PivotTableSortOrder.Descending" />
18+
<DxPivotTableField Area="PivotTableArea.Row"
19+
Field="@nameof(SaleInfo.Country)" />
20+
<DxPivotTableField Area="PivotTableArea.Column"
21+
Field="@nameof(SaleInfo.Date)"
22+
GroupInterval="PivotTableGroupInterval.DateYear"
23+
Caption="Year" />
24+
<DxPivotTableField Area="PivotTableArea.Column"
25+
Field="@nameof(SaleInfo.Date)"
26+
GroupInterval="PivotTableGroupInterval.DateQuarter"
27+
Caption="Quarter">
28+
<ValueTemplate>
29+
<span>@($"Q{context.Text}")</span>
30+
</ValueTemplate>
31+
</DxPivotTableField>
32+
<DxPivotTableField Area="PivotTableArea.Data"
33+
Field="@nameof(SaleInfo.Amount)"
34+
SummaryType="PivotTableSummaryType.Sum"
35+
CellFormat="C0" />
36+
<DxPivotTableField Area="PivotTableArea.Data"
37+
Field="@nameof(SaleInfo.OrderId)"
38+
SummaryType="PivotTableSummaryType.Count"
39+
Caption="Count" />
40+
</Fields>
41+
</DxPivotTable>
42+
43+
@code {
44+
IPivotTable pivotTable { get; set; } = null!;
45+
SaleInfo[]? PivotTableData;
46+
47+
protected override void OnInitialized() {
48+
var years = new[] { DateTime.Now.Year - 3, DateTime.Now.Year - 2, DateTime.Now.Year - 1 };
49+
var quarters = new[] { (1, 3), (4, 6), (7, 9), (10, 12) };
50+
51+
int id = 1;
52+
PivotTableData = Regions
53+
.SelectMany(region => region.Value
54+
.SelectMany(country => years
55+
.SelectMany(year => quarters
56+
.Select(q => new SaleInfo(
57+
id++,
58+
region.Key,
59+
country,
60+
Random.Shared.Next(500, 10000),
61+
new DateTime(year, Random.Shared.Next(q.Item1, q.Item2 + 1), Random.Shared.Next(1, 28))
62+
)))))
63+
.ToArray();
64+
}
65+
66+
async Task OnExportClick() {
67+
if (PivotTableData is null)
68+
return;
69+
70+
var layout = pivotTable.SaveLayout();
71+
72+
var exportConfig = new Services.PivotTableExportConfig {
73+
DataFieldConfigs = new() {
74+
["OrderId"] = new Services.DataFieldConfig {
75+
SummaryFunction = DevExpress.Spreadsheet.PivotDataConsolidationFunction.Count,
76+
Caption = "Count"
77+
}
78+
},
79+
ColumnFieldGroupings = new() {
80+
["Date"] = new Services.ColumnFieldGroupingConfig {
81+
GroupBy = DevExpress.Spreadsheet.PivotFieldGroupByType.Years | DevExpress.Spreadsheet.PivotFieldGroupByType.Quarters
82+
}
83+
}
84+
};
85+
86+
var fileBytes = ExportService.ExportToExcel(PivotTableData, layout, exportConfig);
87+
88+
using var stream = new MemoryStream(fileBytes);
89+
using var streamRef = new DotNetStreamReference(stream);
90+
await JS.InvokeVoidAsync("downloadFileFromStream", "PivotTableExport.xlsx", streamRef);
91+
}
92+
93+
public static readonly Dictionary<string, string[]> Regions = new() {
94+
["Africa"] = ["South Africa", "Egypt"],
95+
["Asia"] = ["UAE", "Japan", "India"],
96+
["Europe"] = ["United Kingdom", "Germany", "Spain"],
97+
["North America"] = ["USA", "Canada", "Mexico"],
98+
["South America"] = ["Brazil", "Argentina", "Chile"]
99+
};
100+
101+
public record SaleInfo(int OrderId, string Region, string Country, int Amount, DateTime Date);
102+
}

BlazorBlankServer/Components/Routes.razor renamed to PivotTable_Export_via_SpreadsheetDocument_API/Components/Routes.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
<Found Context="routeData">
33
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
44
</Found>
5-
</Router>
5+
</Router>

BlazorBlankServer/Components/_Imports.razor renamed to PivotTable_Export_via_SpreadsheetDocument_API/Components/_Imports.razor

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
@using Microsoft.AspNetCore.Components.Web.Virtualization
77
@using Microsoft.JSInterop
88
@using static Microsoft.AspNetCore.Components.Web.RenderMode
9-
@using BlazorBlankServer
10-
@using BlazorBlankServer.Components
11-
@using BlazorBlankServer.Components.Layout
9+
@using PivotTable_Export_via_SpreadsheetDocument_API
10+
@using PivotTable_Export_via_SpreadsheetDocument_API.Components
11+
@using PivotTable_Export_via_SpreadsheetDocument_API.Components.Layout
1212

13-
@using DevExpress.Blazor
13+
@using DevExpress.Blazor
14+
@using DevExpress.Blazor.PivotTable

0 commit comments

Comments
 (0)