diff --git a/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java b/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java index 7febd236b4..0ed44d3048 100644 --- a/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java +++ b/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExportToXml.java @@ -188,14 +188,17 @@ public class XSSFExportToXml implements Comparator{ Node tableRootNode = getNodeByXPath(table.getCommonXpath(),doc.getFirstChild(),doc,true); short startColumnIndex = table.getStartCellReference().getCol(); - for(int j = startColumnIndex; j<= table.getEndCellReference().getCol();j++) { - XSSFCell cell = row.getCell(j); - if (cell!=null) { - XSSFXmlColumnPr pointer = tableColumns.get(j-startColumnIndex); - String localXPath = pointer.getLocalXPath(); - Node currentNode = getNodeByXPath(localXPath,tableRootNode,doc,false); - - mapCellOnNode(cell,currentNode); + for(int j = startColumnIndex; j<= table.getEndCellReference().getCol(); j++) { + int tableColumnIndex = j - startColumnIndex; + if (tableColumnIndex < tableColumns.size()) { + XSSFCell cell = row.getCell(j); + if (cell != null) { + XSSFXmlColumnPr pointer = tableColumns.get(tableColumnIndex); + String localXPath = pointer.getLocalXPath(); + Node currentNode = getNodeByXPath(localXPath,tableRootNode,doc,false); + + mapCellOnNode(cell,currentNode); + } } } } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExportToXML.java b/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExportToXML.java index 783b7a0b26..5c92901079 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExportToXML.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/extractor/TestXSSFExportToXML.java @@ -17,24 +17,11 @@ package org.apache.poi.xssf.extractor; -import junit.framework.TestCase; -import org.apache.poi.POIXMLDocumentPart; -import org.apache.poi.ss.usermodel.Cell; -import org.apache.poi.ss.usermodel.CellType; -import org.apache.poi.ss.usermodel.Row; -import org.apache.poi.ss.usermodel.Sheet; -import org.apache.poi.util.XMLHelper; -import org.apache.poi.xssf.XSSFTestDataSamples; -import org.apache.poi.xssf.model.MapInfo; -import org.apache.poi.xssf.usermodel.XSSFMap; -import org.apache.poi.xssf.usermodel.XSSFWorkbook; -import org.xml.sax.EntityResolver; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.parsers.ParserConfigurationException; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -43,11 +30,33 @@ import java.util.Date; import java.util.regex.Matcher; import java.util.regex.Pattern; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; + +import org.apache.poi.POIXMLDocumentPart; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellType; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.util.DocumentHelper; +import org.apache.poi.util.XMLHelper; +import org.apache.poi.xssf.XSSFTestDataSamples; +import org.apache.poi.xssf.model.MapInfo; +import org.apache.poi.xssf.usermodel.XSSFMap; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.junit.Ignore; +import org.junit.Test; +import org.xml.sax.EntityResolver; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + /** * @author Roberto Manicardi */ -public final class TestXSSFExportToXML extends TestCase { +public final class TestXSSFExportToXML { + @Test public void testExportToXML() throws Exception { XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMappings.xlsx"); @@ -94,7 +103,8 @@ public final class TestXSSFExportToXML extends TestCase { assertTrue(found); } - public void testExportToXMLInverseOrder() throws Exception { + @Test + public void testExportToXMLInverseOrder() throws Exception { XSSFWorkbook wb = XSSFTestDataSamples .openSampleWorkbook("CustomXmlMappings-inverse-order.xlsx"); @@ -141,7 +151,8 @@ public final class TestXSSFExportToXML extends TestCase { assertTrue(found); } - public void testXPathOrdering() { + @Test + public void testXPathOrdering() { XSSFWorkbook wb = XSSFTestDataSamples .openSampleWorkbook("CustomXmlMappings-inverse-order.xlsx"); @@ -164,7 +175,8 @@ public final class TestXSSFExportToXML extends TestCase { assertTrue(found); } - public void testMultiTable() throws Exception { + @Test + public void testMultiTable() throws Exception { XSSFWorkbook wb = XSSFTestDataSamples .openSampleWorkbook("CustomXMLMappings-complex-type.xlsx"); @@ -207,19 +219,20 @@ public final class TestXSSFExportToXML extends TestCase { assertTrue(found); } + @Test + @Ignore(value="Fails, but I don't know if it is ok or not...") public void testExportToXMLSingleAttributeNamespace() throws Exception { - // TODO: Fails, but I don't know if it is ok or not... - -// XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMapping-singleattributenamespace.xlsx"); -// -// for (XSSFMap map : wb.getCustomXMLMappings()) { -// XSSFExportToXml exporter = new XSSFExportToXml(map); -// -// ByteArrayOutputStream os = new ByteArrayOutputStream(); -// exporter.exportToXML(os, true); -// } + XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMapping-singleattributenamespace.xlsx"); + + for (XSSFMap map : wb.getCustomXMLMappings()) { + XSSFExportToXml exporter = new XSSFExportToXml(map); + + ByteArrayOutputStream os = new ByteArrayOutputStream(); + exporter.exportToXML(os, true); + } } + @Test public void test55850ComplexXmlExport() throws Exception { XSSFWorkbook wb = XSSFTestDataSamples @@ -264,76 +277,79 @@ public final class TestXSSFExportToXML extends TestCase { assertTrue(found); } - public void testFormulaCells_Bugzilla_55927() throws Exception { - XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55927.xlsx"); - - boolean found = false; - for (POIXMLDocumentPart p : wb.getRelations()) { - - if (!(p instanceof MapInfo)) { - continue; - } - MapInfo mapInfo = (MapInfo) p; - - XSSFMap map = mapInfo.getXSSFMapById(1); - - assertNotNull("XSSFMap is null", map); - - XSSFExportToXml exporter = new XSSFExportToXml(map); - ByteArrayOutputStream os = new ByteArrayOutputStream(); - exporter.exportToXML(os, true); - String xmlData = os.toString("UTF-8"); - - assertNotNull(xmlData); - assertFalse(xmlData.equals("")); - - assertEquals("2012-01-13", xmlData.split("")[1].split("")[0].trim()); - assertEquals("2012-02-16", xmlData.split("")[1].split("")[0].trim()); - - parseXML(xmlData); - - found = true; - } - assertTrue(found); - } + @Test + public void testFormulaCells_Bugzilla_55927() throws Exception { + XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55927.xlsx"); - public void testFormulaCells_Bugzilla_55926() throws Exception { - XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55926.xlsx"); + boolean found = false; + for (POIXMLDocumentPart p : wb.getRelations()) { - boolean found = false; - for (POIXMLDocumentPart p : wb.getRelations()) { + if (!(p instanceof MapInfo)) { + continue; + } + MapInfo mapInfo = (MapInfo) p; - if (!(p instanceof MapInfo)) { - continue; - } - MapInfo mapInfo = (MapInfo) p; + XSSFMap map = mapInfo.getXSSFMapById(1); - XSSFMap map = mapInfo.getXSSFMapById(1); + assertNotNull("XSSFMap is null", map); - assertNotNull("XSSFMap is null", map); + XSSFExportToXml exporter = new XSSFExportToXml(map); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + exporter.exportToXML(os, true); + String xmlData = os.toString("UTF-8"); - XSSFExportToXml exporter = new XSSFExportToXml(map); - ByteArrayOutputStream os = new ByteArrayOutputStream(); - exporter.exportToXML(os, true); - String xmlData = os.toString("UTF-8"); + assertNotNull(xmlData); + assertFalse(xmlData.equals("")); - assertNotNull(xmlData); - assertFalse(xmlData.equals("")); - - String a = xmlData.split("")[1].split("")[0].trim(); - String doubleValue = a.split("")[1].split("")[0].trim(); - String stringValue = a.split("")[1].split("")[0].trim(); - - assertEquals("Hello World", stringValue); - assertEquals("5.1", doubleValue); - - parseXML(xmlData); - - found = true; - } - assertTrue(found); - } + assertEquals("2012-01-13", xmlData.split("")[1].split("")[0].trim()); + assertEquals("2012-02-16", xmlData.split("")[1].split("")[0].trim()); + + parseXML(xmlData); + + found = true; + } + assertTrue(found); + } + + @Test + public void testFormulaCells_Bugzilla_55926() throws Exception { + XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55926.xlsx"); + + boolean found = false; + for (POIXMLDocumentPart p : wb.getRelations()) { + + if (!(p instanceof MapInfo)) { + continue; + } + MapInfo mapInfo = (MapInfo) p; + + XSSFMap map = mapInfo.getXSSFMapById(1); + + assertNotNull("XSSFMap is null", map); + + XSSFExportToXml exporter = new XSSFExportToXml(map); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + exporter.exportToXML(os, true); + String xmlData = os.toString("UTF-8"); + + assertNotNull(xmlData); + assertFalse(xmlData.equals("")); + + String a = xmlData.split("")[1].split("")[0].trim(); + String doubleValue = a.split("")[1].split("")[0].trim(); + String stringValue = a.split("")[1].split("")[0].trim(); + + assertEquals("Hello World", stringValue); + assertEquals("5.1", doubleValue); + + parseXML(xmlData); + + found = true; + } + assertTrue(found); + } + @Test public void testXmlExportIgnoresEmptyCells_Bugzilla_55924() throws Exception { XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55924.xlsx"); @@ -369,6 +385,7 @@ public final class TestXSSFExportToXML extends TestCase { assertTrue(found); } + @Test public void testXmlExportSchemaWithXSAllTag_Bugzilla_56169() throws Exception { XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56169.xlsx"); @@ -403,6 +420,7 @@ public final class TestXSSFExportToXML extends TestCase { } } + @Test public void testXmlExportCompare_Bug_55923() throws Exception { XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx"); @@ -432,6 +450,7 @@ public final class TestXSSFExportToXML extends TestCase { assertTrue(found); } + @Test public void testXmlExportSchemaOrderingBug_Bugzilla_55923() throws Exception { XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx"); @@ -498,6 +517,7 @@ public final class TestXSSFExportToXML extends TestCase { } } + @Test public void testExportDataTypes() throws Exception { XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx"); @@ -558,6 +578,7 @@ public final class TestXSSFExportToXML extends TestCase { assertTrue(found); } + @Test public void testValidateFalse() throws Exception { XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx"); @@ -588,6 +609,7 @@ public final class TestXSSFExportToXML extends TestCase { assertTrue(found); } + @Test public void testRefElementsInXmlSchema_Bugzilla_56730() throws Exception { XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56730.xlsx"); @@ -621,7 +643,8 @@ public final class TestXSSFExportToXML extends TestCase { assertTrue(found); } - public void testBug59026() throws Exception { + @Test + public void testBug59026() throws Exception { XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("59026.xlsx"); Collection mappings = wb.getCustomXMLMappings(); @@ -634,4 +657,15 @@ public final class TestXSSFExportToXML extends TestCase { assertNotNull(os.toString("UTF-8")); } } + + @Test + public void testExportTableWithNonMappedColumn_Bugzilla_61281() throws Exception { + XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("61281.xlsx"); + for (XSSFMap map : wb.getCustomXMLMappings()) { + XSSFExportToXml exporter = new XSSFExportToXml(map); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + exporter.exportToXML(bos, true); + assertNotNull(DocumentHelper.readDocument(new ByteArrayInputStream(bos.toByteArray()))); + } + } } diff --git a/test-data/spreadsheet/61281.xlsx b/test-data/spreadsheet/61281.xlsx new file mode 100644 index 0000000000..6e8cd739e1 Binary files /dev/null and b/test-data/spreadsheet/61281.xlsx differ