diff --git a/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs b/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs index bdda2bd6237fb52ba04751e7ea4bdd58afef70b3..c72b271add3e4f0e064ac5d68b2f00df6a857814 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 37cf19c0d652664425fd48e7d6e37e3dbae92700..6e75b52592b38a58aa8dc3af2b255844f5ef272d 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 74f999f1950fbe07815bea60c57b0ebf851c3def..1e68dda2a1e4a114eccc043bc8b26d6706ee045c 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 554092f7eb2fc921b0bd07d09ad3516eea1cd47a..6a3a4b2ab4fd74df8e1aa9c4ab79fc60eb5237bb 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