From 892600b33c627899b67c4ab08568a0c714bb2b7a Mon Sep 17 00:00:00 2001 From: Eulises Vargas Date: Fri, 11 Oct 2024 08:37:44 -0300 Subject: [PATCH 1/2] Formula attribute added to support in rows with dto or dynamic attributes --- src/MiniExcel/Attributes/ExcelColumnAttribute.cs | 2 ++ src/MiniExcel/OpenXml/Constants/WorksheetXml.cs | 4 ++-- src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.Async.cs | 2 +- .../OpenXml/ExcelOpenXmlSheetWriter.DefaultOpenXml.cs | 1 + src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs | 3 ++- src/MiniExcel/Utils/CustomPropertyHelper.cs | 5 ++++- 6 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/MiniExcel/Attributes/ExcelColumnAttribute.cs b/src/MiniExcel/Attributes/ExcelColumnAttribute.cs index 965f0579..537f5f52 100644 --- a/src/MiniExcel/Attributes/ExcelColumnAttribute.cs +++ b/src/MiniExcel/Attributes/ExcelColumnAttribute.cs @@ -21,6 +21,8 @@ public class ExcelColumnAttribute : Attribute public bool Ignore { get; set; } + public bool Formula { get; set; } = false; + public int Index { get => _index; diff --git a/src/MiniExcel/OpenXml/Constants/WorksheetXml.cs b/src/MiniExcel/OpenXml/Constants/WorksheetXml.cs index 0fb12908..f780f5f2 100644 --- a/src/MiniExcel/OpenXml/Constants/WorksheetXml.cs +++ b/src/MiniExcel/OpenXml/Constants/WorksheetXml.cs @@ -52,8 +52,8 @@ internal static string Column(int? colIndex, double? columnWidth) internal static string EmptyCell(string cellReference, string styleIndex) => $""; //t check avoid format error ![image](https://user-images.githubusercontent.com/12729184/118770190-9eee3480-b8b3-11eb-9f5a-87a439f5e320.png) - internal static string Cell(string cellReference, string cellType, string styleIndex, string cellValue, bool preserveSpace = false) - => $"{cellValue}"; + internal static string Cell(string cellReference, string cellType, string styleIndex, string cellValue, bool preserveSpace = false, bool formula = false) + => $"{cellValue}"; internal static string Autofilter(string dimensionRef) => $""; diff --git a/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.Async.cs b/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.Async.cs index 87e08b53..137e7406 100644 --- a/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.Async.cs +++ b/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.Async.cs @@ -462,7 +462,7 @@ private async Task WriteCellAsync(MiniExcelAsyncStreamWriter writer, int rowInde /*Prefix and suffix blank space will lost after SaveAs #294*/ var preserveSpace = cellValue != null && (cellValue.StartsWith(" ", StringComparison.Ordinal) || cellValue.EndsWith(" ", StringComparison.Ordinal)); - await writer.WriteAsync(WorksheetXml.Cell(columnReference, dataType, styleIndex, cellValue, preserveSpace: preserveSpace)); + await writer.WriteAsync(WorksheetXml.Cell(columnReference, dataType, styleIndex, cellValue, preserveSpace: preserveSpace, formula: p.ExcelFormula)); } private async Task GenerateEndXmlAsync(CancellationToken cancellationToken) diff --git a/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.DefaultOpenXml.cs b/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.DefaultOpenXml.cs index 007e9b1c..911c17c8 100644 --- a/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.DefaultOpenXml.cs +++ b/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.DefaultOpenXml.cs @@ -103,6 +103,7 @@ private ExcelColumnInfo GetColumnInfosFromDynamicConfiguration(string columnName prop.Nullable = true; prop.ExcelIgnore = dynamicColumn.Ignore; + prop.ExcelFormula = dynamicColumn.Formula; prop.ExcelColumnIndex = dynamicColumn.Index; prop.ExcelColumnWidth = dynamicColumn.Width; //prop.ExcludeNullableType = item2[key]?.GetType(); diff --git a/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs b/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs index 5299a2b6..6a608b11 100644 --- a/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs +++ b/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs @@ -582,7 +582,8 @@ private void WriteCell(MiniExcelStreamWriter writer, int rowIndex, int cellIndex /*Prefix and suffix blank space will lost after SaveAs #294*/ var preserveSpace = cellValue != null && (cellValue.StartsWith(" ", StringComparison.Ordinal) || cellValue.EndsWith(" ", StringComparison.Ordinal)); - writer.Write(WorksheetXml.Cell(columnReference, dataType, styleIndex, cellValue, preserveSpace: preserveSpace)); + var isFormula = columnInfo?.ExcelFormula ?? false; + writer.Write(WorksheetXml.Cell(columnReference, dataType, styleIndex, cellValue, preserveSpace: preserveSpace, formula: isFormula)); } private static void WriteCell(MiniExcelStreamWriter writer, string cellReference, string columnName) diff --git a/src/MiniExcel/Utils/CustomPropertyHelper.cs b/src/MiniExcel/Utils/CustomPropertyHelper.cs index fa7f164f..f6f1ef8c 100644 --- a/src/MiniExcel/Utils/CustomPropertyHelper.cs +++ b/src/MiniExcel/Utils/CustomPropertyHelper.cs @@ -24,6 +24,7 @@ internal class ExcelColumnInfo public string ExcelIndexName { get; internal set; } public bool ExcelIgnore { get; internal set; } public int ExcelFormatId { get; internal set; } + public bool ExcelFormula { get; internal set; } } internal class ExcellSheetInfo @@ -206,7 +207,8 @@ private static IEnumerable ConvertToExcelCustomPropertyInfo(Pro ExcelIndexName = p.GetAttribute()?.ExcelXName ?? excelColumn?.IndexName, ExcelColumnWidth = p.GetAttribute()?.ExcelColumnWidth ?? excelColumn?.Width, ExcelFormat = excelFormat ?? excelColumn?.Format, - ExcelFormatId = excelColumn?.FormatId ?? -1 + ExcelFormatId = excelColumn?.FormatId ?? -1, + ExcelFormula = excelColumn?.Formula ?? false }; }).Where(_ => _ != null); } @@ -307,6 +309,7 @@ internal static void SetDictionaryColumnInfo(List _props, objec p.ExcelColumnName = dynamicColumn.Name; isIgnore = dynamicColumn.Ignore; p.ExcelColumnWidth = dynamicColumn.Width; + p.ExcelFormula = dynamicColumn.Formula; } } if (!isIgnore) From c707ed49eeccd6b8d8a35427322f54344cb95866 Mon Sep 17 00:00:00 2001 From: Eulises Vargas Date: Tue, 15 Oct 2024 10:43:44 -0300 Subject: [PATCH 2/2] Change Formula attribute to ColumnType (Value, Formula) --- src/MiniExcel/Attributes/ExcelColumnAttribute.cs | 3 ++- src/MiniExcel/OpenXml/Constants/WorksheetXml.cs | 6 +++--- src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.Async.cs | 6 ++++-- .../OpenXml/ExcelOpenXmlSheetWriter.DefaultOpenXml.cs | 2 +- src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs | 7 ++++--- src/MiniExcel/Utils/CustomPropertyHelper.cs | 6 +++--- 6 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/MiniExcel/Attributes/ExcelColumnAttribute.cs b/src/MiniExcel/Attributes/ExcelColumnAttribute.cs index 537f5f52..9cae6d9b 100644 --- a/src/MiniExcel/Attributes/ExcelColumnAttribute.cs +++ b/src/MiniExcel/Attributes/ExcelColumnAttribute.cs @@ -21,7 +21,7 @@ public class ExcelColumnAttribute : Attribute public bool Ignore { get; set; } - public bool Formula { get; set; } = false; + public ColumnType Type { get; set; } = ColumnType.Value; public int Index { @@ -52,6 +52,7 @@ private void Init(int index, string columnName = null) } } + public enum ColumnType { Value, Formula } public class DynamicExcelColumn : ExcelColumnAttribute { diff --git a/src/MiniExcel/OpenXml/Constants/WorksheetXml.cs b/src/MiniExcel/OpenXml/Constants/WorksheetXml.cs index f780f5f2..c300cbf0 100644 --- a/src/MiniExcel/OpenXml/Constants/WorksheetXml.cs +++ b/src/MiniExcel/OpenXml/Constants/WorksheetXml.cs @@ -1,4 +1,5 @@ using System.Globalization; +using MiniExcelLibs.Attributes; namespace MiniExcelLibs.OpenXml.Constants { @@ -52,14 +53,13 @@ internal static string Column(int? colIndex, double? columnWidth) internal static string EmptyCell(string cellReference, string styleIndex) => $""; //t check avoid format error ![image](https://user-images.githubusercontent.com/12729184/118770190-9eee3480-b8b3-11eb-9f5a-87a439f5e320.png) - internal static string Cell(string cellReference, string cellType, string styleIndex, string cellValue, bool preserveSpace = false, bool formula = false) - => $"{cellValue}"; + internal static string Cell(string cellReference, string cellType, string styleIndex, string cellValue, bool preserveSpace = false, ColumnType columnType = ColumnType.Value) + => $"{cellValue}"; internal static string Autofilter(string dimensionRef) => $""; internal static string Drawing(int sheetIndex) => $""; - } } diff --git a/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.Async.cs b/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.Async.cs index 137e7406..87cf3746 100644 --- a/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.Async.cs +++ b/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.Async.cs @@ -1,4 +1,5 @@ -using MiniExcelLibs.OpenXml.Constants; +using MiniExcelLibs.Attributes; +using MiniExcelLibs.OpenXml.Constants; using MiniExcelLibs.Utils; using MiniExcelLibs.Zip; using System; @@ -459,10 +460,11 @@ private async Task WriteCellAsync(MiniExcelAsyncStreamWriter writer, int rowInde var styleIndex = tuple.Item1; var dataType = tuple.Item2; var cellValue = tuple.Item3; + var columnType = p.ExcelColumnType; /*Prefix and suffix blank space will lost after SaveAs #294*/ var preserveSpace = cellValue != null && (cellValue.StartsWith(" ", StringComparison.Ordinal) || cellValue.EndsWith(" ", StringComparison.Ordinal)); - await writer.WriteAsync(WorksheetXml.Cell(columnReference, dataType, styleIndex, cellValue, preserveSpace: preserveSpace, formula: p.ExcelFormula)); + await writer.WriteAsync(WorksheetXml.Cell(columnReference, dataType, styleIndex, cellValue, preserveSpace: preserveSpace, columnType: columnType)); } private async Task GenerateEndXmlAsync(CancellationToken cancellationToken) diff --git a/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.DefaultOpenXml.cs b/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.DefaultOpenXml.cs index 911c17c8..b511c51a 100644 --- a/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.DefaultOpenXml.cs +++ b/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.DefaultOpenXml.cs @@ -103,7 +103,7 @@ private ExcelColumnInfo GetColumnInfosFromDynamicConfiguration(string columnName prop.Nullable = true; prop.ExcelIgnore = dynamicColumn.Ignore; - prop.ExcelFormula = dynamicColumn.Formula; + prop.ExcelColumnType = dynamicColumn.Type; prop.ExcelColumnIndex = dynamicColumn.Index; prop.ExcelColumnWidth = dynamicColumn.Width; //prop.ExcludeNullableType = item2[key]?.GetType(); diff --git a/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs b/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs index 6a608b11..c03abadd 100644 --- a/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs +++ b/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs @@ -1,4 +1,5 @@ -using MiniExcelLibs.OpenXml.Constants; +using MiniExcelLibs.Attributes; +using MiniExcelLibs.OpenXml.Constants; using MiniExcelLibs.OpenXml.Models; using MiniExcelLibs.Utils; using MiniExcelLibs.Zip; @@ -579,11 +580,11 @@ private void WriteCell(MiniExcelStreamWriter writer, int rowIndex, int cellIndex var styleIndex = tuple.Item1; // https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.cell?view=openxml-3.0.1 var dataType = tuple.Item2; // https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.cellvalues?view=openxml-3.0.1 var cellValue = tuple.Item3; + var columnType = columnInfo?.ExcelColumnType ?? ColumnType.Value; /*Prefix and suffix blank space will lost after SaveAs #294*/ var preserveSpace = cellValue != null && (cellValue.StartsWith(" ", StringComparison.Ordinal) || cellValue.EndsWith(" ", StringComparison.Ordinal)); - var isFormula = columnInfo?.ExcelFormula ?? false; - writer.Write(WorksheetXml.Cell(columnReference, dataType, styleIndex, cellValue, preserveSpace: preserveSpace, formula: isFormula)); + writer.Write(WorksheetXml.Cell(columnReference, dataType, styleIndex, cellValue, preserveSpace: preserveSpace, columnType: columnType)); } private static void WriteCell(MiniExcelStreamWriter writer, string cellReference, string columnName) diff --git a/src/MiniExcel/Utils/CustomPropertyHelper.cs b/src/MiniExcel/Utils/CustomPropertyHelper.cs index f6f1ef8c..0c8d458d 100644 --- a/src/MiniExcel/Utils/CustomPropertyHelper.cs +++ b/src/MiniExcel/Utils/CustomPropertyHelper.cs @@ -24,7 +24,7 @@ internal class ExcelColumnInfo public string ExcelIndexName { get; internal set; } public bool ExcelIgnore { get; internal set; } public int ExcelFormatId { get; internal set; } - public bool ExcelFormula { get; internal set; } + public ColumnType ExcelColumnType { get; internal set; } } internal class ExcellSheetInfo @@ -208,7 +208,7 @@ private static IEnumerable ConvertToExcelCustomPropertyInfo(Pro ExcelColumnWidth = p.GetAttribute()?.ExcelColumnWidth ?? excelColumn?.Width, ExcelFormat = excelFormat ?? excelColumn?.Format, ExcelFormatId = excelColumn?.FormatId ?? -1, - ExcelFormula = excelColumn?.Formula ?? false + ExcelColumnType = excelColumn?.Type ?? ColumnType.Value }; }).Where(_ => _ != null); } @@ -309,7 +309,7 @@ internal static void SetDictionaryColumnInfo(List _props, objec p.ExcelColumnName = dynamicColumn.Name; isIgnore = dynamicColumn.Ignore; p.ExcelColumnWidth = dynamicColumn.Width; - p.ExcelFormula = dynamicColumn.Formula; + p.ExcelColumnType = dynamicColumn.Type; } } if (!isIgnore)