Search in sources :

Example 76 with XSSFWorkbook

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

the class TestXSSFExportToXML method testFormulaCells_Bugzilla_55927.

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("<DATE>")[1].split("</DATE>")[0].trim());
        assertEquals("2012-02-16", xmlData.split("<FORMULA_DATE>")[1].split("</FORMULA_DATE>")[0].trim());
        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 77 with XSSFWorkbook

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

the class TestXSSFExportToXML method testBug59026.

public void testBug59026() throws Exception {
    XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("59026.xlsx");
    Collection<XSSFMap> mappings = wb.getCustomXMLMappings();
    assertTrue(mappings.size() > 0);
    for (XSSFMap map : mappings) {
        XSSFExportToXml exporter = new XSSFExportToXml(map);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        exporter.exportToXML(os, false);
        assertNotNull(os.toString("UTF-8"));
    }
}
Also used : XSSFMap(org.apache.poi.xssf.usermodel.XSSFMap) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 78 with XSSFWorkbook

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

the class TestXSSFImportFromXML method testOptionalFields_Bugzilla_57890.

@Test
public void testOptionalFields_Bugzilla_57890() throws IOException, ParseException, XPathExpressionException, SAXException {
    XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("57890.xlsx");
    String testXML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + "<TestInfoRoot>" + "<TestData>" + "<Int>" + Integer.MIN_VALUE + "</Int>" + "<UnsignedInt>12345</UnsignedInt>" + "<double>1.0000123</double>" + "<Date>1991-03-14</Date>" + "</TestData>" + "</TestInfoRoot>";
    XSSFMap map = wb.getMapInfo().getXSSFMapByName("TestInfoRoot_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("Date", rowHeadings.getCell(0).getStringCellValue());
    Date date = new SimpleDateFormat("yyyy-MM-dd", DateFormatSymbols.getInstance(Locale.ROOT)).parse("1991-3-14");
    assertEquals(date, rowData.getCell(0).getDateCellValue());
    assertEquals("Amount Int", rowHeadings.getCell(1).getStringCellValue());
    assertEquals(new Double(Integer.MIN_VALUE), rowData.getCell(1).getNumericCellValue(), 0);
    assertEquals("Amount Double", rowHeadings.getCell(2).getStringCellValue());
    assertEquals(1.0000123, rowData.getCell(2).getNumericCellValue(), 0);
    assertEquals("Amount UnsignedInt", rowHeadings.getCell(3).getStringCellValue());
    assertEquals(new Double(12345), rowData.getCell(3).getNumericCellValue(), 0);
    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) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 79 with XSSFWorkbook

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

the class TestXSSFExportToXML method testExportToXML.

public void testExportToXML() throws Exception {
    XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("CustomXMLMappings.xlsx");
    boolean found = false;
    for (POIXMLDocumentPart p : wb.getRelations()) {
        if (!(p instanceof MapInfo)) {
            continue;
        }
        MapInfo mapInfo = (MapInfo) p;
        XSSFMap map = mapInfo.getXSSFMapById(1);
        XSSFExportToXml exporter = new XSSFExportToXml(map);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        exporter.exportToXML(os, true);
        String xml = os.toString("UTF-8");
        assertNotNull(xml);
        assertFalse(xml.equals(""));
        String docente = xml.split("<DOCENTE>")[1].split("</DOCENTE>")[0].trim();
        String nome = xml.split("<NOME>")[1].split("</NOME>")[0].trim();
        String tutor = xml.split("<TUTOR>")[1].split("</TUTOR>")[0].trim();
        String cdl = xml.split("<CDL>")[1].split("</CDL>")[0].trim();
        String durata = xml.split("<DURATA>")[1].split("</DURATA>")[0].trim();
        String argomento = xml.split("<ARGOMENTO>")[1].split("</ARGOMENTO>")[0].trim();
        String progetto = xml.split("<PROGETTO>")[1].split("</PROGETTO>")[0].trim();
        String crediti = xml.split("<CREDITI>")[1].split("</CREDITI>")[0].trim();
        assertEquals("ro", docente);
        assertEquals("ro", nome);
        assertEquals("ds", tutor);
        assertEquals("gs", cdl);
        assertEquals("g", durata);
        assertEquals("gvvv", argomento);
        assertEquals("aaaa", progetto);
        assertEquals("aa", crediti);
        parseXML(xml);
        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 80 with XSSFWorkbook

use of org.apache.poi.xssf.usermodel.XSSFWorkbook 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)

Aggregations

XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)334 Workbook (org.apache.poi.ss.usermodel.Workbook)131 Sheet (org.apache.poi.ss.usermodel.Sheet)119 Test (org.junit.Test)108 XSSFSheet (org.apache.poi.xssf.usermodel.XSSFSheet)82 FileOutputStream (java.io.FileOutputStream)81 Row (org.apache.poi.ss.usermodel.Row)74 Cell (org.apache.poi.ss.usermodel.Cell)68 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)66 IOException (java.io.IOException)65 FileInputStream (java.io.FileInputStream)63 ArrayList (java.util.ArrayList)51 File (java.io.File)46 ByteArrayInputStream (java.io.ByteArrayInputStream)36 XSSFRow (org.apache.poi.xssf.usermodel.XSSFRow)35 FileNotFoundException (java.io.FileNotFoundException)29 CTChart (org.openxmlformats.schemas.drawingml.x2006.chart.CTChart)27 CellStyle (org.apache.poi.ss.usermodel.CellStyle)26 XSSFChart (org.apache.poi.xssf.usermodel.XSSFChart)26 XSSFDrawing (org.apache.poi.xssf.usermodel.XSSFDrawing)25