aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNick Burch <nick@apache.org>2014-05-21 17:08:25 +0000
committerNick Burch <nick@apache.org>2014-05-21 17:08:25 +0000
commitfdf6244ed3450c5b334caafc794261c7797ef9a9 (patch)
tree4bae1d8d839206299fa26afe1fce63fe3e794ace
parent9c51a2f5b7b8eced7d931b732729f44d88a38c6c (diff)
downloadpoi-fdf6244ed3450c5b334caafc794261c7797ef9a9.tar.gz
poi-fdf6244ed3450c5b334caafc794261c7797ef9a9.zip
Patch from Yaniv Kunda from bug #56274 - Fix SXSSF writing of tables when creating from a template
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1596624 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java14
-rw-r--r--src/ooxml/testcases/org/apache/poi/xssf/usermodel/AllXSSFUsermodelTests.java1
-rw-r--r--src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFTable.java73
-rw-r--r--test-data/spreadsheet/56274.xlsxbin0 -> 12782 bytes
4 files changed, 82 insertions, 6 deletions
diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java
index 4f71c2d8ce..9d7e4e77b8 100644
--- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java
+++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFTable.java
@@ -294,13 +294,15 @@ public class XSSFTable extends POIXMLDocumentPart {
int firstHeaderColumn = ref.getCol();
XSSFRow row = sheet.getRow(headerRow);
- if(row != null) for(CTTableColumn col : getCTTable().getTableColumns().getTableColumnList()){
- int colIdx = (int)col.getId() - 1 + firstHeaderColumn;
- XSSFCell cell = row.getCell(colIdx);
- if(cell != null) {
- col.setName(cell.getStringCellValue());
+ if (row != null && row.getCTRow().validate()) {
+ int cellnum = firstHeaderColumn;
+ for (CTTableColumn col : getCTTable().getTableColumns().getTableColumnList()) {
+ XSSFCell cell = row.getCell(cellnum);
+ if (cell != null) {
+ col.setName(cell.getStringCellValue());
+ }
+ cellnum++;
}
}
-
}
}
diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/AllXSSFUsermodelTests.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/AllXSSFUsermodelTests.java
index 11308159ba..e05b2f9a1e 100644
--- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/AllXSSFUsermodelTests.java
+++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/AllXSSFUsermodelTests.java
@@ -50,6 +50,7 @@ import org.junit.runners.Suite;
TestXSSFRow.class,
TestXSSFSheet.class,
TestXSSFSheetUpdateArrayFormulas.class,
+ TestXSSFTable.class,
TestXSSFWorkbook.class,
TestXSSFBorder.class,
TestXSSFCellFill.class,
diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFTable.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFTable.java
new file mode 100644
index 0000000000..78e5d80a0f
--- /dev/null
+++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFTable.java
@@ -0,0 +1,73 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.xssf.usermodel;
+
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.xssf.XSSFTestDataSamples;
+import org.apache.poi.xssf.streaming.SXSSFWorkbook;
+import org.junit.Test;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTable;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public final class TestXSSFTable {
+
+ public TestXSSFTable() {
+ }
+
+ @Test
+ public void bug56274() throws IOException {
+ // read sample file
+ XSSFWorkbook inputWorkbook = XSSFTestDataSamples.openSampleWorkbook("56274.xlsx");
+
+ // read the original sheet header order
+ XSSFRow row = inputWorkbook.getSheetAt(0).getRow(0);
+ List<String> headers = new ArrayList<String>();
+ for (Cell cell : row) {
+ headers.add(cell.getStringCellValue());
+ }
+
+ // save the worksheet as-is using SXSSF
+ File outputFile = File.createTempFile("poi-56274", ".xlsx");
+ SXSSFWorkbook outputWorkbook = new org.apache.poi.xssf.streaming.SXSSFWorkbook(inputWorkbook);
+ outputWorkbook.write(new FileOutputStream(outputFile));
+
+ // re-read the saved file and make sure headers in the xml are in the original order
+ inputWorkbook = new org.apache.poi.xssf.usermodel.XSSFWorkbook(new FileInputStream(outputFile));
+ CTTable ctTable = inputWorkbook.getSheetAt(0).getTables().get(0).getCTTable();
+ List<CTTableColumn> ctTableColumnList = ctTable.getTableColumns().getTableColumnList();
+
+ assertEquals("number of headers in xml table should match number of header cells in worksheet",
+ headers.size(), ctTableColumnList.size());
+ for (int i = 0; i < headers.size(); i++) {
+ assertEquals("header name in xml table should match number of header cells in worksheet",
+ headers.get(i), ctTableColumnList.get(i).getName());
+ }
+ assertTrue(outputFile.delete());
+ }
+
+} \ No newline at end of file
diff --git a/test-data/spreadsheet/56274.xlsx b/test-data/spreadsheet/56274.xlsx
new file mode 100644
index 0000000000..8c3573adb3
--- /dev/null
+++ b/test-data/spreadsheet/56274.xlsx
Binary files differ