Search in sources :

Example 91 with HSSFWorkbook

use of org.apache.poi.hssf.usermodel.HSSFWorkbook in project poi by apache.

the class TestTime method setUp.

@Before
public void setUp() {
    wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");
    style = wb.createCellStyle();
    HSSFDataFormat fmt = wb.createDataFormat();
    style.setDataFormat(fmt.getFormat("hh:mm:ss"));
    cell11 = sheet.createRow(0).createCell(0);
    form = new HSSFDataFormatter();
    evaluator = new HSSFFormulaEvaluator(wb);
}
Also used : HSSFFormulaEvaluator(org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator) HSSFDataFormat(org.apache.poi.hssf.usermodel.HSSFDataFormat) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFDataFormatter(org.apache.poi.hssf.usermodel.HSSFDataFormatter) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Before(org.junit.Before)

Example 92 with HSSFWorkbook

use of org.apache.poi.hssf.usermodel.HSSFWorkbook in project poi by apache.

the class TestSubtotal method testMin.

@Test
public void testMin() throws IOException {
    Workbook wb = new HSSFWorkbook();
    FormulaEvaluator fe = wb.getCreationHelper().createFormulaEvaluator();
    Sheet sh = wb.createSheet();
    Cell a1 = sh.createRow(1).createCell(1);
    a1.setCellValue(1);
    Cell a2 = sh.createRow(2).createCell(1);
    a2.setCellValue(3);
    Cell a3 = sh.createRow(3).createCell(1);
    a3.setCellFormula("SUBTOTAL(5,B2:B3)");
    Cell a4 = sh.createRow(4).createCell(1);
    a4.setCellValue(1);
    Cell a5 = sh.createRow(5).createCell(1);
    a5.setCellValue(7);
    Cell a6 = sh.createRow(6).createCell(1);
    a6.setCellFormula("SUBTOTAL(5,B2:B6)*2 + 2");
    Cell a7 = sh.createRow(7).createCell(1);
    a7.setCellFormula("SUBTOTAL(5,B2:B7)");
    Cell a8 = sh.createRow(8).createCell(1);
    a8.setCellFormula("SUBTOTAL(5,B2,B3,B4,B5,B6,B7,B8)");
    fe.evaluateAll();
    assertEquals(1.0, a3.getNumericCellValue(), 0);
    assertEquals(4.0, a6.getNumericCellValue(), 0);
    assertEquals(1.0, a7.getNumericCellValue(), 0);
    assertEquals(1.0, a8.getNumericCellValue(), 0);
    wb.close();
}
Also used : Sheet(org.apache.poi.ss.usermodel.Sheet) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) Cell(org.apache.poi.ss.usermodel.Cell) Workbook(org.apache.poi.ss.usermodel.Workbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) FormulaEvaluator(org.apache.poi.ss.usermodel.FormulaEvaluator) Test(org.junit.Test)

Example 93 with HSSFWorkbook

use of org.apache.poi.hssf.usermodel.HSSFWorkbook in project poi by apache.

the class TestRank method testFromFile.

public void testFromFile() {
    HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("rank.xls");
    HSSFFormulaEvaluator fe = new HSSFFormulaEvaluator(wb);
    HSSFSheet example1 = wb.getSheet("Example 1");
    HSSFCell ex1cell1 = example1.getRow(7).getCell(0);
    assertEquals(3.0, fe.evaluate(ex1cell1).getNumberValue());
    HSSFCell ex1cell2 = example1.getRow(8).getCell(0);
    assertEquals(5.0, fe.evaluate(ex1cell2).getNumberValue());
    HSSFSheet example2 = wb.getSheet("Example 2");
    for (int rownum = 1; rownum <= 10; rownum++) {
        HSSFCell cell = example2.getRow(rownum).getCell(2);
        //cached formula result
        double cachedResult = cell.getNumericCellValue();
        assertEquals(cachedResult, fe.evaluate(cell).getNumberValue());
    }
    HSSFSheet example3 = wb.getSheet("Example 3");
    for (int rownum = 1; rownum <= 10; rownum++) {
        HSSFCell cellD = example3.getRow(rownum).getCell(3);
        //cached formula result
        double cachedResultD = cellD.getNumericCellValue();
        assertEquals(new CellReference(cellD).formatAsString(), cachedResultD, fe.evaluate(cellD).getNumberValue());
        HSSFCell cellE = example3.getRow(rownum).getCell(4);
        //cached formula result
        double cachedResultE = cellE.getNumericCellValue();
        assertEquals(new CellReference(cellE).formatAsString(), cachedResultE, fe.evaluate(cellE).getNumberValue());
        HSSFCell cellF = example3.getRow(rownum).getCell(5);
        //cached formula result
        double cachedResultF = cellF.getNumericCellValue();
        assertEquals(new CellReference(cellF).formatAsString(), cachedResultF, fe.evaluate(cellF).getNumberValue());
    }
}
Also used : HSSFFormulaEvaluator(org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) CellReference(org.apache.poi.ss.util.CellReference) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 94 with HSSFWorkbook

use of org.apache.poi.hssf.usermodel.HSSFWorkbook in project poi by apache.

the class TestExternalNameReference method testEvaluate.

public void testEvaluate() throws Exception {
    HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("XRefCalc.xls");
    HSSFWorkbook wb2 = HSSFTestDataSamples.openSampleWorkbook("XRefCalcData.xls");
    CellReference cellRef = new CellReference(wb.getName("QUANT").getRefersToFormula());
    HSSFCell cell = wb.getSheet(cellRef.getSheetName()).getRow(cellRef.getRow()).getCell((int) cellRef.getCol());
    cell.setCellValue(NEW_QUANT);
    cell = wb2.getSheet("CostSheet").getRow(1).getCell(1);
    cell.setCellValue(NEW_PART_COST);
    HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(wb);
    HSSFFormulaEvaluator evaluatorCost = new HSSFFormulaEvaluator(wb2);
    String[] bookNames = { "XRefCalc.xls", "XRefCalcData.xls" };
    HSSFFormulaEvaluator[] evaluators = { evaluator, evaluatorCost };
    HSSFFormulaEvaluator.setupEnvironment(bookNames, evaluators);
    cellRef = new CellReference(wb.getName("UNITCOST").getRefersToFormula());
    HSSFCell uccell = wb.getSheet(cellRef.getSheetName()).getRow(cellRef.getRow()).getCell((int) cellRef.getCol());
    cellRef = new CellReference(wb.getName("COST").getRefersToFormula());
    HSSFCell ccell = wb.getSheet(cellRef.getSheetName()).getRow(cellRef.getRow()).getCell((int) cellRef.getCol());
    cellRef = new CellReference(wb.getName("TOTALCOST").getRefersToFormula());
    HSSFCell tccell = wb.getSheet(cellRef.getSheetName()).getRow(cellRef.getRow()).getCell((int) cellRef.getCol());
    evaluator.evaluateFormulaCellEnum(uccell);
    evaluator.evaluateFormulaCellEnum(ccell);
    evaluator.evaluateFormulaCellEnum(tccell);
    assertEquals(NEW_PART_COST, uccell.getNumericCellValue());
    assertEquals(NEW_PART_COST * NEW_QUANT, ccell.getNumericCellValue());
    assertEquals(NEW_PART_COST * NEW_QUANT * MARKUP_COST_2, tccell.getNumericCellValue());
}
Also used : HSSFFormulaEvaluator(org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) CellReference(org.apache.poi.hssf.util.CellReference) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 95 with HSSFWorkbook

use of org.apache.poi.hssf.usermodel.HSSFWorkbook in project poi by apache.

the class TestPercentPtg method testReading.

/**
     * Tests reading a file containing this ptg.
     */
public void testReading() {
    HSSFWorkbook workbook = loadWorkbook("PercentPtg.xls");
    HSSFSheet sheet = workbook.getSheetAt(0);
    assertEquals("Wrong numeric value for original number", 53000.0, sheet.getRow(0).getCell(0).getNumericCellValue(), 0.0);
    assertEquals("Wrong numeric value for percent formula result", 5300.0, sheet.getRow(1).getCell(0).getNumericCellValue(), 0.0);
    assertEquals("Wrong formula string for percent formula", "A1*10%", sheet.getRow(1).getCell(0).getCellFormula());
}
Also used : HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Aggregations

HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)518 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)187 Test (org.junit.Test)172 Workbook (org.apache.poi.ss.usermodel.Workbook)144 Sheet (org.apache.poi.ss.usermodel.Sheet)128 Row (org.apache.poi.ss.usermodel.Row)102 HSSFCell (org.apache.poi.hssf.usermodel.HSSFCell)100 Cell (org.apache.poi.ss.usermodel.Cell)96 FileOutputStream (java.io.FileOutputStream)93 IOException (java.io.IOException)91 HSSFRow (org.apache.poi.hssf.usermodel.HSSFRow)77 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)69 FileInputStream (java.io.FileInputStream)59 File (java.io.File)58 ArrayList (java.util.ArrayList)52 HSSFFormulaEvaluator (org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator)38 InputStream (java.io.InputStream)28 OutputStream (java.io.OutputStream)28 CellStyle (org.apache.poi.ss.usermodel.CellStyle)28 HSSFCellStyle (org.apache.poi.hssf.usermodel.HSSFCellStyle)26