From 71a42803df447e54a25d8d3ff2f6f35a25e4c92d Mon Sep 17 00:00:00 2001 From: suppernovae Date: Tue, 28 Feb 2023 00:45:24 +0100 Subject: [PATCH 1/2] Add null reference check in UpdateHeaders() --- ooxml/XSSF/UserModel/XSSFTable.cs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/ooxml/XSSF/UserModel/XSSFTable.cs b/ooxml/XSSF/UserModel/XSSFTable.cs index d797b4b5c..e437a445b 100644 --- a/ooxml/XSSF/UserModel/XSSFTable.cs +++ b/ooxml/XSSF/UserModel/XSSFTable.cs @@ -594,23 +594,27 @@ public void UpdateHeaders() { XSSFSheet sheet = (XSSFSheet)GetParent(); CellReference ref1 = StartCellReference; + if (ref1 == null) return; int headerRow = ref1.Row; int firstHeaderColumn = ref1.Col; - XSSFRow row = sheet.GetRow(headerRow) as XSSFRow; - if (row != null && row.GetCTRow() != null) + if (sheet.GetRow(headerRow) is XSSFRow row) { int cellnum = firstHeaderColumn; - foreach (CT_TableColumn col in GetCTTable().tableColumns.tableColumn) + CT_TableColumns tableColumns = GetCTTable().tableColumns; + + if (tableColumns != null) { - XSSFCell cell = row.GetCell(cellnum) as XSSFCell; - if (cell != null) + foreach (CT_TableColumn col in tableColumns.tableColumn) { - col.name = (cell.StringCellValue); + if (row.GetCell(cellnum) is XSSFCell cell) + { + col.name = cell.StringCellValue; + } + cellnum++; } - cellnum++; } } tableColumns = null; From afc1897e576e3fffd969fdf2b3c62742ea609074 Mon Sep 17 00:00:00 2001 From: suppernovae Date: Mon, 20 Mar 2023 01:06:34 +0100 Subject: [PATCH 2/2] XSSFTable: Include a test case for SetCellReferences() --- .../ooxml/XSSF/UserModel/TestXSSFTable.cs | 35 ++++++++++++++++--- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/testcases/ooxml/XSSF/UserModel/TestXSSFTable.cs b/testcases/ooxml/XSSF/UserModel/TestXSSFTable.cs index f15f0b61f..5dc47a292 100644 --- a/testcases/ooxml/XSSF/UserModel/TestXSSFTable.cs +++ b/testcases/ooxml/XSSF/UserModel/TestXSSFTable.cs @@ -39,7 +39,7 @@ public void Bug56274() // read the original sheet header order XSSFRow row = inputWorkbook.GetSheetAt(0).GetRow(0) as XSSFRow; - List headers = new List(); + List headers = new List(); foreach (ICell cell in row) { headers.Add(cell.StringCellValue); @@ -53,7 +53,7 @@ public void Bug56274() // re-read the saved file and make sure headers in the xml are in the original order //inputWorkbook = new NPOI.XSSF.UserModel.XSSFWorkbook(new FileStream(outputFile)); - inputWorkbook = XSSFTestDataSamples.WriteOutAndReadBack(inputWorkbook) as XSSFWorkbook; + inputWorkbook = XSSFTestDataSamples.WriteOutAndReadBack(inputWorkbook); CT_Table ctTable = (inputWorkbook.GetSheetAt(0) as XSSFSheet).GetTables()[0].GetCTTable(); List ctTableColumnList = ctTable.tableColumns.tableColumn; @@ -65,6 +65,7 @@ public void Bug56274() "header name in xml table should match number of header cells in worksheet"); } //Assert.IsTrue(outputFile.Delete()); + inputWorkbook.Close(); } [Test] public void TestCTTableStyleInfo() @@ -83,7 +84,7 @@ public void TestCTTableStyleInfo() outputStyleInfo.showColumnStripes = (false); outputStyleInfo.showRowStripes = (true); - XSSFWorkbook inputWorkbook = XSSFTestDataSamples.WriteOutAndReadBack(outputWorkbook) as XSSFWorkbook; + XSSFWorkbook inputWorkbook = XSSFTestDataSamples.WriteOutAndReadBack(outputWorkbook); List tables = (inputWorkbook.GetSheetAt(0) as XSSFSheet).GetTables(); Assert.AreEqual(1, tables.Count, "Tables number"); @@ -94,6 +95,7 @@ public void TestCTTableStyleInfo() Assert.AreEqual(outputStyleInfo.name, inputStyleInfo.name, "Style name"); Assert.AreEqual(outputStyleInfo.showColumnStripes, inputStyleInfo.showColumnStripes, "Show column stripes"); Assert.AreEqual(outputStyleInfo.showRowStripes, inputStyleInfo.showRowStripes, "Show row stripes"); + outputWorkbook.Close(); } @@ -226,7 +228,7 @@ public void GetCellReferences() // can be synchronized with the underlying CTTable XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet sh = wb.CreateSheet() as XSSFSheet; - XSSFTable table = sh.CreateTable() as XSSFTable; + XSSFTable table = sh.CreateTable(); CT_Table ctTable = table.GetCTTable(); ctTable.@ref = "B2:E8"; Assert.AreEqual(new CellReference("B2"), table.StartCellReference); @@ -241,6 +243,7 @@ public void GetCellReferences() table.UpdateReferences(); Assert.AreEqual(new CellReference("C1"), table.StartCellReference); Assert.AreEqual(new CellReference("M3"), table.EndCellReference); + wb.Close(); } [Test] @@ -248,7 +251,7 @@ public void GetRowCount() { XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet sh = wb.CreateSheet() as XSSFSheet; - XSSFTable table = sh.CreateTable() as XSSFTable; + XSSFTable table = sh.CreateTable(); CT_Table ctTable = table.GetCTTable(); Assert.AreEqual(0, table.RowCount); ctTable.@ref = "B2:B2"; @@ -259,6 +262,28 @@ public void GetRowCount() // update cell references to clear the cache table.UpdateReferences(); Assert.AreEqual(11, table.RowCount); + wb.Close(); + } + + [Test] + public void FormatAsTable() + { + XSSFWorkbook wb = new XSSFWorkbook(); + XSSFSheet sh = wb.CreateSheet() as XSSFSheet; + + XSSFRow row = sh.CreateRow(0) as XSSFRow; + row.CreateCell(0).SetCellValue("Col1"); + row.CreateCell(1).SetCellValue("Col2"); + row.CreateCell(2).SetCellValue("Col3"); + + row = sh.CreateRow(1) as XSSFRow; + row.CreateCell(0).SetCellValue("Value1"); + row.CreateCell(1).SetCellValue("Value2"); + row.CreateCell(2).SetCellValue("Value3"); + + XSSFTable table = sh.CreateTable(); + table.SetCellReferences(new AreaReference(new CellReference(0, 0), new CellReference(1, 2))); + wb.Close(); } }