From 11f5b2cddd81ae563256b2d23d7daca4de6e7ee3 Mon Sep 17 00:00:00 2001 From: Tony Qu Date: Fri, 6 Mar 2026 12:32:34 +0800 Subject: [PATCH 1/2] Add maxRows parameter to ISheet.AutoSizeColumn method. Previously, maxRows can only be used in XSSFSheet.AutoSizeColumn or HSSFSheet.AutoSizeColumn instead of ISheet.AutoSizeColumn --- main/HSSF/UserModel/HSSFSheet.cs | 21 +++------------------ main/SS/UserModel/Sheet.cs | 16 ++++++++-------- main/SS/Util/SheetUtil.cs | 1 - ooxml/XSSF/Streaming/SXSSFSheet.cs | 2 +- ooxml/XSSF/UserModel/XSSFDialogsheet.cs | 2 +- ooxml/XSSF/UserModel/XSSFSheet.cs | 12 +++++++----- 6 files changed, 20 insertions(+), 34 deletions(-) diff --git a/main/HSSF/UserModel/HSSFSheet.cs b/main/HSSF/UserModel/HSSFSheet.cs index c04ef67807..225c760db2 100644 --- a/main/HSSF/UserModel/HSSFSheet.cs +++ b/main/HSSF/UserModel/HSSFSheet.cs @@ -2342,21 +2342,6 @@ public void AutoSizeColumn(int column) AutoSizeColumn(column, false); } - /// - /// Adjusts the column width to fit the contents. - /// This Process can be relatively slow on large sheets, so this should - /// normally only be called once per column, at the end of your - /// Processing. - /// You can specify whether the content of merged cells should be considered or ignored. - /// Default is to ignore merged cells. - /// - /// the column index - /// whether to use the contents of merged cells when calculating the width of the column - public void AutoSizeColumn(int column, bool useMergedCells) - { - AutoSizeColumn(column, useMergedCells, maxRows: 0); - } - /// /// Adjusts the column width to fit the contents. /// This Process can be relatively slow on large sheets, so this should @@ -2371,16 +2356,16 @@ public void AutoSizeColumn(int column, bool useMergedCells) public void AutoSizeColumn(int column, bool useMergedCells, int maxRows = 0) { double width = SheetUtil.GetColumnWidth(this, column, useMergedCells, maxRows); - if (width != -1) + if(width != -1) { width *= 256; int maxColumnWidth = 255 * 256; // The maximum column width for an individual cell is 255 characters - if (width > maxColumnWidth) + if(width > maxColumnWidth) { width = maxColumnWidth; } - SetColumnWidth(column, (int)width); + SetColumnWidth(column, (int) width); } } diff --git a/main/SS/UserModel/Sheet.cs b/main/SS/UserModel/Sheet.cs index 3c518dcd33..d5a5b87f2f 100644 --- a/main/SS/UserModel/Sheet.cs +++ b/main/SS/UserModel/Sheet.cs @@ -705,16 +705,16 @@ bool Autobreaks /// /// Adjusts the column width to fit the contents. - /// - /// the column index. - /// whether to use the contents of merged cells when - /// calculating the width of the column. Default is to ignore merged cells. - /// /// This process can be relatively slow on large sheets, so this should /// normally only be called once per column, at the end of your - /// processing. - /// - void AutoSizeColumn(int column, bool useMergedCells); + /// Processing. + /// You can specify whether the content of merged cells should be considered or ignored. + /// Default is to ignore merged cells. + /// + /// the column index + /// whether to use the contents of merged cells when calculating the width of the column + /// limit the scope to maxRows rows to speed up the function, or leave 0 (optional) + void AutoSizeColumn(int column, bool useMergedCells, int maxRows = 0); /// /// Adjusts the row height to fit the contents. diff --git a/main/SS/Util/SheetUtil.cs b/main/SS/Util/SheetUtil.cs index d7dfe3e44e..4294a4d0f8 100644 --- a/main/SS/Util/SheetUtil.cs +++ b/main/SS/Util/SheetUtil.cs @@ -913,6 +913,5 @@ public static ICell GetCellWithMerges(ISheet sheet, int rowIx, int colIx) // live within any merged regions return null; } - } } diff --git a/ooxml/XSSF/Streaming/SXSSFSheet.cs b/ooxml/XSSF/Streaming/SXSSFSheet.cs index 5446dcf428..6e9daacfed 100644 --- a/ooxml/XSSF/Streaming/SXSSFSheet.cs +++ b/ooxml/XSSF/Streaming/SXSSFSheet.cs @@ -557,7 +557,7 @@ public void AutoSizeColumn(int column) * @param column the column index to auto-size * @param useMergedCells whether to use the contents of merged cells when calculating the width of the column */ - public void AutoSizeColumn(int column, bool useMergedCells) + public void AutoSizeColumn(int column, bool useMergedCells, int maxRows = 0) { // Multiple calls to autoSizeColumn need to look up the best-fit width // of rows already flushed to disk plus re-calculate the best-fit width diff --git a/ooxml/XSSF/UserModel/XSSFDialogsheet.cs b/ooxml/XSSF/UserModel/XSSFDialogsheet.cs index cd14aa7792..dfdb368657 100644 --- a/ooxml/XSSF/UserModel/XSSFDialogsheet.cs +++ b/ooxml/XSSF/UserModel/XSSFDialogsheet.cs @@ -604,7 +604,7 @@ void ISheet.AutoSizeColumn(int column) throw new System.NotImplementedException(); } - void ISheet.AutoSizeColumn(int column, bool useMergedCells) + void ISheet.AutoSizeColumn(int column, bool useMergedCells, int maxRows=0) { throw new System.NotImplementedException(); } diff --git a/ooxml/XSSF/UserModel/XSSFSheet.cs b/ooxml/XSSF/UserModel/XSSFSheet.cs index acb92a6e31..771939308d 100644 --- a/ooxml/XSSF/UserModel/XSSFSheet.cs +++ b/ooxml/XSSF/UserModel/XSSFSheet.cs @@ -1778,16 +1778,18 @@ public void AutoSizeColumn(int column) /// /// Adjusts the column width to fit the contents. - /// This process can be relatively slow on large sheets, so this should + /// This Process can be relatively slow on large sheets, so this should /// normally only be called once per column, at the end of your /// Processing. + /// You can specify whether the content of merged cells should be considered or ignored. + /// Default is to ignore merged cells. /// /// the column index - /// whether to use the contents of merged cells - /// when calculating the width of the column - public void AutoSizeColumn(int column, bool useMergedCells) + /// whether to use the contents of merged cells when calculating the width of the column + /// limit the scope to maxRows rows to speed up the function, or leave 0 (optional) + public void AutoSizeColumn(int column, bool useMergedCells, int maxRows = 0) { - double width = SheetUtil.GetColumnWidth(this, column, useMergedCells); + double width = SheetUtil.GetColumnWidth(this, column, useMergedCells, maxRows); if(width != -1) { From a53b462dc217430e536e9b711c338cae9301c14d Mon Sep 17 00:00:00 2001 From: Tony Qu Date: Fri, 6 Mar 2026 12:45:00 +0800 Subject: [PATCH 2/2] fix compilation error --- ooxml/XSSF/UserModel/XSSFDialogsheet.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ooxml/XSSF/UserModel/XSSFDialogsheet.cs b/ooxml/XSSF/UserModel/XSSFDialogsheet.cs index dfdb368657..2060bb3e97 100644 --- a/ooxml/XSSF/UserModel/XSSFDialogsheet.cs +++ b/ooxml/XSSF/UserModel/XSSFDialogsheet.cs @@ -604,7 +604,7 @@ void ISheet.AutoSizeColumn(int column) throw new System.NotImplementedException(); } - void ISheet.AutoSizeColumn(int column, bool useMergedCells, int maxRows=0) + void ISheet.AutoSizeColumn(int column, bool useMergedCells, int maxRows) { throw new System.NotImplementedException(); }