From 7dead6772f96dacc4ea1999933213896a9c66558 Mon Sep 17 00:00:00 2001 From: Argo-Asicotech Date: Wed, 30 Nov 2022 00:21:24 +0800 Subject: [PATCH 1/4] =?UTF-8?q?feat:=20TableExport=20=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=20download=20service=20=E5=AE=8C=E6=88=90?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BootstrapBlazor.TableExport.csproj | 16 +++++--------- .../ExcelExport.cs | 22 +++++++++++++------ ...eExcelExportServiceCollectionExtensions.cs | 4 ++-- .../bundleconfig.json | 13 ----------- .../wwwroot/js/export.js | 10 --------- .../wwwroot/js/export.min.js | 1 - 6 files changed, 22 insertions(+), 44 deletions(-) delete mode 100644 src/Extensions/Components/BootstrapBlazor.TableExport/bundleconfig.json delete mode 100644 src/Extensions/Components/BootstrapBlazor.TableExport/wwwroot/js/export.js delete mode 100644 src/Extensions/Components/BootstrapBlazor.TableExport/wwwroot/js/export.min.js diff --git a/src/Extensions/Components/BootstrapBlazor.TableExport/BootstrapBlazor.TableExport.csproj b/src/Extensions/Components/BootstrapBlazor.TableExport/BootstrapBlazor.TableExport.csproj index a06dc39f4..ccb12768f 100644 --- a/src/Extensions/Components/BootstrapBlazor.TableExport/BootstrapBlazor.TableExport.csproj +++ b/src/Extensions/Components/BootstrapBlazor.TableExport/BootstrapBlazor.TableExport.csproj @@ -1,24 +1,18 @@ - - - + - 7.0.0 + 7.1.0 Bootstrap Blazor WebAssembly wasm UI Components Table Export Bootstrap UI components extensions of export - - - - - + + - - + diff --git a/src/Extensions/Components/BootstrapBlazor.TableExport/ExcelExport.cs b/src/Extensions/Components/BootstrapBlazor.TableExport/ExcelExport.cs index aef1743ea..80ee58d2a 100644 --- a/src/Extensions/Components/BootstrapBlazor.TableExport/ExcelExport.cs +++ b/src/Extensions/Components/BootstrapBlazor.TableExport/ExcelExport.cs @@ -1,4 +1,4 @@ -// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. +// 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/ @@ -13,8 +13,19 @@ namespace BootstrapBlazor.Components; /// internal class ExcelExport : ITableExcelExport { + private DownloadService DownloadService { get; set; } + + /// + /// 构造函数 + /// + /// + public ExcelExport(DownloadService downloadService) + { + DownloadService = downloadService; + } + /// - /// + /// 导出 Excel 方法 /// /// public async Task ExportAsync(IEnumerable items, IEnumerable cols, IJSRuntime jsRuntime) where TItem : class @@ -54,11 +65,8 @@ internal class ExcelExport : ITableExcelExport } var bytes = excelPackage.GetAsByteArray(); - var fileName = DateTime.Now.Ticks; - var contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; - var excelName = $"{fileName}.xlsx"; - var bytesBase64 = Convert.ToBase64String(bytes); - await jsRuntime.InvokeVoidAsync(identifier: "$.generatefile", excelName, bytesBase64, contentType); + var fileName = $"ExportData_{DateTime.Now:yyyyMMddHHmmss}.xlsx"; + await DownloadService.DownloadFromByteArrayAsync(fileName, bytes); return true; } diff --git a/src/Extensions/Components/BootstrapBlazor.TableExport/TableExcelExportServiceCollectionExtensions.cs b/src/Extensions/Components/BootstrapBlazor.TableExport/TableExcelExportServiceCollectionExtensions.cs index 7c6ebcd52..b51729608 100644 --- a/src/Extensions/Components/BootstrapBlazor.TableExport/TableExcelExportServiceCollectionExtensions.cs +++ b/src/Extensions/Components/BootstrapBlazor.TableExport/TableExcelExportServiceCollectionExtensions.cs @@ -2,7 +2,7 @@ // 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; +using Microsoft.Extensions.DependencyInjection.Extensions; namespace Microsoft.Extensions.DependencyInjection; @@ -18,7 +18,7 @@ public static class TableExcelExportServiceCollectionExtensions /// public static IServiceCollection AddBootstrapBlazorTableExcelExport(this IServiceCollection services) { - services.AddSingleton(); + services.TryAddTransient(); return services; } } diff --git a/src/Extensions/Components/BootstrapBlazor.TableExport/bundleconfig.json b/src/Extensions/Components/BootstrapBlazor.TableExport/bundleconfig.json deleted file mode 100644 index 0b9bbd995..000000000 --- a/src/Extensions/Components/BootstrapBlazor.TableExport/bundleconfig.json +++ /dev/null @@ -1,13 +0,0 @@ -[ - { - "outputFileName": "wwwroot/js/export.min.js", - "inputFiles": [ - "wwwroot/js/export.js" - ], - "minify": { - "enabled": true, - "renameLocals": true - }, - "sourceMap": false - } -] diff --git a/src/Extensions/Components/BootstrapBlazor.TableExport/wwwroot/js/export.js b/src/Extensions/Components/BootstrapBlazor.TableExport/wwwroot/js/export.js deleted file mode 100644 index c6aa2f10e..000000000 --- a/src/Extensions/Components/BootstrapBlazor.TableExport/wwwroot/js/export.js +++ /dev/null @@ -1,10 +0,0 @@ -(function ($) { - $.generatefile = function (fileName, bytesBase64, contenttype) { - var link = document.createElement('a'); - link.download = fileName; - link.href = 'data:' + contenttype + ';base64,' + bytesBase64; - document.body.appendChild(link); - link.click(); - document.body.removeChild(link); - }; -})(jQuery); diff --git a/src/Extensions/Components/BootstrapBlazor.TableExport/wwwroot/js/export.min.js b/src/Extensions/Components/BootstrapBlazor.TableExport/wwwroot/js/export.min.js deleted file mode 100644 index 87e1a3741..000000000 --- a/src/Extensions/Components/BootstrapBlazor.TableExport/wwwroot/js/export.min.js +++ /dev/null @@ -1 +0,0 @@ -(function(n){n.generatefile=function(n,t,i){var r=document.createElement("a");r.download=n;r.href="data:"+i+";base64,"+t;document.body.appendChild(r);r.click();document.body.removeChild(r)}})(jQuery); \ No newline at end of file -- Gitee From 6e7bd55923627da0e639de9e14c9cc52c2ee7e8e Mon Sep 17 00:00:00 2001 From: Argo-Asicotech Date: Wed, 30 Nov 2022 00:21:59 +0800 Subject: [PATCH 2/4] =?UTF-8?q?doc:=20=E6=9B=B4=E6=96=B0=E4=BE=9D=E8=B5=96?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor.Shared/BootstrapBlazor.Shared.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BootstrapBlazor.Shared/BootstrapBlazor.Shared.csproj b/src/BootstrapBlazor.Shared/BootstrapBlazor.Shared.csproj index 6407d2bea..35a7cc673 100644 --- a/src/BootstrapBlazor.Shared/BootstrapBlazor.Shared.csproj +++ b/src/BootstrapBlazor.Shared/BootstrapBlazor.Shared.csproj @@ -27,7 +27,7 @@ - + -- Gitee From f3239d7657fdf0ca4e1dab9aad936c9e3e8ad9d4 Mon Sep 17 00:00:00 2001 From: Argo-Asicotech Date: Wed, 30 Nov 2022 08:31:31 +0800 Subject: [PATCH 3/4] =?UTF-8?q?chore:=20=E6=9B=B4=E6=96=B0=E4=BE=9D?= =?UTF-8?q?=E8=B5=96=E5=8C=85=207.0.9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BootstrapBlazor.TableExport.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Extensions/Components/BootstrapBlazor.TableExport/BootstrapBlazor.TableExport.csproj b/src/Extensions/Components/BootstrapBlazor.TableExport/BootstrapBlazor.TableExport.csproj index ccb12768f..c139783db 100644 --- a/src/Extensions/Components/BootstrapBlazor.TableExport/BootstrapBlazor.TableExport.csproj +++ b/src/Extensions/Components/BootstrapBlazor.TableExport/BootstrapBlazor.TableExport.csproj @@ -7,7 +7,7 @@ - + -- Gitee From 7408523b4a21fa1bf36b3bcef4ab4aecd1de8e2d Mon Sep 17 00:00:00 2001 From: Argo-Asicotech Date: Wed, 30 Nov 2022 08:31:50 +0800 Subject: [PATCH 4/4] =?UTF-8?q?refactor:=20=E6=A0=B9=E6=8D=AE=E6=9C=80?= =?UTF-8?q?=E6=96=B0=E6=8E=A5=E5=8F=A3=E6=9B=B4=E6=96=B0=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/BootstrapBlazor.TableExport/ExcelExport.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Extensions/Components/BootstrapBlazor.TableExport/ExcelExport.cs b/src/Extensions/Components/BootstrapBlazor.TableExport/ExcelExport.cs index 80ee58d2a..82e8fd5bf 100644 --- a/src/Extensions/Components/BootstrapBlazor.TableExport/ExcelExport.cs +++ b/src/Extensions/Components/BootstrapBlazor.TableExport/ExcelExport.cs @@ -2,7 +2,6 @@ // 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 Microsoft.JSInterop; using OfficeOpenXml; using System.Globalization; @@ -28,7 +27,7 @@ internal class ExcelExport : ITableExcelExport /// 导出 Excel 方法 /// /// - public async Task ExportAsync(IEnumerable items, IEnumerable cols, IJSRuntime jsRuntime) where TItem : class + public async Task ExportAsync(IEnumerable items, IEnumerable cols) where TItem : class { using var excelPackage = new ExcelPackage(); var worksheet = excelPackage.Workbook.Worksheets.Add("sheet1"); @@ -39,7 +38,10 @@ internal class ExcelExport : ITableExcelExport var x = 1; foreach (var pi in item.GetType().GetProperties()) { - if (!cols.Any(col => col.GetFieldName() == pi.Name)) continue; + if (!cols.Any(col => col.GetFieldName() == pi.Name)) + { + continue; + } if (y == 1) { -- Gitee