From 6bdde577d16afb0457040a7a1057446087618428 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=86=B3=E6=98=8E=E5=AD=90?= Date: Wed, 6 Apr 2022 10:39:25 +0800 Subject: [PATCH] =?UTF-8?q?fix(0000):=20=E5=8C=BF=E5=90=8Dlist=EF=BC=8C?= =?UTF-8?q?=E4=B8=8D=E6=94=AF=E6=8C=81=E5=8A=A8=E6=80=81=E5=88=97=E5=90=8D?= =?UTF-8?q?=E3=80=81=E4=B8=8D=E6=94=AF=E6=8C=81=E5=8A=A8=E6=80=81=E5=BF=BD?= =?UTF-8?q?=E7=95=A5=E5=88=97.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../OpenXml/ExcelOpenXmlSheetWriter.cs | 8 ++- src/MiniExcel/OpenXml/OpenXmlConfiguration.cs | 6 ++ src/MiniExcel/Utils/CustomPropertyHelper.cs | 2 +- tests/MiniExcelTests/MiniExcelOpenXmlTests.cs | 65 +++++++++++++++++++ 4 files changed, 79 insertions(+), 2 deletions(-) diff --git a/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs b/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs index bdda2bd..c72b271 100644 --- a/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs +++ b/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs @@ -277,7 +277,7 @@ private void CreateSheetXml(object value, string sheetPath) _zipDictionary.Add(sheetPath, new ZipPackageInfo(entry, "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml")); } - private static void SetGenericTypePropertiesMode(Type genericType, ref string mode, out int maxColumnIndex, out List props) + private void SetGenericTypePropertiesMode(Type genericType, ref string mode, out int maxColumnIndex, out List props) { mode = "Properties"; if (genericType.IsValueType) @@ -285,6 +285,12 @@ private static void SetGenericTypePropertiesMode(Type genericType, ref string mo else if (genericType == typeof(string) || genericType == typeof(DateTime) || genericType == typeof(Guid)) throw new NotImplementedException($"MiniExcel not support only {genericType.Name} generic type"); props = CustomPropertyHelper.GetSaveAsProperties(genericType); + + if (_configuration.CustomPropertyAction != null) + { + _configuration.CustomPropertyAction(props); + } + maxColumnIndex = props.Count; } diff --git a/src/MiniExcel/OpenXml/OpenXmlConfiguration.cs b/src/MiniExcel/OpenXml/OpenXmlConfiguration.cs index 37cf19c..6e75b52 100644 --- a/src/MiniExcel/OpenXml/OpenXmlConfiguration.cs +++ b/src/MiniExcel/OpenXml/OpenXmlConfiguration.cs @@ -1,4 +1,8 @@  +using MiniExcelLibs.Utils; + +using System.Collections.Generic; +using System; using System.ComponentModel; namespace MiniExcelLibs.OpenXml @@ -13,5 +17,7 @@ public class OpenXmlConfiguration : Configuration public bool IgnoreTemplateParameterMissing { get; set; } = true; public bool EnableSharedStringCache { get; set; } = true; public long SharedStringCacheSize { get; set; } = 5 * 1024 * 1024; + + public Action> CustomPropertyAction { get; set; } } } \ No newline at end of file diff --git a/src/MiniExcel/Utils/CustomPropertyHelper.cs b/src/MiniExcel/Utils/CustomPropertyHelper.cs index 74f999f..1e68dda 100644 --- a/src/MiniExcel/Utils/CustomPropertyHelper.cs +++ b/src/MiniExcel/Utils/CustomPropertyHelper.cs @@ -8,7 +8,7 @@ using System.Linq; using System.Reflection; - internal class ExcelCustomPropertyInfo + public class ExcelCustomPropertyInfo { public int? ExcelColumnIndex { get; set; } public string ExcelColumnName { get; set; } diff --git a/tests/MiniExcelTests/MiniExcelOpenXmlTests.cs b/tests/MiniExcelTests/MiniExcelOpenXmlTests.cs index 554092f..6a3a4b2 100644 --- a/tests/MiniExcelTests/MiniExcelOpenXmlTests.cs +++ b/tests/MiniExcelTests/MiniExcelOpenXmlTests.cs @@ -1197,5 +1197,70 @@ public void SharedStringNoCacheTest() output.WriteLine("elapsedMilliseconds: " + stopWatch.ElapsedMilliseconds); stopWatch.Stop(); } + + + [Fact] + public void CustomPropertyActionTest() + { + var detailList = new List() + { + new { Id = 1, Name="github", Att04 = "1-Att04", Att03 = "1-Att03", Att01 = "1-Att01", Att02 = "1-Att02"}, + }; + + + var sheets = new Dictionary + { + ["明细"] = detailList, + }; + + { + var path = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}.xlsx"); + + Action> customPropertyAction = _ => + { + //1.需要移除列 + List removeColumns = new List(); + removeColumns.Add("Att02"); + removeColumns.Add("Att04"); + + foreach (var item in removeColumns) + { + _.RemoveAll(c => c.ExcelColumnName == item); + } + //2.自定义列名 + Dictionary keyValues = new Dictionary() + { + ["Att01"] = "自定义列1", + ["Att03"] = "自定义列3" + }; + foreach (var item in _) + { + if (keyValues.ContainsKey(item.ExcelColumnName)) + { + item.ExcelColumnName = keyValues[item.ExcelColumnName]; + } + } + }; + + MiniExcel.SaveAs(path, sheets, configuration: new OpenXmlConfiguration() { CustomPropertyAction = customPropertyAction }); + + var rows = MiniExcel.Query(path, false).ToList(); + Assert.Equal("自定义列3", rows[0].C); + Assert.Equal("自定义列1", rows[0].D); + } + + + { + var path = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}.xlsx"); + + MiniExcel.SaveAs(path, sheets, configuration: new OpenXmlConfiguration() { CustomPropertyAction = null }); + + var rows = MiniExcel.Query(path, false).ToList(); + Assert.Equal("Att04", rows[0].C); + Assert.Equal("Att03", rows[0].D); + Assert.Equal("Att01", rows[0].E); + Assert.Equal("Att02", rows[0].F); + } + } } } \ No newline at end of file -- Gitee