Search in sources :

Example 21 with XSSFMap

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

the class TestXSSFImportFromXML method testSingleAttributeCellWithNamespace.

@Test
public void testSingleAttributeCellWithNamespace() throws IOException, XPathExpressionException, SAXException {
    XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMapping-singleattributenamespace.xlsx");
    try {
        int id = 1;
        String displayName = "dispName";
        String ref = "19";
        int count = 21;
        String testXML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>" + "<ns1:table xmlns:ns1=\"" + NS_SPREADSHEETML + "\" id=\"" + id + "\" displayName=\"" + displayName + "\" ref=\"" + ref + "\">" + "<ns1:tableColumns count=\"" + count + "\" />" + "</ns1:table>";
        XSSFMap map = wb.getMapInfo().getXSSFMapByName("table_mapping");
        assertNotNull(map);
        XSSFImportFromXML importer = new XSSFImportFromXML(map);
        importer.importFromXML(testXML);
        //Check for Schema element
        XSSFSheet sheet = wb.getSheetAt(0);
        assertEquals(new Double(id), sheet.getRow(28).getCell(1).getNumericCellValue(), 0);
        assertEquals(displayName, sheet.getRow(11).getCell(5).getStringCellValue());
        assertEquals(ref, sheet.getRow(14).getCell(7).getStringCellValue());
        assertEquals(new Double(count), sheet.getRow(18).getCell(3).getNumericCellValue(), 0);
    } 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 22 with XSSFMap

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

the class TestXSSFExportToXML method testExportDataTypes.

public void testExportDataTypes() throws Exception {
    XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55923.xlsx");
    Sheet sheet = wb.getSheetAt(0);
    Row row = sheet.getRow(0);
    Cell cString = row.createCell(0);
    cString.setCellValue("somestring");
    cString.setCellType(CellType.STRING);
    Cell cBoolean = row.createCell(1);
    cBoolean.setCellValue(true);
    cBoolean.setCellType(CellType.BOOLEAN);
    Cell cError = row.createCell(2);
    cError.setCellType(CellType.ERROR);
    Cell cFormulaString = row.createCell(3);
    cFormulaString.setCellFormula("A1");
    cFormulaString.setCellType(CellType.FORMULA);
    Cell cFormulaNumeric = row.createCell(4);
    cFormulaNumeric.setCellFormula("F1");
    cFormulaNumeric.setCellType(CellType.FORMULA);
    Cell cNumeric = row.createCell(5);
    cNumeric.setCellValue(1.2);
    cNumeric.setCellType(CellType.NUMERIC);
    Cell cDate = row.createCell(6);
    cDate.setCellValue(new Date());
    cDate.setCellType(CellType.NUMERIC);
    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(""));
        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) Row(org.apache.poi.ss.usermodel.Row) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) Date(java.util.Date)

Example 23 with XSSFMap

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

the class TestXSSFExportToXML method test55850ComplexXmlExport.

public void test55850ComplexXmlExport() throws Exception {
    XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("55850.xlsx");
    boolean found = false;
    for (POIXMLDocumentPart p : wb.getRelations()) {
        if (!(p instanceof MapInfo)) {
            continue;
        }
        MapInfo mapInfo = (MapInfo) p;
        XSSFMap map = mapInfo.getXSSFMapById(2);
        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 b = a.split("<B>")[1].split("</B>")[0].trim();
        String c = b.split("<C>")[1].split("</C>")[0].trim();
        String d = c.split("<D>")[1].split("</Dd>")[0].trim();
        String e = d.split("<E>")[1].split("</EA>")[0].trim();
        String euro = e.split("<EUR>")[1].split("</EUR>")[0].trim();
        String chf = e.split("<CHF>")[1].split("</CHF>")[0].trim();
        assertEquals("15", euro);
        assertEquals("19", chf);
        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 24 with XSSFMap

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

the class TestXSSFExportToXML method testValidateFalse.

public void testValidateFalse() 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, false);
        String xmlData = os.toString("UTF-8");
        assertNotNull(xmlData);
        assertFalse(xmlData.equals(""));
        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)

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