Skip to content

Commit 2e7219c

Browse files
authored
feat(DynamicExcelColumn): make the CustomFormatter property more powerful (#715)
* feat(DynamicExcelColumn): make the `CustomFormatter` property more powerful * support `IEnumerable` data
1 parent 6e37d20 commit 2e7219c

File tree

4 files changed

+26
-28
lines changed

4 files changed

+26
-28
lines changed

src/MiniExcel/Attributes/ExcelColumnAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public class DynamicExcelColumn : ExcelColumnAttribute
5858
{
5959
public string Key { get; set; }
6060

61-
public Func<object, string> CustomFormatter { get; set; }
61+
public Func<object, object> CustomFormatter { get; set; }
6262

6363
public DynamicExcelColumn(string key)
6464
{

src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.Async.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -334,29 +334,29 @@ private async Task WriteCellAsync(MiniExcelAsyncStreamWriter writer, int rowInde
334334
return;
335335
}
336336

337-
var tuple = GetCellValue(rowIndex, cellIndex, value, p, valueIsNull);
338-
339-
var styleIndex = tuple.Item1;
340-
var dataType = tuple.Item2;
341-
var cellValue = tuple.Item3;
342-
var columnType = p.ExcelColumnType;
343-
344-
/*Prefix and suffix blank space will lost after SaveAs #294*/
345-
var preserveSpace = cellValue != null && (cellValue.StartsWith(" ", StringComparison.Ordinal) ||
346-
cellValue.EndsWith(" ", StringComparison.Ordinal));
347-
348337
if (p.CustomFormatter != null)
349338
{
350339
try
351340
{
352-
cellValue = p.CustomFormatter(cellValue);
341+
value = p.CustomFormatter(value);
353342
}
354343
catch
355344
{
356345
//ignored
357346
}
358347
}
359348

349+
var tuple = GetCellValue(rowIndex, cellIndex, value, p, valueIsNull);
350+
351+
var styleIndex = tuple.Item1;
352+
var dataType = tuple.Item2;
353+
var cellValue = tuple.Item3;
354+
var columnType = p.ExcelColumnType;
355+
356+
/*Prefix and suffix blank space will lost after SaveAs #294*/
357+
var preserveSpace = cellValue != null && (cellValue.StartsWith(" ", StringComparison.Ordinal) ||
358+
cellValue.EndsWith(" ", StringComparison.Ordinal));
359+
360360
await writer.WriteAsync(WorksheetXml.Cell(columnReference, dataType, GetCellXfId(styleIndex), cellValue, preserveSpace: preserveSpace, columnType: columnType));
361361
widthCollection?.AdjustWidth(cellIndex, cellValue);
362362
}

src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -334,24 +334,24 @@ private void WriteCell(MiniExcelStreamWriter writer, int rowIndex, int cellIndex
334334
return;
335335
}
336336

337-
var tuple = GetCellValue(rowIndex, cellIndex, value, columnInfo, valueIsNull);
338-
339-
var styleIndex = tuple.Item1; // https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.cell?view=openxml-3.0.1
340-
var dataType = tuple.Item2; // https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.cellvalues?view=openxml-3.0.1
341-
var cellValue = tuple.Item3;
342-
343337
if (columnInfo?.CustomFormatter != null)
344338
{
345339
try
346340
{
347-
cellValue = columnInfo.CustomFormatter(cellValue);
341+
value = columnInfo.CustomFormatter(value);
348342
}
349343
catch
350344
{
351345
//ignored
352346
}
353347
}
354348

349+
var tuple = GetCellValue(rowIndex, cellIndex, value, columnInfo, valueIsNull);
350+
351+
var styleIndex = tuple.Item1; // https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.cell?view=openxml-3.0.1
352+
var dataType = tuple.Item2; // https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.cellvalues?view=openxml-3.0.1
353+
var cellValue = tuple.Item3;
354+
355355
var columnType = columnInfo?.ExcelColumnType ?? ColumnType.Value;
356356

357357
/*Prefix and suffix blank space will lost after SaveAs #294*/

src/MiniExcel/Utils/CustomPropertyHelper.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ internal class ExcelColumnInfo
2525
public bool ExcelIgnore { get; internal set; }
2626
public int ExcelFormatId { get; internal set; }
2727
public ColumnType ExcelColumnType { get; internal set; }
28-
public Func<object, string> CustomFormatter { get; set; }
28+
public Func<object, object> CustomFormatter { get; set; }
2929
}
3030

3131
internal class ExcellSheetInfo
@@ -183,12 +183,9 @@ private static IEnumerable<ExcelColumnInfo> ConvertToExcelCustomPropertyInfo(Pro
183183
var excludeNullableType = gt ?? p.PropertyType;
184184
var excelFormat = p.GetAttribute<ExcelFormatAttribute>()?.Format;
185185
var excelColumn = p.GetAttribute<ExcelColumnAttribute>();
186-
if (configuration.DynamicColumns != null && configuration.DynamicColumns.Length > 0)
187-
{
188-
var dynamicColumn = configuration.DynamicColumns.SingleOrDefault(_ => _.Key == p.Name);
189-
if (dynamicColumn != null)
190-
excelColumn = dynamicColumn;
191-
}
186+
var dynamicColumn = configuration?.DynamicColumns?.SingleOrDefault(_ => _.Key == p.Name);
187+
if (dynamicColumn != null)
188+
excelColumn = dynamicColumn;
192189

193190
var ignore = p.GetAttributeValue((ExcelIgnoreAttribute x) => x.ExcelIgnore) || p.GetAttributeValue((ExcelColumnAttribute x) => x.Ignore) || (excelColumn != null && excelColumn.Ignore);
194191
if (ignore)
@@ -209,7 +206,8 @@ private static IEnumerable<ExcelColumnInfo> ConvertToExcelCustomPropertyInfo(Pro
209206
ExcelColumnWidth = p.GetAttribute<ExcelColumnWidthAttribute>()?.ExcelColumnWidth ?? excelColumn?.Width,
210207
ExcelFormat = excelFormat ?? excelColumn?.Format,
211208
ExcelFormatId = excelColumn?.FormatId ?? -1,
212-
ExcelColumnType = excelColumn?.Type ?? ColumnType.Value
209+
ExcelColumnType = excelColumn?.Type ?? ColumnType.Value,
210+
CustomFormatter = dynamicColumn?.CustomFormatter
213211
};
214212
}).Where(_ => _ != null);
215213
}

0 commit comments

Comments
 (0)