Skip to content

Commit 892600b

Browse files
author
Eulises Vargas
committed
Formula attribute added to support in rows with dto or dynamic attributes
1 parent 436aaa8 commit 892600b

File tree

6 files changed

+12
-5
lines changed

6 files changed

+12
-5
lines changed

src/MiniExcel/Attributes/ExcelColumnAttribute.cs

Lines changed: 2 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 bool Formula { get; set; } = false;
25+
2426
public int Index
2527
{
2628
get => _index;

src/MiniExcel/OpenXml/Constants/WorksheetXml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ internal static string Column(int? colIndex, double? columnWidth)
5252
internal static string EmptyCell(string cellReference, string styleIndex)
5353
=> $"<x:c r=\"{cellReference}\" s=\"{styleIndex}\"></x:c>";
5454
//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>";
55+
internal static string Cell(string cellReference, string cellType, string styleIndex, string cellValue, bool preserveSpace = false, bool formula = false)
56+
=> $"<x:c r=\"{cellReference}\"{(cellType == null ? string.Empty : $" t=\"{cellType}\"")} s=\"{styleIndex}\"{(preserveSpace ? " xml:space=\"preserve\"" : string.Empty)}><x:{(formula ? "f" : "v")}>{cellValue}</x:{(formula ? "f" : "v")}></x:c>";
5757

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

src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.Async.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ private async Task WriteCellAsync(MiniExcelAsyncStreamWriter writer, int rowInde
462462

463463
/*Prefix and suffix blank space will lost after SaveAs #294*/
464464
var preserveSpace = cellValue != null && (cellValue.StartsWith(" ", StringComparison.Ordinal) || cellValue.EndsWith(" ", StringComparison.Ordinal));
465-
await writer.WriteAsync(WorksheetXml.Cell(columnReference, dataType, styleIndex, cellValue, preserveSpace: preserveSpace));
465+
await writer.WriteAsync(WorksheetXml.Cell(columnReference, dataType, styleIndex, cellValue, preserveSpace: preserveSpace, formula: p.ExcelFormula));
466466
}
467467

468468
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.ExcelFormula = dynamicColumn.Formula;
106107
prop.ExcelColumnIndex = dynamicColumn.Index;
107108
prop.ExcelColumnWidth = dynamicColumn.Width;
108109
//prop.ExcludeNullableType = item2[key]?.GetType();

src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,8 @@ private void WriteCell(MiniExcelStreamWriter writer, int rowIndex, int cellIndex
582582

583583
/*Prefix and suffix blank space will lost after SaveAs #294*/
584584
var preserveSpace = cellValue != null && (cellValue.StartsWith(" ", StringComparison.Ordinal) || cellValue.EndsWith(" ", StringComparison.Ordinal));
585-
writer.Write(WorksheetXml.Cell(columnReference, dataType, styleIndex, cellValue, preserveSpace: preserveSpace));
585+
var isFormula = columnInfo?.ExcelFormula ?? false;
586+
writer.Write(WorksheetXml.Cell(columnReference, dataType, styleIndex, cellValue, preserveSpace: preserveSpace, formula: isFormula));
586587
}
587588

588589
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 bool ExcelFormula { 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+
ExcelFormula = excelColumn?.Formula ?? false
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.ExcelFormula = dynamicColumn.Formula;
310313
}
311314
}
312315
if (!isIgnore)

0 commit comments

Comments
 (0)