-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDefaultTableExport.cs
More file actions
190 lines (166 loc) · 8.06 KB
/
DefaultTableExport.cs
File metadata and controls
190 lines (166 loc) · 8.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/
using BootstrapBlazor.Components.Extensions;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using MiniExcelLibs;
using MiniExcelLibs.OpenXml;
using System.Text;
namespace BootstrapBlazor.Components;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="serviceProvider"></param>
class DefaultTableExport(IServiceProvider serviceProvider) : ITableExport
{
/// <summary>
/// <inheritdoc/>
/// </summary>
public Task<bool> ExportAsync<TModel>(IEnumerable<TModel> items, IEnumerable<ITableColumn>? cols = null, string? fileName = null) => InternalExportAsync(items, cols, ExcelType.UNKNOWN, fileName);
/// <summary>
/// <inheritdoc/>
/// </summary>
public Task<bool> ExportAsync<TModel>(IEnumerable<TModel> items, IEnumerable<ITableColumn>? cols, TableExportOptions options, string? fileName = null) => InternalExportAsync(items, cols, ExcelType.UNKNOWN, fileName, options);
/// <summary>
/// <inheritdoc/>
/// </summary>
public Task<bool> ExportExcelAsync<TModel>(IEnumerable<TModel> items, IEnumerable<ITableColumn>? cols = null, string? fileName = null) => InternalExportAsync(items, cols, ExcelType.XLSX, fileName);
/// <summary>
/// <inheritdoc/>
/// </summary>
public Task<bool> ExportExcelAsync<TModel>(IEnumerable<TModel> items, IEnumerable<ITableColumn>? cols, TableExportOptions options, string? fileName = null) => InternalExportAsync(items, cols, ExcelType.XLSX, fileName, options);
/// <summary>
/// <inheritdoc/>
/// </summary>
public Task<bool> ExportCsvAsync<TModel>(IEnumerable<TModel> items, IEnumerable<ITableColumn>? cols, string? fileName = null) => InternalExportAsync(items, cols, ExcelType.CSV, fileName);
/// <summary>
/// <inheritdoc/>
/// </summary>
public Task<bool> ExportCsvAsync<TModel>(IEnumerable<TModel> items, IEnumerable<ITableColumn>? cols, TableExportOptions options, string? fileName = null) => InternalExportAsync(items, cols, ExcelType.CSV, fileName, options);
private async Task<bool> InternalExportAsync<TModel>(IEnumerable<TModel> items, IEnumerable<ITableColumn>? cols, ExcelType excelType, string? fileName = null, TableExportOptions? options = null)
{
options ??= serviceProvider.GetRequiredService<IOptions<BootstrapBlazorOptions>>().Value.TableSettings.TableExportOptions;
cols ??= Utility.GetTableColumns<TModel>();
IConfiguration? configuration = null;
if (excelType == ExcelType.XLSX)
{
configuration = new OpenXmlConfiguration()
{
AutoFilter = options.EnableAutoFilter,
EnableAutoWidth = options.EnableAutoWidth,
};
}
var lookupService = serviceProvider.GetRequiredService<ILookupService>();
var value = new ExportDataReader<TModel>(items, cols, options, lookupService);
using var stream = new MemoryStream();
await MiniExcel.SaveAsAsync(stream, value, excelType: excelType, configuration: configuration);
fileName ??= $"ExportData_{DateTime.Now:yyyyMMddHHmmss}.{GetExtension()}";
stream.Position = 0;
var downloadService = serviceProvider.GetRequiredService<DownloadService>();
await downloadService.DownloadFromStreamAsync(fileName, stream);
return true;
string GetExtension() => excelType == ExcelType.XLSX ? "xlsx" : "csv";
}
/// <summary>
/// <inheritdoc/>
/// </summary>
public Task<bool> ExportPdfAsync<TModel>(IEnumerable<TModel> items, IEnumerable<ITableColumn>? cols, string? fileName = null)
{
var options = serviceProvider.GetRequiredService<IOptions<BootstrapBlazorOptions>>().Value.TableSettings.TableExportOptions;
return ExportPdfAsync(items, cols, options, fileName, null);
}
/// <summary>
/// <inheritdoc/>
/// </summary>
public Task<bool> ExportPdfAsync<TModel>(IEnumerable<TModel> items, IEnumerable<ITableColumn>? cols, TableExportOptions options, string? fileName = null) => ExportPdfAsync(items, cols, options, fileName, null);
/// <summary>
/// <inheritdoc/>
/// </summary>
public async Task<bool> ExportPdfAsync<TModel>(IEnumerable<TModel> items, IEnumerable<ITableColumn>? cols, TableExportOptions options, string? fileName = null, IEnumerable<string>? links = null)
{
var ret = false;
var logger = serviceProvider.GetRequiredService<ILogger<DefaultTableExport>>();
var lookupService = serviceProvider.GetRequiredService<ILookupService>();
try
{
var tags = GetDefaultLinks();
if (links != null)
{
tags.AddRange(links);
}
var linkString = string.Join("", tags.Select(i => $"<link rel=\"stylesheet\" href=\"{i}\">"));
var html = await GenerateTableHtmlAsync(items, cols, options, lookupService);
var htmlString = $"""
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
{linkString}
</head>
<body class="p-3">
{html}
</body>
</html>
""";
// 得到 Pdf 文件数据
var pdfService = serviceProvider.GetRequiredService<IHtml2Pdf>();
var stream = await pdfService.PdfStreamFromHtmlAsync(htmlString);
// 下载 Pdf 文件
var downloadService = serviceProvider.GetRequiredService<DownloadService>();
fileName ??= $"ExportData_{DateTime.Now:yyyyMMddHHmmss}.pdf";
await downloadService.DownloadFromStreamAsync(fileName, stream);
ret = true;
}
catch (Exception ex)
{
logger.LogError(ex, "ExportPdfAsync execute failed");
}
return ret;
}
private List<string> GetDefaultLinks()
{
var navigationManager = serviceProvider.GetRequiredService<NavigationManager>();
var baseUri = navigationManager.BaseUri;
return
[
$"{baseUri}_content/BootstrapBlazor.FontAwesome/css/font-awesome.min.css",
$"{baseUri}_content/BootstrapBlazor.MaterialDesign/css/md.min.css",
$"{baseUri}_content/BootstrapBlazor.BootstrapIcon/css/bootstrap-icons.min.css",
$"{baseUri}_content/BootstrapBlazor/css/bootstrap.blazor.bundle.min.css",
$"{baseUri}_content/BootstrapBlazor/css/motronic.min.css"
];
}
private static async Task<string> GenerateTableHtmlAsync<TModel>(IEnumerable<TModel> items, IEnumerable<ITableColumn>? cols, TableExportOptions options, ILookupService lookupService)
{
var builder = new StringBuilder();
cols ??= Utility.GetTableColumns<TModel>();
builder.AppendLine("<table class=\"table table-bordered table-striped\">");
// 生成表头
builder.AppendLine("<thead><tr>");
foreach (var pi in cols)
{
builder.AppendLine($"<th><div class=\"table-cell\">{pi.GetDisplayName()}</div></th>");
}
builder.AppendLine("</tr></thead>");
builder.AppendLine("<tbody>");
foreach (var item in items)
{
if (item != null)
{
builder.AppendLine("<tr>");
foreach (var pi in cols)
{
var val = await pi.FormatValueAsync(Utility.GetPropertyValue(item, pi.GetFieldName()), options, lookupService);
builder.AppendLine($"<td><div class=\"table-cell\">{val}</div></td>");
}
builder.AppendLine("</tr>");
}
}
builder.AppendLine("</tbody>");
builder.AppendLine("</table>");
return builder.ToString();
}
}