nissl-lab / npoi

a .NET library that can read/write Office formats without Microsoft Office installed. No COM+, no interop.
Apache License 2.0
5.65k stars 1.42k forks source link

fixed System.ArgumentException: 'Invalid CellReference: 'Sheet1' #1368

Closed htoyama closed 2 months ago

htoyama commented 2 months ago

NPOI Version

2.7.0

File Type

Upload the Excel File

Book1.xlsx

Reproduce Steps

Workbook.CloneSheet("Sheet1,") Workbook.Write(New_File)

Issue Description

diff --git a/ooxml/XSSF/UserModel/XSSFSheet.cs b/ooxml/XSSF/UserModel/XSSFSheet.cs
index 7a12241b..02b8e410 100644
--- a/ooxml/XSSF/UserModel/XSSFSheet.cs
+++ b/ooxml/XSSF/UserModel/XSSFSheet.cs
@@ -5521,7 +5521,31 @@ private CellRangeAddress GetRepeatingRowsOrColums(bool rows)
                 return null;
             }

-            string[] parts = refStr.Split(",".ToCharArray());
+            // The character(') used to quote sheet names when they contain special characters
+            bool skipComma = false;
+            string subStr = String.Empty;
+            List<string> parts = new List<string>();
+
+            foreach (char c in refStr)
+            {
+                if (c == '\'')
+                    skipComma = !skipComma;
+
+                if (c == ',' && !skipComma)
+                {
+                    if (subStr.Length > 0)
+                    {
+                        parts.Add(subStr);
+                        subStr = String.Empty;
+                    }
+                    continue;
+                }
+                subStr += c;
+            }
+            if (subStr.Length > 0)
+                parts.Add(subStr);
+
+            //string[] parts = refStr.Split(",".ToCharArray());
             int maxRowIndex = SpreadsheetVersion.EXCEL2007.LastRowIndex;
             int maxColIndex = SpreadsheetVersion.EXCEL2007.LastColumnIndex;
             foreach (string part in parts)
tonyqus commented 2 months ago

Hi, I don't fully understand this issue.

Are you providing a fix for this issue? If so, can you use the Pull Request?

htoyama commented 2 months ago

I agree with you. This issue, occur exception with book1.xlsx.