Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions ooxml/XSSF/UserModel/XSSFTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
35 changes: 30 additions & 5 deletions testcases/ooxml/XSSF/UserModel/TestXSSFTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void Bug56274()

// read the original sheet header order
XSSFRow row = inputWorkbook.GetSheetAt(0).GetRow(0) as XSSFRow;
List<String> headers = new List<String>();
List<string> headers = new List<string>();
foreach (ICell cell in row)
{
headers.Add(cell.StringCellValue);
Expand All @@ -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<CT_TableColumn> ctTableColumnList = ctTable.tableColumns.tableColumn;

Expand All @@ -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()
Expand All @@ -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<XSSFTable> tables = (inputWorkbook.GetSheetAt(0) as XSSFSheet).GetTables();
Assert.AreEqual(1, tables.Count, "Tables number");

Expand All @@ -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();

}

Expand Down Expand Up @@ -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);
Expand All @@ -241,14 +243,15 @@ public void GetCellReferences()
table.UpdateReferences();
Assert.AreEqual(new CellReference("C1"), table.StartCellReference);
Assert.AreEqual(new CellReference("M3"), table.EndCellReference);
wb.Close();
}

[Test]
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";
Expand All @@ -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();
}

}
Expand Down