Skip to content

Commit

Permalink
Handle CustomFormatter logic;
Browse files Browse the repository at this point in the history
  • Loading branch information
IcedMango committed Nov 26, 2024
1 parent 591310c commit 12a9c1d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
50 changes: 46 additions & 4 deletions src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,25 @@ private async Task GenerateSheetByIDataReaderAsync(MiniExcelAsyncStreamWriter wr
for (var i = 0; i < reader.FieldCount; i++)
{
var columnName = reader.GetName(i);
var prop = GetColumnInfosFromDynamicConfiguration(columnName);
props.Add(prop);

if (!_configuration.DynamicColumnFirst)
{
var prop = GetColumnInfosFromDynamicConfiguration(columnName);
props.Add(prop);
continue;
}

if (_configuration
.DynamicColumns
.Any(a => string.Equals(
a.Key,
columnName,
StringComparison.OrdinalIgnoreCase)))

{
var prop = GetColumnInfosFromDynamicConfiguration(columnName);
props.Add(prop);
}
}
maxColumnIndex = props.Count;

Expand Down Expand Up @@ -149,7 +166,18 @@ private async Task GenerateSheetByIDataReaderAsync(MiniExcelAsyncStreamWriter wr
var xIndex = 1;
for (int i = 0; i < fieldCount; i++)
{
var cellValue = reader.GetValue(i);
object cellValue;

if (_configuration.DynamicColumnFirst)
{
var columnIndex = reader.GetOrdinal(props[i].Key.ToString());
cellValue = reader.GetValue(columnIndex);
}
else
{
cellValue = reader.GetValue(i);
}

await WriteCellAsync(writer, yIndex, xIndex, cellValue, props[i], widths);
xIndex++;
}
Expand Down Expand Up @@ -534,7 +562,21 @@ private async Task WriteCellAsync(MiniExcelAsyncStreamWriter writer, int rowInde
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));
var preserveSpace = cellValue != null && (cellValue.StartsWith(" ", StringComparison.Ordinal) ||
cellValue.EndsWith(" ", StringComparison.Ordinal));

if (p.CustomFormatter != null)
{
try
{
cellValue = p.CustomFormatter(cellValue);
}
catch (Exception e)

Check warning on line 574 in src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.Async.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'e' is declared but never used

Check warning on line 574 in src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.Async.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'e' is declared but never used

Check warning on line 574 in src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.Async.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'e' is declared but never used
{
//ignored
}
}

await writer.WriteAsync(WorksheetXml.Cell(columnReference, dataType, styleIndex, cellValue, preserveSpace: preserveSpace, columnType: columnType));
widthCollection?.AdjustWidth(cellIndex, cellValue);
}
Expand Down
13 changes: 13 additions & 0 deletions src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,19 @@ private void WriteCell(MiniExcelStreamWriter writer, int rowIndex, int cellIndex
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);
}
catch (Exception e)

Check warning on line 580 in src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'e' is declared but never used

Check warning on line 580 in src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'e' is declared but never used

Check warning on line 580 in src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs

View workflow job for this annotation

GitHub Actions / build

The variable 'e' is declared but never used
{
//ignored
}
}

var columnType = columnInfo?.ExcelColumnType ?? ColumnType.Value;

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

0 comments on commit 12a9c1d

Please sign in to comment.