Closed BaatenHannes closed 2 weeks ago
Hi @BaatenHannes, thanks for the PR. :)
I think to prevent repeating the Freeze pane code for the Async versions we can do the following:
add the following methods to \src\MiniExcel\OpenXml\ExcelOpenXmlSheetWriter.DefaultOpenXml.cs
:
private string GetSheetViews()
{
// exit early if no style to write
if (_configuration.FreezeRowCount <= 0 && _configuration.FreezeColumnCount <= 0)
{
return string.Empty;
}
var sb = new StringBuilder();
// start sheetViews
sb.Append(WorksheetXml.StartSheetViews);
sb.Append(WorksheetXml.StartSheetView());
// Write panes
sb.Append(GetPanes());
// end sheetViews
sb.Append(WorksheetXml.EndSheetView);
sb.Append(WorksheetXml.EndSheetViews);
return sb.ToString();
}
private string GetPanes()
{
var sb = new StringBuilder();
string activePane;
if (_configuration.FreezeColumnCount > 0 && _configuration.FreezeRowCount > 0)
{
activePane = "bottomRight";
}
else if (_configuration.FreezeColumnCount > 0)
{
activePane = "topRight";
}
else
{
activePane = "bottomLeft";
}
sb.Append(
WorksheetXml.StartPane(
xSplit: _configuration.FreezeColumnCount > 0 ? _configuration.FreezeColumnCount : (int?)null,
ySplit: _configuration.FreezeRowCount > 0 ? _configuration.FreezeRowCount : (int?)null,
topLeftCell: ExcelOpenXmlUtils.ConvertXyToCell(
_configuration.FreezeColumnCount + 1,
_configuration.FreezeRowCount + 1
),
activePane: activePane,
state: "frozen"
)
);
// write pane selections
if (_configuration.FreezeColumnCount > 0 && _configuration.FreezeRowCount > 0)
{
// freeze row and column
/*
<selection pane="topRight" activeCell="B1" sqref="B1"/>
<selection pane="bottomLeft" activeCell="A3" sqref="A3"/>
<selection pane="bottomRight" activeCell="B3" sqref="B3"/>
*/
var cellTR = ExcelOpenXmlUtils.ConvertXyToCell(_configuration.FreezeColumnCount + 1, 1);
sb.Append(WorksheetXml.PaneSelection("topRight", cellTR, cellTR));
var cellBL = ExcelOpenXmlUtils.ConvertXyToCell(1, _configuration.FreezeRowCount + 1);
sb.Append(WorksheetXml.PaneSelection("bottomLeft", cellBL, cellBL));
var cellBR = ExcelOpenXmlUtils.ConvertXyToCell(_configuration.FreezeColumnCount + 1, _configuration.FreezeRowCount + 1);
sb.Append(WorksheetXml.PaneSelection("bottomRight", cellBR, cellBR));
}
else if (_configuration.FreezeColumnCount > 0)
{
// freeze column
/*
<selection pane="topRight" activeCell="A1" sqref="A1"/>
*/
var cellTR = ExcelOpenXmlUtils.ConvertXyToCell(_configuration.FreezeColumnCount, 1);
sb.Append(WorksheetXml.PaneSelection("topRight", cellTR, cellTR));
}
else
{
// freeze row
/*
<selection pane="bottomLeft"/>
*/
sb.Append(WorksheetXml.PaneSelection("bottomLeft", null, null));
}
return sb.ToString();
}
remove the WriteSheetViews
and WritePanes
methods from \src\MiniExcel\OpenXml\ExcelOpenXmlSheetWriter.cs
replace the 3 WriteSheetViews(writer);
calls in \src\MiniExcel\OpenXml\ExcelOpenXmlSheetWriter.cs
with writer.Write(GetSheetViews());
replace the await WritePanesAsync(writer);
calls in this PR with await writer.WriteAsync(GetSheetViews());
remove the WriteSheetViewsAsync(MiniExcelAsyncStreamWriter writer)
and WritePanesAsync(MiniExcelAsyncStreamWriter writer)
methods in this PR
This will ensure that the sync and async writers are using the same code to build the panes.
Hi @meld-cp, good point, didn't notice the DefaultOpenXml class. I refactored it to prevent the code duplication.
@BaatenHannes 👍👍👍 Merged, could we invite you to our team?
See issue #678. Added async implementation of PR #620 Freeze top row.