Search in sources :

Example 76 with Workbook

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

the class TestCellWalk method testNotTraverseEmptyCells.

public void testNotTraverseEmptyCells() {
    Workbook wb = new HSSFWorkbook();
    Sheet sheet = new SheetBuilder(wb, testData).build();
    CellRangeAddress range = CellRangeAddress.valueOf("A1:C3");
    CellWalk cellWalk = new CellWalk(sheet, range);
    countCellHandler.reset();
    cellWalk.traverse(countCellHandler);
    assertEquals(4, countCellHandler.getVisitedCellsNumber());
    /* 1 + 2 + 5 + 9 */
    assertEquals(17L, countCellHandler.getOrdinalNumberSum());
}
Also used : SheetBuilder(org.apache.poi.ss.util.SheetBuilder) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) Sheet(org.apache.poi.ss.usermodel.Sheet) Workbook(org.apache.poi.ss.usermodel.Workbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 77 with Workbook

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

the class TestSheetUtil method testCellWithMerges.

public void testCellWithMerges() throws Exception {
    Workbook wb = new HSSFWorkbook();
    Sheet s = wb.createSheet();
    // Create some test data
    Row r2 = s.createRow(1);
    r2.createCell(0).setCellValue(10);
    r2.createCell(1).setCellValue(11);
    Row r3 = s.createRow(2);
    r3.createCell(0).setCellValue(20);
    r3.createCell(1).setCellValue(21);
    s.addMergedRegion(new CellRangeAddress(2, 3, 0, 0));
    s.addMergedRegion(new CellRangeAddress(2, 2, 1, 4));
    // With a cell that isn't defined, we'll get null
    assertEquals(null, SheetUtil.getCellWithMerges(s, 0, 0));
    // With a cell that's not in a merged region, we'll get that
    assertEquals(10.0, SheetUtil.getCellWithMerges(s, 1, 0).getNumericCellValue());
    assertEquals(11.0, SheetUtil.getCellWithMerges(s, 1, 1).getNumericCellValue());
    // With a cell that's the primary one of a merged region, we get that cell
    assertEquals(20.0, SheetUtil.getCellWithMerges(s, 2, 0).getNumericCellValue());
    assertEquals(21., SheetUtil.getCellWithMerges(s, 2, 1).getNumericCellValue());
    // With a cell elsewhere in the merged region, get top-left
    assertEquals(20.0, SheetUtil.getCellWithMerges(s, 3, 0).getNumericCellValue());
    assertEquals(21.0, SheetUtil.getCellWithMerges(s, 2, 2).getNumericCellValue());
    assertEquals(21.0, SheetUtil.getCellWithMerges(s, 2, 3).getNumericCellValue());
    assertEquals(21.0, SheetUtil.getCellWithMerges(s, 2, 4).getNumericCellValue());
    wb.close();
}
Also used : Row(org.apache.poi.ss.usermodel.Row) Sheet(org.apache.poi.ss.usermodel.Sheet) Workbook(org.apache.poi.ss.usermodel.Workbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 78 with Workbook

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

the class TestBugs method bug56737.

/**
     * Formulas which reference named ranges, either in other
     *  sheets, or workbook scoped but in other workbooks.
     * Used to fail with
     * java.lang.RuntimeException: Unexpected eval class (org.apache.poi.ss.formula.eval.NameXEval)
     */
@Test
public void bug56737() throws IOException {
    Workbook wb = openSample("56737.xls");
    // Check the named range definitions
    Name nSheetScope = wb.getName("NR_To_A1");
    Name nWBScope = wb.getName("NR_Global_B2");
    assertNotNull(nSheetScope);
    assertNotNull(nWBScope);
    assertEquals("Defines!$A$1", nSheetScope.getRefersToFormula());
    assertEquals("Defines!$B$2", nWBScope.getRefersToFormula());
    // Check the different kinds of formulas
    Sheet s = wb.getSheetAt(0);
    Cell cRefSName = s.getRow(1).getCell(3);
    Cell cRefWName = s.getRow(2).getCell(3);
    assertEquals("Defines!NR_To_A1", cRefSName.getCellFormula());
    // TODO Correct this, so that the filename is shown too, see bug #56742
    // This is what Excel itself shows
    //assertEquals("'56737.xls'!NR_Global_B2", cRefWName.getCellFormula());
    // TODO This isn't right, but it's what we currently generate....
    assertEquals("NR_Global_B2", cRefWName.getCellFormula());
    // Try to evaluate them
    FormulaEvaluator eval = wb.getCreationHelper().createFormulaEvaluator();
    assertEquals("Test A1", eval.evaluate(cRefSName).getStringValue());
    assertEquals(142, (int) eval.evaluate(cRefWName).getNumberValue());
    // Try to evaluate everything
    eval.evaluateAll();
    wb.close();
}
Also used : InternalSheet(org.apache.poi.hssf.model.InternalSheet) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) InternalWorkbook(org.apache.poi.hssf.model.InternalWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) Name(org.apache.poi.ss.usermodel.Name) FormulaEvaluator(org.apache.poi.ss.usermodel.FormulaEvaluator) Test(org.junit.Test)

Example 79 with Workbook

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

the class TestBugs method test57163.

@Test
public void test57163() throws IOException {
    Workbook wb = openSample("57163.xls");
    while (wb.getNumberOfSheets() > 1) {
        wb.removeSheetAt(1);
    }
    wb.close();
}
Also used : InternalWorkbook(org.apache.poi.hssf.model.InternalWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) Test(org.junit.Test)

Example 80 with Workbook

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

the class TestBugs method test57925.

@Test
public void test57925() throws IOException {
    Workbook wb = HSSFTestDataSamples.openSampleWorkbook("57925.xls");
    wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
    for (int i = 0; i < wb.getNumberOfSheets(); i++) {
        Sheet sheet = wb.getSheetAt(i);
        for (Row row : sheet) {
            for (Cell cell : row) {
                new DataFormatter().formatCellValue(cell);
            }
        }
    }
    wb.close();
}
Also used : Row(org.apache.poi.ss.usermodel.Row) InternalSheet(org.apache.poi.hssf.model.InternalSheet) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) InternalWorkbook(org.apache.poi.hssf.model.InternalWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) DataFormatter(org.apache.poi.ss.usermodel.DataFormatter) 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