Search in sources :

Example 41 with HSSFWorkbook

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

the class TestFormulaParser method testBooleanNamedSheet.

@Test
public void testBooleanNamedSheet() throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("true");
    HSSFCell cell = sheet.createRow(0).createCell(0);
    cell.setCellFormula("'true'!B2");
    assertEquals("'true'!B2", cell.getCellFormula());
    wb.close();
}
Also used : HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.Test)

Example 42 with HSSFWorkbook

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

the class TestFormulaParser method testMultiSheetReference.

@Test
public void testMultiSheetReference() throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook();
    wb.createSheet("Cash_Flow");
    wb.createSheet("Test Sheet");
    HSSFSheet sheet = wb.createSheet("Test");
    HSSFRow row = sheet.createRow(0);
    HSSFCell cell = row.createCell(0);
    String formula;
    // References to a single cell:
    // One sheet
    cell.setCellFormula("Cash_Flow!A1");
    formula = cell.getCellFormula();
    assertEquals("Cash_Flow!A1", formula);
    // Then the other
    cell.setCellFormula("\'Test Sheet\'!A1");
    formula = cell.getCellFormula();
    assertEquals("\'Test Sheet\'!A1", formula);
    // Now both
    cell.setCellFormula("Cash_Flow:\'Test Sheet\'!A1");
    formula = cell.getCellFormula();
    assertEquals("Cash_Flow:\'Test Sheet\'!A1", formula);
    // References to a range (area) of cells:
    // One sheet
    cell.setCellFormula("Cash_Flow!A1:B2");
    formula = cell.getCellFormula();
    assertEquals("Cash_Flow!A1:B2", formula);
    // Then the other
    cell.setCellFormula("\'Test Sheet\'!A1:B2");
    formula = cell.getCellFormula();
    assertEquals("\'Test Sheet\'!A1:B2", formula);
    // Now both
    cell.setCellFormula("Cash_Flow:\'Test Sheet\'!A1:B2");
    formula = cell.getCellFormula();
    assertEquals("Cash_Flow:\'Test Sheet\'!A1:B2", formula);
    wb.close();
}
Also used : HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) UnicodeString(org.apache.poi.hssf.record.common.UnicodeString) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Test(org.junit.Test)

Example 43 with HSSFWorkbook

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

the class TestWorkbook method testAddNameX.

@Test
public void testAddNameX() throws IOException {
    HSSFWorkbook hwb = new HSSFWorkbook();
    InternalWorkbook wb = TestHSSFWorkbook.getInternalWorkbook(hwb);
    assertNotNull(wb.getNameXPtg("ISODD", AggregatingUDFFinder.DEFAULT));
    FreeRefFunction NotImplemented = new FreeRefFunction() {

        @Override
        public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
            throw new RuntimeException("not implemented");
        }
    };
    /*
         * register the two test UDFs in a UDF finder, to be passed to the evaluator
         */
    UDFFinder udff1 = new DefaultUDFFinder(new String[] { "myFunc" }, new FreeRefFunction[] { NotImplemented });
    UDFFinder udff2 = new DefaultUDFFinder(new String[] { "myFunc2" }, new FreeRefFunction[] { NotImplemented });
    UDFFinder udff = new AggregatingUDFFinder(udff1, udff2);
    assertNotNull(wb.getNameXPtg("myFunc", udff));
    assertNotNull(wb.getNameXPtg("myFunc2", udff));
    // myFunc3 is unknown
    assertNull(wb.getNameXPtg("myFunc3", udff));
    hwb.close();
}
Also used : AggregatingUDFFinder(org.apache.poi.ss.formula.udf.AggregatingUDFFinder) OperationEvaluationContext(org.apache.poi.ss.formula.OperationEvaluationContext) AggregatingUDFFinder(org.apache.poi.ss.formula.udf.AggregatingUDFFinder) DefaultUDFFinder(org.apache.poi.ss.formula.udf.DefaultUDFFinder) UDFFinder(org.apache.poi.ss.formula.udf.UDFFinder) FreeRefFunction(org.apache.poi.ss.formula.functions.FreeRefFunction) TestHSSFWorkbook(org.apache.poi.hssf.usermodel.TestHSSFWorkbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) DefaultUDFFinder(org.apache.poi.ss.formula.udf.DefaultUDFFinder) Test(org.junit.Test)

Example 44 with HSSFWorkbook

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

the class TestArrayRecord method testBug57231.

public void testBug57231() {
    HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("57231_MixedGasReport.xls");
    HSSFSheet sheet = wb.getSheet("master");
    HSSFSheet newSheet = wb.cloneSheet(wb.getSheetIndex(sheet));
    int idx = wb.getSheetIndex(newSheet);
    wb.setSheetName(idx, "newName");
    // Write the output to a file
    HSSFWorkbook wbBack = HSSFTestDataSamples.writeOutAndReadBack(wb);
    assertNotNull(wbBack);
    assertNotNull(wbBack.getSheet("master"));
    assertNotNull(wbBack.getSheet("newName"));
}
Also used : HSSFSheet(org.apache.poi.hssf.usermodel.HSSFSheet) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook)

Example 45 with HSSFWorkbook

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

the class TestRVA method data.

@Parameters(name = "{1}")
public static Collection<Object[]> data() throws Exception {
    poifs = new NPOIFSFileSystem(HSSFTestDataSamples.getSampleFile("testRVA.xls"), true);
    workbook = new HSSFWorkbook(poifs);
    sheet = workbook.getSheetAt(0);
    List<Object[]> data = new ArrayList<Object[]>();
    for (int rowIdx = 0; true; rowIdx++) {
        HSSFRow row = sheet.getRow(rowIdx);
        if (row == null) {
            break;
        }
        HSSFCell cell = row.getCell(0);
        if (cell == null || cell.getCellTypeEnum() == CellType.BLANK) {
            break;
        }
        String formula = cell.getCellFormula();
        data.add(new Object[] { cell, formula });
    }
    return data;
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) HSSFCell(org.apache.poi.hssf.usermodel.HSSFCell) ArrayList(java.util.ArrayList) HSSFRow(org.apache.poi.hssf.usermodel.HSSFRow) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) Parameters(org.junit.runners.Parameterized.Parameters)

Aggregations

HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)532 HSSFSheet (org.apache.poi.hssf.usermodel.HSSFSheet)189 Test (org.junit.Test)173 Workbook (org.apache.poi.ss.usermodel.Workbook)151 Sheet (org.apache.poi.ss.usermodel.Sheet)138 Row (org.apache.poi.ss.usermodel.Row)113 HSSFCell (org.apache.poi.hssf.usermodel.HSSFCell)102 Cell (org.apache.poi.ss.usermodel.Cell)101 FileOutputStream (java.io.FileOutputStream)99 IOException (java.io.IOException)99 HSSFRow (org.apache.poi.hssf.usermodel.HSSFRow)79 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)72 FileInputStream (java.io.FileInputStream)61 File (java.io.File)59 ArrayList (java.util.ArrayList)52 HSSFFormulaEvaluator (org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator)38 InputStream (java.io.InputStream)34 HSSFCellStyle (org.apache.poi.hssf.usermodel.HSSFCellStyle)32 CellStyle (org.apache.poi.ss.usermodel.CellStyle)31 OutputStream (java.io.OutputStream)28