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());
}
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();
}
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();
}
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();
}
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();
}
Aggregations