Search in sources :

Example 1 with XSSFMap

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

the class CustomXMLMapping method main.

public static void main(String[] args) throws Exception {
    OPCPackage pkg = OPCPackage.open(args[0]);
    XSSFWorkbook wb = new XSSFWorkbook(pkg);
    for (XSSFMap map : wb.getCustomXMLMappings()) {
        XSSFExportToXml exporter = new XSSFExportToXml(map);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        exporter.exportToXML(os, true);
        String xml = os.toString("UTF-8");
        System.out.println(xml);
    }
    pkg.close();
}
Also used : XSSFMap(org.apache.poi.xssf.usermodel.XSSFMap) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFExportToXml(org.apache.poi.xssf.extractor.XSSFExportToXml) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage)

Example 2 with XSSFMap

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

the class TestMapInfo method testMapInfoExists.

public void testMapInfoExists() {
    XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMappings.xlsx");
    MapInfo mapInfo = null;
    SingleXmlCells singleXMLCells = null;
    for (POIXMLDocumentPart p : wb.getRelations()) {
        if (p instanceof MapInfo) {
            mapInfo = (MapInfo) p;
            CTMapInfo ctMapInfo = mapInfo.getCTMapInfo();
            assertNotNull(ctMapInfo);
            assertEquals(1, ctMapInfo.sizeOfSchemaArray());
            for (XSSFMap map : mapInfo.getAllXSSFMaps()) {
                Node xmlSchema = map.getSchema();
                assertNotNull(xmlSchema);
            }
        }
    }
    XSSFSheet sheet1 = wb.getSheetAt(0);
    for (POIXMLDocumentPart p : sheet1.getRelations()) {
        if (p instanceof SingleXmlCells) {
            singleXMLCells = (SingleXmlCells) p;
        }
    }
    assertNotNull(mapInfo);
    assertNotNull(singleXMLCells);
}
Also used : XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) POIXMLDocumentPart(org.apache.poi.POIXMLDocumentPart) CTMapInfo(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTMapInfo) XSSFMap(org.apache.poi.xssf.usermodel.XSSFMap) Node(org.w3c.dom.Node) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) CTMapInfo(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTMapInfo)

Example 3 with XSSFMap

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

the class TestXSSFImportFromXML method testImportFromXML.

@Test
public void testImportFromXML() throws IOException, XPathExpressionException, SAXException {
    XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMappings.xlsx");
    try {
        String name = "name";
        String teacher = "teacher";
        String tutor = "tutor";
        String cdl = "cdl";
        String duration = "duration";
        String topic = "topic";
        String project = "project";
        String credits = "credits";
        String testXML = "<CORSO>" + "<NOME>" + name + "</NOME>" + "<DOCENTE>" + teacher + "</DOCENTE>" + "<TUTOR>" + tutor + "</TUTOR>" + "<CDL>" + cdl + "</CDL>" + "<DURATA>" + duration + "</DURATA>" + "<ARGOMENTO>" + topic + "</ARGOMENTO>" + "<PROGETTO>" + project + "</PROGETTO>" + "<CREDITI>" + credits + "</CREDITI>" + "</CORSO>";
        XSSFMap map = wb.getMapInfo().getXSSFMapByName("CORSO_mapping");
        assertNotNull(map);
        XSSFImportFromXML importer = new XSSFImportFromXML(map);
        importer.importFromXML(testXML);
        XSSFSheet sheet = wb.getSheetAt(0);
        XSSFRow row = sheet.getRow(0);
        assertTrue(row.getCell(0).getStringCellValue().equals(name));
        assertTrue(row.getCell(1).getStringCellValue().equals(teacher));
        assertTrue(row.getCell(2).getStringCellValue().equals(tutor));
        assertTrue(row.getCell(3).getStringCellValue().equals(cdl));
        assertTrue(row.getCell(4).getStringCellValue().equals(duration));
        assertTrue(row.getCell(5).getStringCellValue().equals(topic));
        assertTrue(row.getCell(6).getStringCellValue().equals(project));
        assertTrue(row.getCell(7).getStringCellValue().equals(credits));
    } 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)

Example 4 with XSSFMap

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

the class TestXSSFExportToXML method testXmlExportSchemaOrderingBug_Bugzilla_55923.

public void testXmlExportSchemaOrderingBug_Bugzilla_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);
        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 a_b = a.split("<B>")[1].split("</B>")[0].trim();
        String a_b_c = a_b.split("<C>")[1].split("</C>")[0].trim();
        String a_b_c_e = a_b_c.split("<E>")[1].split("</EA>")[0].trim();
        String a_b_c_e_euro = a_b_c_e.split("<EUR>")[1].split("</EUR>")[0].trim();
        String a_b_c_e_chf = a_b_c_e.split("<CHF>")[1].split("</CHF>")[0].trim();
        assertEquals("1", a_b_c_e_euro);
        assertEquals("2", a_b_c_e_chf);
        String a_b_d = a_b.split("<D>")[1].split("</Dd>")[0].trim();
        String a_b_d_e = a_b_d.split("<E>")[1].split("</EA>")[0].trim();
        String a_b_d_e_euro = a_b_d_e.split("<EUR>")[1].split("</EUR>")[0].trim();
        String a_b_d_e_chf = a_b_d_e.split("<CHF>")[1].split("</CHF>")[0].trim();
        assertEquals("3", a_b_d_e_euro);
        assertEquals("4", a_b_d_e_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) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 5 with XSSFMap

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

the class TestXSSFExportToXML method testXmlExportSchemaWithXSAllTag_Bugzilla_56169.

public void testXmlExportSchemaWithXSAllTag_Bugzilla_56169() throws Exception {
    XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("56169.xlsx");
    for (XSSFMap map : wb.getCustomXMLMappings()) {
        XSSFExportToXml exporter = new XSSFExportToXml(map);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        exporter.exportToXML(os, true);
        String xmlData = os.toString("UTF-8");
        assertNotNull(xmlData);
        assertTrue(!xmlData.equals(""));
        String a = xmlData.split("<A>")[1].split("</A>")[0].trim();
        String a_b = a.split("<B>")[1].split("</B>")[0].trim();
        String a_b_c = a_b.split("<C>")[1].split("</C>")[0].trim();
        String a_b_c_e = a_b_c.split("<E>")[1].split("</EA>")[0].trim();
        String a_b_c_e_euro = a_b_c_e.split("<EUR>")[1].split("</EUR>")[0].trim();
        String a_b_c_e_chf = a_b_c_e.split("<CHF>")[1].split("</CHF>")[0].trim();
        assertEquals("1", a_b_c_e_euro);
        assertEquals("2", a_b_c_e_chf);
        String a_b_d = a_b.split("<D>")[1].split("</Dd>")[0].trim();
        String a_b_d_e = a_b_d.split("<E>")[1].split("</EA>")[0].trim();
        String a_b_d_e_euro = a_b_d_e.split("<EUR>")[1].split("</EUR>")[0].trim();
        String a_b_d_e_chf = a_b_d_e.split("<CHF>")[1].split("</CHF>")[0].trim();
        assertEquals("3", a_b_d_e_euro);
        assertEquals("4", a_b_d_e_chf);
    }
}
Also used : XSSFMap(org.apache.poi.xssf.usermodel.XSSFMap) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

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