Skip to content

Commit

Permalink
move freezing top row implementation to DefaultOpenXml.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
BaatenHannes committed Nov 1, 2024
1 parent b04837f commit 7913cbc
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 178 deletions.
93 changes: 3 additions & 90 deletions src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private async Task GenerateSheetByIDataReaderAsync(MiniExcelAsyncStreamWriter wr
maxColumnIndex = props.Count;

//sheet view
await WritePanesAsync(writer);
await writer.WriteAsync(GetSheetViews());

await WriteColumnsWidthsAsync(writer, props);

Expand Down Expand Up @@ -245,7 +245,7 @@ private async Task GenerateSheetByEnumerableAsync(MiniExcelAsyncStreamWriter wri
}

//sheet view
await WritePanesAsync(writer);
await writer.WriteAsync(GetSheetViews());

//cols:width
await WriteColumnsWidthsAsync(writer, props);
Expand Down Expand Up @@ -316,7 +316,7 @@ private async Task GenerateSheetByDataTableAsync(MiniExcelAsyncStreamWriter writ
}

//sheet view
await WritePanesAsync(writer);
await writer.WriteAsync(GetSheetViews());

await WriteColumnsWidthsAsync(writer, props);

Expand Down Expand Up @@ -361,93 +361,6 @@ private async Task GenerateSheetByDataTableAsync(MiniExcelAsyncStreamWriter writ
await writer.WriteAsync(WorksheetXml.EndWorksheet);
}

private async Task WriteSheetViewsAsync(MiniExcelAsyncStreamWriter writer)
{
// exit early if no style to write
if (_configuration.FreezeRowCount <= 0 && _configuration.FreezeColumnCount <= 0)
{
return;
}

// start sheetViews
await writer.WriteAsync(WorksheetXml.StartSheetViews);
await writer.WriteAsync(WorksheetXml.StartSheetView());

// Write panes
await WritePanesAsync(writer);

// end sheetViews
await writer.WriteAsync(WorksheetXml.EndSheetView);
await writer.WriteAsync(WorksheetXml.EndSheetViews);
}

private async Task WritePanesAsync(MiniExcelAsyncStreamWriter writer)
{

string activePane;
if (_configuration.FreezeColumnCount > 0 && _configuration.FreezeRowCount > 0)
{
activePane = "bottomRight";
}
else if (_configuration.FreezeColumnCount > 0)
{
activePane = "topRight";
}
else
{
activePane = "bottomLeft";
}
await writer.WriteAsync(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);
await writer.WriteAsync(WorksheetXml.PaneSelection("topRight", cellTR, cellTR));

var cellBL = ExcelOpenXmlUtils.ConvertXyToCell(1, _configuration.FreezeRowCount + 1);
await writer.WriteAsync(WorksheetXml.PaneSelection("bottomLeft", cellBL, cellBL));

var cellBR = ExcelOpenXmlUtils.ConvertXyToCell(_configuration.FreezeColumnCount + 1, _configuration.FreezeRowCount + 1);
await writer.WriteAsync(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);
await writer.WriteAsync(WorksheetXml.PaneSelection("topRight", cellTR, cellTR));

}
else
{
// freeze row
/*
<selection pane="bottomLeft"/>
*/
await writer.WriteAsync(WorksheetXml.PaneSelection("bottomLeft", null, null));

}

}

private static async Task WriteColumnsWidthsAsync(MiniExcelAsyncStreamWriter writer, IEnumerable<ExcelColumnInfo> props)
{
var ecwProps = props.Where(x => x?.ExcelColumnWidth != null).ToList();
Expand Down
97 changes: 97 additions & 0 deletions src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.DefaultOpenXml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,103 @@ private ExcellSheetInfo GetSheetInfos(string sheetName)

return info;
}

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

}

private ExcelColumnInfo GetColumnInfosFromDynamicConfiguration(string columnName)
{
Expand Down
91 changes: 3 additions & 88 deletions src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private void GenerateSheetByIDataReader(MiniExcelStreamWriter writer, IDataReade
maxColumnIndex = props.Count;

//sheet view
WriteSheetViews(writer);
writer.Write(GetSheetViews());

WriteColumnsWidths(writer, props);

Expand Down Expand Up @@ -279,7 +279,7 @@ private void GenerateSheetByEnumerable(MiniExcelStreamWriter writer, IEnumerable
}

//sheet view
WriteSheetViews(writer);
writer.Write(GetSheetViews());

//cols:width
WriteColumnsWidths(writer, props);
Expand Down Expand Up @@ -352,7 +352,7 @@ private void GenerateSheetByDataTable(MiniExcelStreamWriter writer, DataTable va
}

//sheet view
WriteSheetViews(writer);
writer.Write(GetSheetViews());

WriteColumnsWidths(writer, props);

Expand Down Expand Up @@ -411,91 +411,6 @@ private static void WriteColumnsWidths(MiniExcelStreamWriter writer, IEnumerable
writer.Write(WorksheetXml.EndCols);
}

private void WriteSheetViews(MiniExcelStreamWriter writer) {
// exit early if no style to write
if (_configuration.FreezeRowCount <= 0 && _configuration.FreezeColumnCount <= 0)
{
return;
}

// start sheetViews
writer.Write(WorksheetXml.StartSheetViews);
writer.Write(WorksheetXml.StartSheetView());

// Write panes
WritePanes(writer);

// end sheetViews
writer.Write(WorksheetXml.EndSheetView);
writer.Write(WorksheetXml.EndSheetViews);
}

private void WritePanes(MiniExcelStreamWriter writer) {

string activePane;
if (_configuration.FreezeColumnCount > 0 && _configuration.FreezeRowCount > 0)
{
activePane = "bottomRight";
}
else if (_configuration.FreezeColumnCount > 0)
{
activePane = "topRight";
}
else
{
activePane = "bottomLeft";
}
writer.Write( 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);
writer.Write(WorksheetXml.PaneSelection("topRight", cellTR, cellTR));

var cellBL = ExcelOpenXmlUtils.ConvertXyToCell(1, _configuration.FreezeRowCount+1);
writer.Write(WorksheetXml.PaneSelection("bottomLeft", cellBL, cellBL));

var cellBR = ExcelOpenXmlUtils.ConvertXyToCell(_configuration.FreezeColumnCount+1, _configuration.FreezeRowCount+1);
writer.Write(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);
writer.Write(WorksheetXml.PaneSelection("topRight", cellTR, cellTR));

}
else
{
// freeze row
/*
<selection pane="bottomLeft"/>
*/
writer.Write(WorksheetXml.PaneSelection("bottomLeft", null, null));

}

}

private static void PrintHeader(MiniExcelStreamWriter writer, List<ExcelColumnInfo> props)
{
var xIndex = 1;
Expand Down

0 comments on commit 7913cbc

Please sign in to comment.