Skip to content

Commit 12a9c1d

Browse files
committed
Handle CustomFormatter logic;
1 parent 591310c commit 12a9c1d

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.Async.cs

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,25 @@ private async Task GenerateSheetByIDataReaderAsync(MiniExcelAsyncStreamWriter wr
117117
for (var i = 0; i < reader.FieldCount; i++)
118118
{
119119
var columnName = reader.GetName(i);
120-
var prop = GetColumnInfosFromDynamicConfiguration(columnName);
121-
props.Add(prop);
120+
121+
if (!_configuration.DynamicColumnFirst)
122+
{
123+
var prop = GetColumnInfosFromDynamicConfiguration(columnName);
124+
props.Add(prop);
125+
continue;
126+
}
127+
128+
if (_configuration
129+
.DynamicColumns
130+
.Any(a => string.Equals(
131+
a.Key,
132+
columnName,
133+
StringComparison.OrdinalIgnoreCase)))
134+
135+
{
136+
var prop = GetColumnInfosFromDynamicConfiguration(columnName);
137+
props.Add(prop);
138+
}
122139
}
123140
maxColumnIndex = props.Count;
124141

@@ -149,7 +166,18 @@ private async Task GenerateSheetByIDataReaderAsync(MiniExcelAsyncStreamWriter wr
149166
var xIndex = 1;
150167
for (int i = 0; i < fieldCount; i++)
151168
{
152-
var cellValue = reader.GetValue(i);
169+
object cellValue;
170+
171+
if (_configuration.DynamicColumnFirst)
172+
{
173+
var columnIndex = reader.GetOrdinal(props[i].Key.ToString());
174+
cellValue = reader.GetValue(columnIndex);
175+
}
176+
else
177+
{
178+
cellValue = reader.GetValue(i);
179+
}
180+
153181
await WriteCellAsync(writer, yIndex, xIndex, cellValue, props[i], widths);
154182
xIndex++;
155183
}
@@ -534,7 +562,21 @@ private async Task WriteCellAsync(MiniExcelAsyncStreamWriter writer, int rowInde
534562
var columnType = p.ExcelColumnType;
535563

536564
/*Prefix and suffix blank space will lost after SaveAs #294*/
537-
var preserveSpace = cellValue != null && (cellValue.StartsWith(" ", StringComparison.Ordinal) || cellValue.EndsWith(" ", StringComparison.Ordinal));
565+
var preserveSpace = cellValue != null && (cellValue.StartsWith(" ", StringComparison.Ordinal) ||
566+
cellValue.EndsWith(" ", StringComparison.Ordinal));
567+
568+
if (p.CustomFormatter != null)
569+
{
570+
try
571+
{
572+
cellValue = p.CustomFormatter(cellValue);
573+
}
574+
catch (Exception e)
575+
{
576+
//ignored
577+
}
578+
}
579+
538580
await writer.WriteAsync(WorksheetXml.Cell(columnReference, dataType, styleIndex, cellValue, preserveSpace: preserveSpace, columnType: columnType));
539581
widthCollection?.AdjustWidth(cellIndex, cellValue);
540582
}

src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,19 @@ private void WriteCell(MiniExcelStreamWriter writer, int rowIndex, int cellIndex
570570
var styleIndex = tuple.Item1; // https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.cell?view=openxml-3.0.1
571571
var dataType = tuple.Item2; // https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.cellvalues?view=openxml-3.0.1
572572
var cellValue = tuple.Item3;
573+
574+
if (columnInfo?.CustomFormatter != null)
575+
{
576+
try
577+
{
578+
cellValue = columnInfo.CustomFormatter(cellValue);
579+
}
580+
catch (Exception e)
581+
{
582+
//ignored
583+
}
584+
}
585+
573586
var columnType = columnInfo?.ExcelColumnType ?? ColumnType.Value;
574587

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

0 commit comments

Comments
 (0)