From 7913cbc001e082caba41f366abf4cdae14e9228a Mon Sep 17 00:00:00 2001 From: Hannes Date: Fri, 1 Nov 2024 21:11:51 +0100 Subject: [PATCH] move freezing top row implementation to DefaultOpenXml.cs --- .../OpenXml/ExcelOpenXmlSheetWriter.Async.cs | 93 +----------------- .../ExcelOpenXmlSheetWriter.DefaultOpenXml.cs | 97 +++++++++++++++++++ .../OpenXml/ExcelOpenXmlSheetWriter.cs | 91 +---------------- 3 files changed, 103 insertions(+), 178 deletions(-) diff --git a/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.Async.cs b/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.Async.cs index 4fc9b0e..d852a4d 100644 --- a/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.Async.cs +++ b/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.Async.cs @@ -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); @@ -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); @@ -316,7 +316,7 @@ private async Task GenerateSheetByDataTableAsync(MiniExcelAsyncStreamWriter writ } //sheet view - await WritePanesAsync(writer); + await writer.WriteAsync(GetSheetViews()); await WriteColumnsWidthsAsync(writer, props); @@ -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 - /* - - - - */ - 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 - /* - - */ - var cellTR = ExcelOpenXmlUtils.ConvertXyToCell(_configuration.FreezeColumnCount, 1); - await writer.WriteAsync(WorksheetXml.PaneSelection("topRight", cellTR, cellTR)); - - } - else - { - // freeze row - /* - - */ - await writer.WriteAsync(WorksheetXml.PaneSelection("bottomLeft", null, null)); - - } - - } - private static async Task WriteColumnsWidthsAsync(MiniExcelAsyncStreamWriter writer, IEnumerable props) { var ecwProps = props.Where(x => x?.ExcelColumnWidth != null).ToList(); diff --git a/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.DefaultOpenXml.cs b/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.DefaultOpenXml.cs index b511c51..23bd573 100644 --- a/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.DefaultOpenXml.cs +++ b/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.DefaultOpenXml.cs @@ -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 + /* + + + + */ + 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 + /* + + */ + var cellTR = ExcelOpenXmlUtils.ConvertXyToCell(_configuration.FreezeColumnCount, 1); + sb.Append(WorksheetXml.PaneSelection("topRight", cellTR, cellTR)); + + } + else + { + // freeze row + /* + + */ + sb.Append(WorksheetXml.PaneSelection("bottomLeft", null, null)); + + } + + return sb.ToString(); + + } private ExcelColumnInfo GetColumnInfosFromDynamicConfiguration(string columnName) { diff --git a/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs b/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs index c03abad..82b9532 100644 --- a/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs +++ b/src/MiniExcel/OpenXml/ExcelOpenXmlSheetWriter.cs @@ -152,7 +152,7 @@ private void GenerateSheetByIDataReader(MiniExcelStreamWriter writer, IDataReade maxColumnIndex = props.Count; //sheet view - WriteSheetViews(writer); + writer.Write(GetSheetViews()); WriteColumnsWidths(writer, props); @@ -279,7 +279,7 @@ private void GenerateSheetByEnumerable(MiniExcelStreamWriter writer, IEnumerable } //sheet view - WriteSheetViews(writer); + writer.Write(GetSheetViews()); //cols:width WriteColumnsWidths(writer, props); @@ -352,7 +352,7 @@ private void GenerateSheetByDataTable(MiniExcelStreamWriter writer, DataTable va } //sheet view - WriteSheetViews(writer); + writer.Write(GetSheetViews()); WriteColumnsWidths(writer, props); @@ -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 - /* - - - - */ - 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 - /* - - */ - var cellTR = ExcelOpenXmlUtils.ConvertXyToCell(_configuration.FreezeColumnCount, 1); - writer.Write(WorksheetXml.PaneSelection("topRight", cellTR, cellTR)); - - } - else - { - // freeze row - /* - - */ - writer.Write(WorksheetXml.PaneSelection("bottomLeft", null, null)); - - } - - } - private static void PrintHeader(MiniExcelStreamWriter writer, List props) { var xIndex = 1;