Search in sources :

Example 26 with Workbook

use of org.apache.poi.ss.usermodel.Workbook in project poi by apache.

the class TestPackage method openZipBombFile.

private void openZipBombFile(String file) throws IOException, OpenXML4JException, XmlException {
    try {
        Workbook wb = XSSFTestDataSamples.openSampleWorkbook(file);
        wb.close();
        POITextExtractor extractor = ExtractorFactory.createExtractor(HSSFTestDataSamples.getSampleFile("poc-shared-strings.xlsx"));
        try {
            assertNotNull(extractor);
            extractor.getText();
        } finally {
            extractor.close();
        }
        fail("Should catch an exception because of a ZipBomb");
    } catch (IllegalStateException e) {
        if (!e.getMessage().contains("The text would exceed the max allowed overall size of extracted text.")) {
            throw e;
        }
    } catch (POIXMLException e) {
        checkForZipBombException(e);
    }
}
Also used : Workbook(org.apache.poi.ss.usermodel.Workbook)

Example 27 with Workbook

use of org.apache.poi.ss.usermodel.Workbook in project poi by apache.

the class TestSumifsXSSF method testBug60858.

/**
     * handle null cell predicate
     */
@Test
public void testBug60858() {
    Workbook wb = XSSFTestDataSamples.openSampleWorkbook("bug60858.xlsx");
    FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
    Sheet sheet = wb.getSheetAt(0);
    Cell cell = sheet.getRow(1).getCell(5);
    fe.evaluate(cell);
    assertEquals(0.0, cell.getNumericCellValue(), 0.0000000000000001);
}
Also used : Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) Workbook(org.apache.poi.ss.usermodel.Workbook) FormulaEvaluator(org.apache.poi.ss.usermodel.FormulaEvaluator) Test(org.junit.Test)

Example 28 with Workbook

use of org.apache.poi.ss.usermodel.Workbook in project poi by apache.

the class TestSXSSFCell method testPreserveSpaces.

@Test
public void testPreserveSpaces() throws IOException {
    String[] samplesWithSpaces = { " POI", "POI ", " POI ", "\nPOI", "\n\nPOI \n" };
    for (String str : samplesWithSpaces) {
        Workbook swb = _testDataProvider.createWorkbook();
        Cell sCell = swb.createSheet().createRow(0).createCell(0);
        sCell.setCellValue(str);
        assertEquals(sCell.getStringCellValue(), str);
        // read back as XSSF and check that xml:spaces="preserve" is set
        XSSFWorkbook xwb = (XSSFWorkbook) _testDataProvider.writeOutAndReadBack(swb);
        XSSFCell xCell = xwb.getSheetAt(0).getRow(0).getCell(0);
        CTRst is = xCell.getCTCell().getIs();
        XmlCursor c = is.newCursor();
        c.toNextToken();
        String t = c.getAttributeText(new QName("http://www.w3.org/XML/1998/namespace", "space"));
        c.dispose();
        assertEquals("expected xml:spaces=\"preserve\" \"" + str + "\"", "preserve", t);
        xwb.close();
        swb.close();
    }
}
Also used : CTRst(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst) QName(javax.xml.namespace.QName) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) BaseTestXCell(org.apache.poi.ss.usermodel.BaseTestXCell) Cell(org.apache.poi.ss.usermodel.Cell) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) XmlCursor(org.apache.xmlbeans.XmlCursor) Test(org.junit.Test)

Example 29 with Workbook

use of org.apache.poi.ss.usermodel.Workbook in project poi by apache.

the class TestSXSSFBugs method bug49253.

/**
     * Setting repeating rows and columns shouldn't break
     *  any print settings that were there before
     */
@Test
public void bug49253() throws Exception {
    Workbook wb1 = new SXSSFWorkbook();
    Workbook wb2 = new SXSSFWorkbook();
    CellRangeAddress cra = CellRangeAddress.valueOf("C2:D3");
    // No print settings before repeating
    Sheet s1 = wb1.createSheet();
    s1.setRepeatingColumns(cra);
    s1.setRepeatingRows(cra);
    PrintSetup ps1 = s1.getPrintSetup();
    assertEquals(false, ps1.getValidSettings());
    assertEquals(false, ps1.getLandscape());
    // Had valid print settings before repeating
    Sheet s2 = wb2.createSheet();
    PrintSetup ps2 = s2.getPrintSetup();
    ps2.setLandscape(false);
    assertEquals(true, ps2.getValidSettings());
    assertEquals(false, ps2.getLandscape());
    s2.setRepeatingColumns(cra);
    s2.setRepeatingRows(cra);
    ps2 = s2.getPrintSetup();
    assertEquals(true, ps2.getValidSettings());
    assertEquals(false, ps2.getLandscape());
    wb1.close();
    wb2.close();
}
Also used : SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) PrintSetup(org.apache.poi.ss.usermodel.PrintSetup) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) Sheet(org.apache.poi.ss.usermodel.Sheet) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) Test(org.junit.Test)

Example 30 with Workbook

use of org.apache.poi.ss.usermodel.Workbook in project poi by apache.

the class TestSXSSFWorkbook method bug53515.

@Ignore("currently writing the same sheet multiple times is not supported...")
@Test
public void bug53515() throws Exception {
    Workbook wb1 = new SXSSFWorkbook(10);
    populateWorkbook(wb1);
    saveTwice(wb1);
    Workbook wb2 = new XSSFWorkbook();
    populateWorkbook(wb2);
    saveTwice(wb2);
    wb2.close();
    wb1.close();
}
Also used : XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) BaseTestXWorkbook(org.apache.poi.ss.usermodel.BaseTestXWorkbook) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

Workbook (org.apache.poi.ss.usermodel.Workbook)319 Sheet (org.apache.poi.ss.usermodel.Sheet)224 Test (org.junit.Test)207 Cell (org.apache.poi.ss.usermodel.Cell)140 Row (org.apache.poi.ss.usermodel.Row)123 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)104 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)95 FileOutputStream (java.io.FileOutputStream)33 XSSFChart (org.apache.poi.xssf.usermodel.XSSFChart)32 ByteArrayInputStream (java.io.ByteArrayInputStream)30 FormulaEvaluator (org.apache.poi.ss.usermodel.FormulaEvaluator)30 XSSFDrawing (org.apache.poi.xssf.usermodel.XSSFDrawing)27 CTChart (org.openxmlformats.schemas.drawingml.x2006.chart.CTChart)27 File (java.io.File)26 CellStyle (org.apache.poi.ss.usermodel.CellStyle)25 InternalWorkbook (org.apache.poi.hssf.model.InternalWorkbook)24 LangYearFilterPagingRequest (org.devgateway.ocds.web.rest.controller.request.LangYearFilterPagingRequest)23 IOException (java.io.IOException)22 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)18 FileInputStream (java.io.FileInputStream)15