Search in sources :

Example 11 with XSSFMap

use of org.apache.poi.xssf.usermodel.XSSFMap in project poi by apache.

the class TestXSSFExportToXML method testFormulaCells_Bugzilla_55926.

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("<A>")[1].split("</A>")[0].trim();
        String doubleValue = a.split("<DOUBLE>")[1].split("</DOUBLE>")[0].trim();
        String stringValue = a.split("<STRING>")[1].split("</STRING>")[0].trim();
        assertEquals("Hello World", stringValue);
        assertEquals("5.1", doubleValue);
        parseXML(xmlData);
        found = true;
    }
    assertTrue(found);
}
Also used : POIXMLDocumentPart(org.apache.poi.POIXMLDocumentPart) XSSFMap(org.apache.poi.xssf.usermodel.XSSFMap) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) MapInfo(org.apache.poi.xssf.model.MapInfo) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 12 with XSSFMap

use of org.apache.poi.xssf.usermodel.XSSFMap in project poi by apache.

the class XSSFFileHandler method exportToXML.

private void exportToXML(XSSFWorkbook wb) throws SAXException, ParserConfigurationException, TransformerException {
    for (XSSFMap map : wb.getCustomXMLMappings()) {
        XSSFExportToXml exporter = new XSSFExportToXml(map);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        exporter.exportToXML(os, true);
    }
}
Also used : XSSFMap(org.apache.poi.xssf.usermodel.XSSFMap) XSSFExportToXml(org.apache.poi.xssf.extractor.XSSFExportToXml) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 13 with XSSFMap

use of org.apache.poi.xssf.usermodel.XSSFMap in project poi by apache.

the class TestXSSFExportToXML method testXmlExportCompare_Bug_55923.

public void testXmlExportCompare_Bug_55923() throws Exception {
    XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx");
    boolean found = false;
    for (POIXMLDocumentPart p : wb.getRelations()) {
        if (!(p instanceof MapInfo)) {
            continue;
        }
        MapInfo mapInfo = (MapInfo) p;
        XSSFMap map = mapInfo.getXSSFMapById(4);
        assertNotNull("XSSFMap is null", map);
        XSSFExportToXml exporter = new XSSFExportToXml(map);
        assertEquals(0, exporter.compare("", ""));
        assertEquals(0, exporter.compare("/", "/"));
        assertEquals(0, exporter.compare("//", "//"));
        assertEquals(0, exporter.compare("/a/", "/b/"));
        assertEquals(-1, exporter.compare("/ns1:Entry/ns1:A/ns1:B/ns1:C/ns1:E/ns1:EUR", "/ns1:Entry/ns1:A/ns1:B/ns1:C/ns1:E/ns1:CHF"));
        found = true;
    }
    assertTrue(found);
}
Also used : POIXMLDocumentPart(org.apache.poi.POIXMLDocumentPart) XSSFMap(org.apache.poi.xssf.usermodel.XSSFMap) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) MapInfo(org.apache.poi.xssf.model.MapInfo)

Example 14 with XSSFMap

use of org.apache.poi.xssf.usermodel.XSSFMap in project poi by apache.

the class TestXSSFImportFromXML method testMultiTable.

@Test(timeout = 60000)
public void testMultiTable() throws IOException, XPathExpressionException, SAXException {
    XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMappings-complex-type.xlsx");
    try {
        String cellC6 = "c6";
        String cellC7 = "c7";
        String cellC8 = "c8";
        String cellC9 = "c9";
        String testXML = "<ns1:MapInfo xmlns:ns1=\"" + NS_SPREADSHEETML + "\" SelectionNamespaces=\"\">" + "<ns1:Schema ID=\"" + cellC6 + "\" SchemaRef=\"a\" />" + "<ns1:Schema ID=\"" + cellC7 + "\" SchemaRef=\"b\" />" + "<ns1:Schema ID=\"" + cellC8 + "\" SchemaRef=\"c\" />" + "<ns1:Schema ID=\"" + cellC9 + "\" SchemaRef=\"d\" />";
        for (int i = 10; i < 10010; i++) {
            testXML += "<ns1:Schema ID=\"c" + i + "\" SchemaRef=\"d\" />";
        }
        testXML += "<ns1:Map ID=\"1\" Name=\"\" RootElement=\"\" SchemaID=\"\" ShowImportExportValidationErrors=\"\" AutoFit=\"\" Append=\"\" PreserveSortAFLayout=\"\" PreserveFormat=\"\">" + "<ns1:DataBinding DataBindingLoadMode=\"\" />" + "</ns1:Map>" + "<ns1:Map ID=\"2\" Name=\"\" RootElement=\"\" SchemaID=\"\" ShowImportExportValidationErrors=\"\" AutoFit=\"\" Append=\"\" PreserveSortAFLayout=\"\" PreserveFormat=\"\">" + "<ns1:DataBinding DataBindingLoadMode=\"\" />" + "</ns1:Map>" + "<ns1:Map ID=\"3\" Name=\"\" RootElement=\"\" SchemaID=\"\" ShowImportExportValidationErrors=\"\" AutoFit=\"\" Append=\"\" PreserveSortAFLayout=\"\" PreserveFormat=\"\">" + "<ns1:DataBinding DataBindingLoadMode=\"\" />" + "</ns1:Map>" + "</ns1:MapInfo>";
        XSSFMap map = wb.getMapInfo().getXSSFMapByName("MapInfo_mapping");
        assertNotNull(map);
        XSSFImportFromXML importer = new XSSFImportFromXML(map);
        importer.importFromXML(testXML);
        //Check for Schema element
        XSSFSheet sheet = wb.getSheetAt(1);
        assertEquals(cellC6, sheet.getRow(5).getCell(2).getStringCellValue());
        assertEquals(cellC7, sheet.getRow(6).getCell(2).getStringCellValue());
        assertEquals(cellC8, sheet.getRow(7).getCell(2).getStringCellValue());
        assertEquals(cellC9, sheet.getRow(8).getCell(2).getStringCellValue());
        assertEquals("c5001", sheet.getRow(5000).getCell(2).getStringCellValue());
    } finally {
        wb.close();
    }
}
Also used : XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFMap(org.apache.poi.xssf.usermodel.XSSFMap) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Test(org.junit.Test)

Example 15 with XSSFMap

use of org.apache.poi.xssf.usermodel.XSSFMap in project poi by apache.

the class TestXSSFImportFromXML method testOptionalFields_Bugzilla_55864.

@Test
public void testOptionalFields_Bugzilla_55864() throws IOException, XPathExpressionException, SAXException {
    XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55864.xlsx");
    try {
        String testXML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + "<PersonInfoRoot>" + "<PersonData>" + "<FirstName>Albert</FirstName>" + "<LastName>Einstein</LastName>" + "<BirthDate>1879-03-14</BirthDate>" + "</PersonData>" + "</PersonInfoRoot>";
        XSSFMap map = wb.getMapInfo().getXSSFMapByName("PersonInfoRoot_Map");
        assertNotNull(map);
        XSSFImportFromXML importer = new XSSFImportFromXML(map);
        importer.importFromXML(testXML);
        XSSFSheet sheet = wb.getSheetAt(0);
        XSSFRow rowHeadings = sheet.getRow(0);
        XSSFRow rowData = sheet.getRow(1);
        assertEquals("FirstName", rowHeadings.getCell(0).getStringCellValue());
        assertEquals("Albert", rowData.getCell(0).getStringCellValue());
        assertEquals("LastName", rowHeadings.getCell(1).getStringCellValue());
        assertEquals("Einstein", rowData.getCell(1).getStringCellValue());
        assertEquals("BirthDate", rowHeadings.getCell(2).getStringCellValue());
        assertEquals("1879-03-14", rowData.getCell(2).getStringCellValue());
        // Value for OptionalRating is declared optional (minOccurs=0) in 55864.xlsx
        assertEquals("OptionalRating", rowHeadings.getCell(3).getStringCellValue());
        assertNull("", rowData.getCell(3));
    } finally {
        wb.close();
    }
}
Also used : XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) XSSFMap(org.apache.poi.xssf.usermodel.XSSFMap) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Test(org.junit.Test)

Aggregations

XSSFMap (org.apache.poi.xssf.usermodel.XSSFMap)24 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)22 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 POIXMLDocumentPart (org.apache.poi.POIXMLDocumentPart)14 MapInfo (org.apache.poi.xssf.model.MapInfo)13 XSSFSheet (org.apache.poi.xssf.usermodel.XSSFSheet)6 Test (org.junit.Test)5 XSSFRow (org.apache.poi.xssf.usermodel.XSSFRow)3 Date (java.util.Date)2 XSSFExportToXml (org.apache.poi.xssf.extractor.XSSFExportToXml)2 IOException (java.io.IOException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)1 Cell (org.apache.poi.ss.usermodel.Cell)1 Row (org.apache.poi.ss.usermodel.Row)1 Sheet (org.apache.poi.ss.usermodel.Sheet)1 XmlException (org.apache.xmlbeans.XmlException)1 CTMap (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTMap)1