Skip to content

Commit f0fe803

Browse files
RaZer0kEulises Vargas
and
Eulises Vargas
authored
Formula attribute added to support in rows with dto or dynamic attrib… (#679)
* Formula attribute added to support in rows with dto or dynamic attributes * Change Formula attribute to ColumnType (Value, Formula) --------- Co-authored-by: Eulises Vargas <eulises.vargas@iconstruye.onmicrosoft.com>
1 parent 436aaa8 commit f0fe803

File tree

6 files changed

+19
-8
lines changed

6 files changed

+19
-8
lines changed

src/MiniExcel/Attributes/ExcelColumnAttribute.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public class ExcelColumnAttribute : Attribute
2121

2222
public bool Ignore { get; set; }
2323

24+
public ColumnType Type { get; set; } = ColumnType.Value;
25+
2426
public int Index
2527
{
2628
get => _index;
@@ -50,6 +52,7 @@ private void Init(int index, string columnName = null)
5052
}
5153
}
5254

55+
public enum ColumnType { Value, Formula }
5356

5457
public class DynamicExcelColumn : ExcelColumnAttribute
5558
{

src/MiniExcel/OpenXml/Constants/WorksheetXml.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Globalization;
2+
using MiniExcelLibs.Attributes;
23

34
namespace MiniExcelLibs.OpenXml.Constants
45
{
@@ -52,14 +53,13 @@ internal static string Column(int? colIndex, double? columnWidth)
5253
internal static string EmptyCell(string cellReference, string styleIndex)
5354
=> $"<x:c r=\"{cellReference}\" s=\"{styleIndex}\"></x:c>";
5455
//t check avoid format error ![image](https://user-images.githubusercontent.com/12729184/118770190-9eee3480-b8b3-11eb-9f5a-87a439f5e320.png)
55-
internal static string Cell(string cellReference, string cellType, string styleIndex, string cellValue, bool preserveSpace = false)
56-
=> $"<x:c r=\"{cellReference}\"{(cellType == null ? string.Empty : $" t=\"{cellType}\"")} s=\"{styleIndex}\"{(preserveSpace ? " xml:space=\"preserve\"" : string.Empty)}><x:v>{cellValue}</x:v></x:c>";
56+
internal static string Cell(string cellReference, string cellType, string styleIndex, string cellValue, bool preserveSpace = false, ColumnType columnType = ColumnType.Value)
57+
=> $"<x:c r=\"{cellReference}\"{(cellType == null ? string.Empty : $" t=\"{cellType}\"")} s=\"{styleIndex}\"{(preserveSpace ? " xml:space=\"preserve\"" : string.Empty)}><x:{(columnType == ColumnType.Formula ? "f" : "v")}>{cellValue}</x:{(columnType == ColumnType.Formula ? "f" : "v")}></x:c>";
5758

5859
internal static string Autofilter(string dimensionRef)
5960
=> $"<x:autoFilter ref=\"{dimensionRef}\" />";
6061

6162
internal static string Drawing(int sheetIndex)
6263
=> $"<x:drawing r:id=\"drawing{sheetIndex}\" />";
63-
6464
}
6565
}

src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.Async.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using MiniExcelLibs.OpenXml.Constants;
1+
using MiniExcelLibs.Attributes;
2+
using MiniExcelLibs.OpenXml.Constants;
23
using MiniExcelLibs.Utils;
34
using MiniExcelLibs.Zip;
45
using System;
@@ -459,10 +460,11 @@ private async Task WriteCellAsync(MiniExcelAsyncStreamWriter writer, int rowInde
459460
var styleIndex = tuple.Item1;
460461
var dataType = tuple.Item2;
461462
var cellValue = tuple.Item3;
463+
var columnType = p.ExcelColumnType;
462464

463465
/*Prefix and suffix blank space will lost after SaveAs #294*/
464466
var preserveSpace = cellValue != null && (cellValue.StartsWith(" ", StringComparison.Ordinal) || cellValue.EndsWith(" ", StringComparison.Ordinal));
465-
await writer.WriteAsync(WorksheetXml.Cell(columnReference, dataType, styleIndex, cellValue, preserveSpace: preserveSpace));
467+
await writer.WriteAsync(WorksheetXml.Cell(columnReference, dataType, styleIndex, cellValue, preserveSpace: preserveSpace, columnType: columnType));
466468
}
467469

468470
private async Task GenerateEndXmlAsync(CancellationToken cancellationToken)

src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.DefaultOpenXml.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ private ExcelColumnInfo GetColumnInfosFromDynamicConfiguration(string columnName
103103

104104
prop.Nullable = true;
105105
prop.ExcelIgnore = dynamicColumn.Ignore;
106+
prop.ExcelColumnType = dynamicColumn.Type;
106107
prop.ExcelColumnIndex = dynamicColumn.Index;
107108
prop.ExcelColumnWidth = dynamicColumn.Width;
108109
//prop.ExcludeNullableType = item2[key]?.GetType();

src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using MiniExcelLibs.OpenXml.Constants;
1+
using MiniExcelLibs.Attributes;
2+
using MiniExcelLibs.OpenXml.Constants;
23
using MiniExcelLibs.OpenXml.Models;
34
using MiniExcelLibs.Utils;
45
using MiniExcelLibs.Zip;
@@ -579,10 +580,11 @@ private void WriteCell(MiniExcelStreamWriter writer, int rowIndex, int cellIndex
579580
var styleIndex = tuple.Item1; // https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.cell?view=openxml-3.0.1
580581
var dataType = tuple.Item2; // https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.cellvalues?view=openxml-3.0.1
581582
var cellValue = tuple.Item3;
583+
var columnType = columnInfo?.ExcelColumnType ?? ColumnType.Value;
582584

583585
/*Prefix and suffix blank space will lost after SaveAs #294*/
584586
var preserveSpace = cellValue != null && (cellValue.StartsWith(" ", StringComparison.Ordinal) || cellValue.EndsWith(" ", StringComparison.Ordinal));
585-
writer.Write(WorksheetXml.Cell(columnReference, dataType, styleIndex, cellValue, preserveSpace: preserveSpace));
587+
writer.Write(WorksheetXml.Cell(columnReference, dataType, styleIndex, cellValue, preserveSpace: preserveSpace, columnType: columnType));
586588
}
587589

588590
private static void WriteCell(MiniExcelStreamWriter writer, string cellReference, string columnName)

src/MiniExcel/Utils/CustomPropertyHelper.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ internal class ExcelColumnInfo
2424
public string ExcelIndexName { get; internal set; }
2525
public bool ExcelIgnore { get; internal set; }
2626
public int ExcelFormatId { get; internal set; }
27+
public ColumnType ExcelColumnType { get; internal set; }
2728
}
2829

2930
internal class ExcellSheetInfo
@@ -206,7 +207,8 @@ private static IEnumerable<ExcelColumnInfo> ConvertToExcelCustomPropertyInfo(Pro
206207
ExcelIndexName = p.GetAttribute<ExcelColumnIndexAttribute>()?.ExcelXName ?? excelColumn?.IndexName,
207208
ExcelColumnWidth = p.GetAttribute<ExcelColumnWidthAttribute>()?.ExcelColumnWidth ?? excelColumn?.Width,
208209
ExcelFormat = excelFormat ?? excelColumn?.Format,
209-
ExcelFormatId = excelColumn?.FormatId ?? -1
210+
ExcelFormatId = excelColumn?.FormatId ?? -1,
211+
ExcelColumnType = excelColumn?.Type ?? ColumnType.Value
210212
};
211213
}).Where(_ => _ != null);
212214
}
@@ -307,6 +309,7 @@ internal static void SetDictionaryColumnInfo(List<ExcelColumnInfo> _props, objec
307309
p.ExcelColumnName = dynamicColumn.Name;
308310
isIgnore = dynamicColumn.Ignore;
309311
p.ExcelColumnWidth = dynamicColumn.Width;
312+
p.ExcelColumnType = dynamicColumn.Type;
310313
}
311314
}
312315
if (!isIgnore)

0 commit comments

Comments
 (0)