diff --git a/exclusion.dic b/exclusion.dic index 8532dbfa4fa..1ad05d9bec0 100644 --- a/exclusion.dic +++ b/exclusion.dic @@ -55,3 +55,4 @@ Splittings Foos Localizer onchange +Render diff --git a/localization/de.json b/localization/de.json index b06a7dd6bc5..cbb22d2c10a 100644 --- a/localization/de.json +++ b/localization/de.json @@ -230,6 +230,7 @@ "ExportToastTitle": "Daten exportieren", "ExportToastContent": "Daten exportieren {0}, automatisches Schließen in {1} Sekunden", "ExportToastInProgressContent": "Daten exportieren, automatisches Schließen in {0} Sekunden", + "ExportCsvDropdownItemText": "MS-Csv", "ExportExcelDropdownItemText": "MS-Excel", "ExportPdfDropdownItemText": "Pdf", "PageInfoText": "{0} - {1} Total {2}", diff --git a/localization/es.json b/localization/es.json index 99ebbec61db..ce6336ce572 100644 --- a/localization/es.json +++ b/localization/es.json @@ -230,6 +230,7 @@ "ExportToastTitle": "Exportar datos", "ExportToastContent": "Exportar datos {0}, cierre automático en {0} segundos", "ExportToastInProgressContent": "Exportar datos, cierre automático en {0} segundos", + "ExportCsvDropdownItemText": "MS-Csv", "ExportExcelDropdownItemText": "MS-Excel", "ExportPdfDropdownItemText": "Pdf", "PageInfoText": "{0} - {1} Total {2}", diff --git a/localization/pt.json b/localization/pt.json index e8aacbb2984..ae88a27804b 100644 --- a/localization/pt.json +++ b/localization/pt.json @@ -230,6 +230,7 @@ "ExportToastTitle": "Exportar dados", "ExportToastContent": "Exportar dados {0}, Fechamento automático em {0} segundos", "ExportToastInProgressContent": "Exportar dados, Fechamento automático em {0} segundos", + "ExportCsvDropdownItemText": "MS-Csv", "ExportExcelDropdownItemText": "MS-Excel", "ExportPdfDropdownItemText": "Pdf", "PageInfoText": "{0} - {1} Total {2}", diff --git a/localization/th-TH.json b/localization/th-TH.json index 3f08d9fbe69..88542ac1fb9 100644 --- a/localization/th-TH.json +++ b/localization/th-TH.json @@ -1,4 +1,4 @@ -{ +{ "BootstrapBlazor.Components.AutoComplete": { "NoDataTip": "ไม่มีข้อมูลที่ตรงกัน", "PlaceHolder": "กรุณากรอก" @@ -225,6 +225,7 @@ "ExportToastTitle": "ส่งออกข้อมูล", "ExportToastContent": "ส่งออกข้อมูล {0},{1} ปิดอัตโนมัติหลังจากวินาที", "ExportToastInProgressContent": "กำลังส่งออกข้อมูล โปรดทราบภายหลัง, {0} ปิดอัตโนมัติหลังจากวินาที", + "ExportCsvDropdownItemText": "ไมโครซอฟท์ Csv รูปแบบ", "ExportExcelDropdownItemText": "ไมโครซอฟท์ Excel รูปแบบ", "ExportPdfDropdownItemText": "Pdf รูปแบบ", "PageInfoText": "{0} - {1} ทั้งหมด {2} บทความ", diff --git a/localization/zh-TW.json b/localization/zh-TW.json index 7bd84d686a1..56fa9eabde4 100644 --- a/localization/zh-TW.json +++ b/localization/zh-TW.json @@ -239,6 +239,7 @@ "ExportToastTitle": "匯出資料", "ExportToastContent": "匯出資料 {0},{1} 秒後自動關閉", "ExportToastInProgressContent": "正在匯出資料,請稍後, {0} 秒後自動關閉", + "ExportCsvDropdownItemText": "微軟 Csv 格式", "ExportExcelDropdownItemText": "微軟 Excel 格式", "ExportPdfDropdownItemText": "Pdf 格式" }, diff --git a/src/BootstrapBlazor.Server/BootstrapBlazor.Server.csproj b/src/BootstrapBlazor.Server/BootstrapBlazor.Server.csproj index 30ab477da6d..557e9df10b4 100644 --- a/src/BootstrapBlazor.Server/BootstrapBlazor.Server.csproj +++ b/src/BootstrapBlazor.Server/BootstrapBlazor.Server.csproj @@ -42,12 +42,12 @@ - + - + @@ -56,7 +56,7 @@ - + diff --git a/src/BootstrapBlazor.Server/Components/Samples/Html2Pdfs.razor.cs b/src/BootstrapBlazor.Server/Components/Samples/Html2Pdfs.razor.cs index 710ba2c9382..68020d96e78 100644 --- a/src/BootstrapBlazor.Server/Components/Samples/Html2Pdfs.razor.cs +++ b/src/BootstrapBlazor.Server/Components/Samples/Html2Pdfs.razor.cs @@ -19,10 +19,6 @@ public partial class Html2Pdfs [NotNull] private DownloadService? DownloadService { get; set; } - [Inject] - [NotNull] - private IComponentHtmlRenderer? HtmlRenderService { get; set; } - [Inject] [NotNull] private IWebHostEnvironment? WebHostEnvironment { get; set; } @@ -63,7 +59,7 @@ private async Task OnExportAsync() // 拼接导出文件网址 var url = $"{NavigationManager.BaseUri}{fileName}"; - var data = await PdfService.ExportDataAsync(url); + var data = await PdfService.PdfDataAsync(url); using var stream = new MemoryStream(data); await DownloadService.DownloadFromStreamAsync("table.pdf", stream); await ToastService.Success("Pdf Export", "Export pdf element success."); diff --git a/src/BootstrapBlazor.Server/Components/Samples/Table/TablesExport.razor b/src/BootstrapBlazor.Server/Components/Samples/Table/TablesExport.razor index 9c5c4092e7d..2acd3c4ae2a 100644 --- a/src/BootstrapBlazor.Server/Components/Samples/Table/TablesExport.razor +++ b/src/BootstrapBlazor.Server/Components/Samples/Table/TablesExport.razor @@ -10,7 +10,7 @@ -

@Localizer["TablesExportTips"]:

+

@Localizer["TablesExportTips"]:

public void ConfigureServices(IServiceCollection services)
 {
@@ -38,6 +38,24 @@
     
 
 
+
+    
+        
+            
+            
+            
+            
+        
+    
+
+ diff --git a/src/BootstrapBlazor.Server/Components/Samples/Table/TablesExport.razor.cs b/src/BootstrapBlazor.Server/Components/Samples/Table/TablesExport.razor.cs index 8ac6bf28926..c9059cbea6d 100644 --- a/src/BootstrapBlazor.Server/Components/Samples/Table/TablesExport.razor.cs +++ b/src/BootstrapBlazor.Server/Components/Samples/Table/TablesExport.razor.cs @@ -25,6 +25,7 @@ public partial class TablesExport /// [NotNull] private List? Items { get; set; } + private static IEnumerable PageItemsSource => new int[] { 4, 10, 20 }; /// @@ -53,7 +54,7 @@ private async Task OnExportAsync(ITableExportDataContext context) // 自定义导出方法 // 通过 context 参数可以自己查询数据进行导出操作 // 本例使用 context 传递来的 Rows/Columns 自定义文件名为 Test.xlsx - var ret = await Exporter.ExportAsync(context.Rows, context.Columns, "Test.xlsx"); + var ret = await TableExport.ExportExcelAsync(context.Rows, context.Columns, "Test.xlsx"); // 返回 true 时自动弹出提示框 return ret; @@ -61,7 +62,7 @@ private async Task OnExportAsync(ITableExportDataContext context) [Inject] [NotNull] - private ITableExcelExport? Exporter { get; set; } + private ITableExport? TableExport { get; set; } [Inject] [NotNull] @@ -124,7 +125,7 @@ private async Task ExcelExportAsync(ITableExportContext context) // 自定义导出模板导出当前页面数据为 Excel 方法 // 使用 BootstrapBlazor 内置服务 ITableExcelExport 实例方法 ExportAsync 进行导出操作 // 导出数据使用 context 传递来的 Rows/Columns 即为当前页数据 - var ret = await Exporter.ExportAsync(context.Rows, context.Columns, $"Test_{DateTime.Now:yyyyMMddHHmmss}.xlsx"); + var ret = await TableExport.ExportExcelAsync(context.Rows, context.Columns, $"Test_{DateTime.Now:yyyyMMddHHmmss}.xlsx"); // 返回 true 时自动弹出提示框 await ShowToast(ret); @@ -145,7 +146,7 @@ private async Task ExcelExportAllAsync(ITableExportContext context) var data = Items.Where(filter.GetFilterFunc()); // 导出符合条件的所有数据 data - var ret = await Exporter.ExportAsync(data, context.Columns, $"Test_{DateTime.Now:yyyyMMddHHmmss}.xlsx"); + var ret = await TableExport.ExportExcelAsync(data, context.Columns, $"Test_{DateTime.Now:yyyyMMddHHmmss}.xlsx"); // 返回 true 时自动弹出提示框 await ShowToast(ret); @@ -156,7 +157,7 @@ private async Task CsvExportAsync(ITableExportContext context) // 自定义导出模板导出当前页面数据为 Csv 方法 // 使用 BootstrapBlazor 内置服务 ITableExcelExport 实例方法 ExportCsvAsync 进行导出操作 // 导出数据使用 context 传递来的 Rows/Columns 即为当前页数据 - var ret = await Exporter.ExportCsvAsync(context.Rows, context.Columns, $"Test_{DateTime.Now:yyyyMMddHHmmss}.csv"); + var ret = await TableExport.ExportCsvAsync(context.Rows, context.Columns, $"Test_{DateTime.Now:yyyyMMddHHmmss}.csv"); // 返回 true 时自动弹出提示框 await ShowToast(ret); diff --git a/src/BootstrapBlazor.Server/Extensions/ServicesExtensions.cs b/src/BootstrapBlazor.Server/Extensions/ServicesExtensions.cs index 73494c670a3..c41437445ff 100644 --- a/src/BootstrapBlazor.Server/Extensions/ServicesExtensions.cs +++ b/src/BootstrapBlazor.Server/Extensions/ServicesExtensions.cs @@ -143,6 +143,7 @@ public static IServiceCollection AddWebSiteServices(this IServiceCollection serv // 增加 Table Excel 导出服务 services.AddBootstrapBlazorTableExcelExport(); + services.AddTransient(); return services; } diff --git a/src/BootstrapBlazor.Server/Locales/en.json b/src/BootstrapBlazor.Server/Locales/en.json index dd302c982f0..725b7707725 100644 --- a/src/BootstrapBlazor.Server/Locales/en.json +++ b/src/BootstrapBlazor.Server/Locales/en.json @@ -5136,6 +5136,8 @@ "TablesExportNote2": "Add Table Excel export service", "TablesExportShowExportButtonTitle": "Table export function", "TablesExportShowExportButtonIntro": "Whether to display the export button by setting the ShowExportButton property, the default is false", + "TablesExportShowExportCsvButtonTitle": "Export Csv/Pdf Button", + "TablesExportShowExportCsvButtonIntro": "Show/hide Csv/Pdf button by set ShowExportCsvButton=\"true\" ShowExportPdfButton=\"true\"", "TablesExportOnExportAsyncTitle": "Custom export method", "TablesExportOnExportAsyncIntro": "You can customize the export method by setting the OnExportAsync callback delegate method. If you don't set it, the built-in export function of the component will be used.", "TablesExportButtonDropdownTemplateTitle": "Custom export dropdown button", diff --git a/src/BootstrapBlazor.Server/Locales/zh.json b/src/BootstrapBlazor.Server/Locales/zh.json index 6e88057c773..34d5a31fba9 100644 --- a/src/BootstrapBlazor.Server/Locales/zh.json +++ b/src/BootstrapBlazor.Server/Locales/zh.json @@ -5136,6 +5136,8 @@ "TablesExportNote2": "增加 Table Excel 导出服务", "TablesExportShowExportButtonTitle": "表格导出功能", "TablesExportShowExportButtonIntro": "通过设置 ShowExportButton=\"true\" 属性是否显示导出按钮,默认为false", + "TablesExportShowExportCsvButtonTitle": "导出 Csv/Pdf", + "TablesExportShowExportCsvButtonIntro": "通过设置 ShowExportCsvButton=\"true\" ShowExportPdfButton=\"true\" 控制 Csv/Pdf 导出按钮", "TablesExportOnExportAsyncTitle": "自定义导出方法", "TablesExportOnExportAsyncIntro": "通过设置 OnExportAsync 回调委托方法可自定义导出方法,不设置将使用组件内置导出函数", "TablesExportButtonDropdownTemplateTitle": "自定义导出下拉框按钮", diff --git a/src/BootstrapBlazor.Server/Services/TableDemoDataService.cs b/src/BootstrapBlazor.Server/Services/TableDemoDataService.cs index 0e98c829daf..825d41fc7b7 100644 --- a/src/BootstrapBlazor.Server/Services/TableDemoDataService.cs +++ b/src/BootstrapBlazor.Server/Services/TableDemoDataService.cs @@ -7,17 +7,12 @@ namespace BootstrapBlazor.Server.Services; /// /// 演示网站示例数据注入服务实现类 /// -internal class TableDemoDataService : DataServiceBase where TModel : class, new() +class TableDemoDataService(IStringLocalizer localizer) : DataServiceBase where TModel : class, new() { [NotNull] private List? Items { get; set; } - private IStringLocalizer Localizer { get; set; } - - public TableDemoDataService(IStringLocalizer localizer) - { - Localizer = localizer; - } + private IStringLocalizer Localizer { get; } = localizer; /// /// 查询操作方法 diff --git a/src/BootstrapBlazor.Server/Services/TableExportPdfService.cs b/src/BootstrapBlazor.Server/Services/TableExportPdfService.cs new file mode 100644 index 00000000000..4f243d7f2b5 --- /dev/null +++ b/src/BootstrapBlazor.Server/Services/TableExportPdfService.cs @@ -0,0 +1,46 @@ +// 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/ + +namespace BootstrapBlazor.Server.Services; + +class TableExportPdfService(IWebHostEnvironment webHostEnvironment, NavigationManager navigationManager, IHtml2Pdf html2Pdf) : ITableExportPdf +{ + private readonly IWebHostEnvironment _webHostEnvironment = webHostEnvironment; + private readonly NavigationManager _navigationManager = navigationManager; + private readonly IHtml2Pdf _html2Pdf = html2Pdf; + + public async Task PdfDataAsync(string content) + { + var url = await GenerateHtmlAsync(content); + + // 生成 Pdf + return await _html2Pdf.PdfDataAsync(url); + } + + public async Task PdfStreamAsync(string content) + { + var url = await GenerateHtmlAsync(content); + + // 生成 Pdf + return await _html2Pdf.PdfStreamAsync(url); + } + + private async Task GenerateHtmlAsync(string content) + { + // 通过 template 模板文件生成网页文件 + var templateFileName = Path.Combine(_webHostEnvironment.WebRootPath, "pdf/template.htm"); + var template = await File.ReadAllTextAsync(templateFileName); + + // 生成静态 html 文件 + var htmlFileName = $"pdf/{Guid.NewGuid()}.html"; + var filePath = Path.Combine(_webHostEnvironment.WebRootPath, htmlFileName); + using var writer = File.CreateText(filePath); + await writer.WriteLineAsync(string.Format(template, content)); + await writer.FlushAsync(); + writer.Close(); + + // 拼接导出文件网址 + return $"{_navigationManager.BaseUri}{htmlFileName}"; + } +} diff --git a/src/BootstrapBlazor/BootstrapBlazor.csproj b/src/BootstrapBlazor/BootstrapBlazor.csproj index f0c6c5518b5..5b5a5a4c990 100644 --- a/src/BootstrapBlazor/BootstrapBlazor.csproj +++ b/src/BootstrapBlazor/BootstrapBlazor.csproj @@ -1,7 +1,7 @@ - 8.0.5 + 8.0.6-beta01 diff --git a/src/BootstrapBlazor/Components/Table/Table.razor b/src/BootstrapBlazor/Components/Table/Table.razor index fbeaaf06758..0dc0278e39a 100644 --- a/src/BootstrapBlazor/Components/Table/Table.razor +++ b/src/BootstrapBlazor/Components/Table/Table.razor @@ -125,6 +125,13 @@ } else { + if (ShowExportCsvButton) + { + + } if (ShowExportExcelButton) {