Search in sources :

Example 41 with Workbook

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

the class TestXSSFFormulaEvaluation method testMultiSheetReferencesHSSFandXSSF.

/**
     * A handful of functions (such as SUM, COUNTA, MIN) support
     *  multi-sheet references (eg Sheet1:Sheet3!A1 = Cell A1 from
     *  Sheets 1 through Sheet 3).
     * This test, based on common test files for HSSF and XSSF, checks
     *  that we can correctly evaluate these
     */
@Test
public void testMultiSheetReferencesHSSFandXSSF() throws Exception {
    Workbook wb1 = HSSFTestDataSamples.openSampleWorkbook("55906-MultiSheetRefs.xls");
    Workbook wb2 = XSSFTestDataSamples.openSampleWorkbook("55906-MultiSheetRefs.xlsx");
    for (Workbook wb : new Workbook[] { wb1, wb2 }) {
        FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
        Sheet s1 = wb.getSheetAt(0);
        // Simple SUM over numbers
        Cell sumF = s1.getRow(2).getCell(0);
        assertNotNull(sumF);
        assertEquals("SUM(Sheet1:Sheet3!A1)", sumF.getCellFormula());
        assertEquals("Failed for " + wb.getClass(), "66.0", evaluator.evaluate(sumF).formatAsString());
        // Various Stats formulas on numbers
        Cell avgF = s1.getRow(2).getCell(1);
        assertNotNull(avgF);
        assertEquals("AVERAGE(Sheet1:Sheet3!A1)", avgF.getCellFormula());
        assertEquals("22.0", evaluator.evaluate(avgF).formatAsString());
        Cell minF = s1.getRow(3).getCell(1);
        assertNotNull(minF);
        assertEquals("MIN(Sheet1:Sheet3!A$1)", minF.getCellFormula());
        assertEquals("11.0", evaluator.evaluate(minF).formatAsString());
        Cell maxF = s1.getRow(4).getCell(1);
        assertNotNull(maxF);
        assertEquals("MAX(Sheet1:Sheet3!A$1)", maxF.getCellFormula());
        assertEquals("33.0", evaluator.evaluate(maxF).formatAsString());
        Cell countF = s1.getRow(5).getCell(1);
        assertNotNull(countF);
        assertEquals("COUNT(Sheet1:Sheet3!A$1)", countF.getCellFormula());
        assertEquals("3.0", evaluator.evaluate(countF).formatAsString());
        // Various CountAs on Strings
        Cell countA_1F = s1.getRow(2).getCell(2);
        assertNotNull(countA_1F);
        assertEquals("COUNTA(Sheet1:Sheet3!C1)", countA_1F.getCellFormula());
        assertEquals("3.0", evaluator.evaluate(countA_1F).formatAsString());
        Cell countA_2F = s1.getRow(2).getCell(3);
        assertNotNull(countA_2F);
        assertEquals("COUNTA(Sheet1:Sheet3!D1)", countA_2F.getCellFormula());
        assertEquals("0.0", evaluator.evaluate(countA_2F).formatAsString());
        Cell countA_3F = s1.getRow(2).getCell(4);
        assertNotNull(countA_3F);
        assertEquals("COUNTA(Sheet1:Sheet3!E1)", countA_3F.getCellFormula());
        assertEquals("3.0", evaluator.evaluate(countA_3F).formatAsString());
    }
    wb2.close();
    wb1.close();
}
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) BaseTestFormulaEvaluator(org.apache.poi.ss.usermodel.BaseTestFormulaEvaluator) Test(org.junit.Test)

Example 42 with Workbook

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

the class TestXSSFFormulaEvaluation method test59736.

@Test
public void test59736() {
    Workbook wb = XSSFTestDataSamples.openSampleWorkbook("59736.xlsx");
    FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
    Cell cell = wb.getSheetAt(0).getRow(0).getCell(0);
    assertEquals(1, cell.getNumericCellValue(), 0.001);
    cell = wb.getSheetAt(0).getRow(1).getCell(0);
    CellValue value = evaluator.evaluate(cell);
    assertEquals(1, value.getNumberValue(), 0.001);
    cell = wb.getSheetAt(0).getRow(2).getCell(0);
    value = evaluator.evaluate(cell);
    assertEquals(1, value.getNumberValue(), 0.001);
}
Also used : CellValue(org.apache.poi.ss.usermodel.CellValue) Cell(org.apache.poi.ss.usermodel.Cell) Workbook(org.apache.poi.ss.usermodel.Workbook) FormulaEvaluator(org.apache.poi.ss.usermodel.FormulaEvaluator) BaseTestFormulaEvaluator(org.apache.poi.ss.usermodel.BaseTestFormulaEvaluator) Test(org.junit.Test)

Example 43 with Workbook

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

the class TestXSSFWorkbook method bug47090b.

@Test
public void bug47090b() throws IOException {
    Workbook workbook = XSSFTestDataSamples.openSampleWorkbook("47090.xlsx");
    assertSheetOrder(workbook, "Sheet1", "Sheet2");
    workbook.removeSheetAt(1);
    assertSheetOrder(workbook, "Sheet1");
    workbook.createSheet();
    // Sheet0 because it uses "Sheet" + sheets.size() as starting point!
    assertSheetOrder(workbook, "Sheet1", "Sheet0");
    Workbook read = XSSFTestDataSamples.writeOutAndReadBack(workbook);
    assertSheetOrder(read, "Sheet1", "Sheet0");
    read.close();
    workbook.close();
}
Also used : BaseTestXWorkbook(org.apache.poi.ss.usermodel.BaseTestXWorkbook) CTWorkbook(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) Test(org.junit.Test)

Example 44 with Workbook

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

the class TestXSSFChartTitle method createWorkbookWithChart.

private Workbook createWorkbookWithChart() {
    Workbook wb = new XSSFWorkbook();
    Sheet sheet = wb.createSheet("linechart");
    final int NUM_OF_ROWS = 3;
    final int NUM_OF_COLUMNS = 10;
    // Create a row and put some cells in it. Rows are 0 based.
    Row row;
    Cell cell;
    for (int rowIndex = 0; rowIndex < NUM_OF_ROWS; rowIndex++) {
        row = sheet.createRow((short) rowIndex);
        for (int colIndex = 0; colIndex < NUM_OF_COLUMNS; colIndex++) {
            cell = row.createCell((short) colIndex);
            cell.setCellValue(colIndex * (rowIndex + 1));
        }
    }
    Drawing<?> drawing = sheet.createDrawingPatriarch();
    ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 10, 15);
    Chart chart = drawing.createChart(anchor);
    ChartLegend legend = chart.getOrCreateLegend();
    legend.setPosition(LegendPosition.TOP_RIGHT);
    LineChartData data = chart.getChartDataFactory().createLineChartData();
    // Use a category axis for the bottom axis.
    ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);
    ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);
    leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
    ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(0, 0, 0, NUM_OF_COLUMNS - 1));
    ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 1, 0, NUM_OF_COLUMNS - 1));
    ChartDataSource<Number> ys2 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(2, 2, 0, NUM_OF_COLUMNS - 1));
    data.addSeries(xs, ys1);
    data.addSeries(xs, ys2);
    chart.plot(data, bottomAxis, leftAxis);
    return wb;
}
Also used : ChartLegend(org.apache.poi.ss.usermodel.charts.ChartLegend) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) ClientAnchor(org.apache.poi.ss.usermodel.ClientAnchor) ChartAxis(org.apache.poi.ss.usermodel.charts.ChartAxis) ValueAxis(org.apache.poi.ss.usermodel.charts.ValueAxis) LineChartData(org.apache.poi.ss.usermodel.charts.LineChartData) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Row(org.apache.poi.ss.usermodel.Row) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) Sheet(org.apache.poi.ss.usermodel.Sheet) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) Cell(org.apache.poi.ss.usermodel.Cell) Chart(org.apache.poi.ss.usermodel.Chart) XSSFChart(org.apache.poi.xssf.usermodel.XSSFChart)

Example 45 with Workbook

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

the class TestXSSFChartTitle method testExistingChartWithFormulaTitle.

@Test
public void testExistingChartWithFormulaTitle() throws IOException {
    Workbook wb = XSSFTestDataSamples.openSampleWorkbook("chartTitle_withTitleFormula.xlsx");
    XSSFChart chart = getChartFromWorkbook(wb, "Sheet1");
    assertNotNull(chart);
    XSSFRichTextString originalTitle = chart.getTitleText();
    assertNotNull(originalTitle);
    assertEquals("", originalTitle.toString());
    String formula = chart.getTitleFormula();
    assertNotNull(formula);
    assertEquals("Sheet1!$E$1", formula);
    wb.close();
}
Also used : XSSFChart(org.apache.poi.xssf.usermodel.XSSFChart) XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) 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