Skip to content

Commit

Permalink
feat(DynamicExcelColumn): make the CustomFormatter property more powe…
Browse files Browse the repository at this point in the history
…rful (#715)

* feat(DynamicExcelColumn): make the `CustomFormatter` property more powerful

* support `IEnumerable` data
  • Loading branch information
izanhzh authored Jan 23, 2025
1 parent 6e37d20 commit 2e7219c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/MiniExcel/Attributes/ExcelColumnAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class DynamicExcelColumn : ExcelColumnAttribute
{
public string Key { get; set; }

public Func<object, string> CustomFormatter { get; set; }
public Func<object, object> CustomFormatter { get; set; }

public DynamicExcelColumn(string key)
{
Expand Down
24 changes: 12 additions & 12 deletions src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,29 +334,29 @@ private async Task WriteCellAsync(MiniExcelAsyncStreamWriter writer, int rowInde
return;
}

var tuple = GetCellValue(rowIndex, cellIndex, value, p, valueIsNull);

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));

if (p.CustomFormatter != null)
{
try
{
cellValue = p.CustomFormatter(cellValue);
value = p.CustomFormatter(value);
}
catch
{
//ignored
}
}

var tuple = GetCellValue(rowIndex, cellIndex, value, p, valueIsNull);

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, GetCellXfId(styleIndex), cellValue, preserveSpace: preserveSpace, columnType: columnType));
widthCollection?.AdjustWidth(cellIndex, cellValue);
}
Expand Down
14 changes: 7 additions & 7 deletions src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,24 +334,24 @@ private void WriteCell(MiniExcelStreamWriter writer, int rowIndex, int cellIndex
return;
}

var tuple = GetCellValue(rowIndex, cellIndex, value, columnInfo, valueIsNull);

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;

if (columnInfo?.CustomFormatter != null)
{
try
{
cellValue = columnInfo.CustomFormatter(cellValue);
value = columnInfo.CustomFormatter(value);
}
catch
{
//ignored
}
}

var tuple = GetCellValue(rowIndex, cellIndex, value, columnInfo, valueIsNull);

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*/
Expand Down
14 changes: 6 additions & 8 deletions src/MiniExcel/Utils/CustomPropertyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal class ExcelColumnInfo
public bool ExcelIgnore { get; internal set; }
public int ExcelFormatId { get; internal set; }
public ColumnType ExcelColumnType { get; internal set; }
public Func<object, string> CustomFormatter { get; set; }
public Func<object, object> CustomFormatter { get; set; }
}

internal class ExcellSheetInfo
Expand Down Expand Up @@ -183,12 +183,9 @@ private static IEnumerable<ExcelColumnInfo> ConvertToExcelCustomPropertyInfo(Pro
var excludeNullableType = gt ?? p.PropertyType;
var excelFormat = p.GetAttribute<ExcelFormatAttribute>()?.Format;
var excelColumn = p.GetAttribute<ExcelColumnAttribute>();
if (configuration.DynamicColumns != null && configuration.DynamicColumns.Length > 0)
{
var dynamicColumn = configuration.DynamicColumns.SingleOrDefault(_ => _.Key == p.Name);
if (dynamicColumn != null)
excelColumn = dynamicColumn;
}
var dynamicColumn = configuration?.DynamicColumns?.SingleOrDefault(_ => _.Key == p.Name);
if (dynamicColumn != null)
excelColumn = dynamicColumn;

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

0 comments on commit 2e7219c

Please sign in to comment.