use of org.apache.poi.ss.usermodel.Workbook in project poi by apache.
the class BaseTestCellUtil method getRow.
@Test
public void getRow() throws IOException {
Workbook wb = _testDataProvider.createWorkbook();
Sheet sh = wb.createSheet();
Row row1 = sh.createRow(0);
// Get row that already exists
Row r1 = CellUtil.getRow(0, sh);
assertNotNull(r1);
assertSame("An existing row should not be recreated", row1, r1);
// Get row that does not exist yet
assertNotNull(CellUtil.getRow(1, sh));
wb.close();
}
use of org.apache.poi.ss.usermodel.Workbook in project poi by apache.
the class BaseTestCellUtil method setFillForegroundColorBeforeFillBackgroundColor.
/**
* bug 55555
* @deprecated Replaced by {@link #setFillForegroundColorBeforeFillBackgroundColorEnum()}
* @since POI 3.15 beta 3
*/
@Deprecated
// bug 55555
@Test
public void setFillForegroundColorBeforeFillBackgroundColor() throws IOException {
Workbook wb1 = _testDataProvider.createWorkbook();
Cell A1 = wb1.createSheet().createRow(0).createCell(0);
Map<String, Object> properties = new HashMap<String, Object>();
// FIXME: Use FillPatternType.BRICKS enum
properties.put(CellUtil.FILL_PATTERN, CellStyle.BRICKS);
properties.put(CellUtil.FILL_FOREGROUND_COLOR, IndexedColors.BLUE.index);
properties.put(CellUtil.FILL_BACKGROUND_COLOR, IndexedColors.RED.index);
CellUtil.setCellStyleProperties(A1, properties);
CellStyle style = A1.getCellStyle();
// FIXME: Use FillPatternType.BRICKS enum
assertEquals("fill pattern", CellStyle.BRICKS, style.getFillPattern());
assertEquals("fill foreground color", IndexedColors.BLUE, IndexedColors.fromInt(style.getFillForegroundColor()));
assertEquals("fill background color", IndexedColors.RED, IndexedColors.fromInt(style.getFillBackgroundColor()));
wb1.close();
}
use of org.apache.poi.ss.usermodel.Workbook in project poi by apache.
the class BaseTestCellUtil method createCell.
@Test
public void createCell() throws IOException {
Workbook wb = _testDataProvider.createWorkbook();
Sheet sh = wb.createSheet();
Row row = sh.createRow(0);
CellStyle style = wb.createCellStyle();
style.setWrapText(true);
// calling createCell on a non-existing cell should create a cell and set the cell value and style.
Cell F1 = CellUtil.createCell(row, 5, "Cell Value", style);
assertSame(row.getCell(5), F1);
assertEquals("Cell Value", F1.getStringCellValue());
assertEquals(style, F1.getCellStyle());
// should be assertSame, but a new HSSFCellStyle is returned for each getCellStyle() call.
// HSSFCellStyle wraps an underlying style record, and the underlying
// style record is the same between multiple getCellStyle() calls.
// calling createCell on an existing cell should return the existing cell and modify the cell value and style.
Cell f1 = CellUtil.createCell(row, 5, "Overwritten cell value", null);
assertSame(row.getCell(5), f1);
assertSame(F1, f1);
assertEquals("Overwritten cell value", f1.getStringCellValue());
assertEquals("Overwritten cell value", F1.getStringCellValue());
assertEquals("cell style should be unchanged with createCell(..., null)", style, f1.getCellStyle());
assertEquals("cell style should be unchanged with createCell(..., null)", style, F1.getCellStyle());
// test createCell(row, column, value) (no CellStyle)
f1 = CellUtil.createCell(row, 5, "Overwritten cell with default style");
assertSame(F1, f1);
wb.close();
}
use of org.apache.poi.ss.usermodel.Workbook in project poi by apache.
the class BaseTestCellUtil method setCellStylePropertyBorderWithShortAndEnum.
@Test()
public void setCellStylePropertyBorderWithShortAndEnum() throws IOException {
Workbook wb = _testDataProvider.createWorkbook();
Sheet s = wb.createSheet();
Row r = s.createRow(0);
Cell c = r.createCell(0);
// A valid BorderStyle constant, as a Short
CellUtil.setCellStyleProperty(c, CellUtil.BORDER_BOTTOM, BorderStyle.DASH_DOT.getCode());
assertEquals(BorderStyle.DASH_DOT, c.getCellStyle().getBorderBottomEnum());
// A valid BorderStyle constant, as an Enum
CellUtil.setCellStyleProperty(c, CellUtil.BORDER_TOP, BorderStyle.MEDIUM_DASH_DOT);
assertEquals(BorderStyle.MEDIUM_DASH_DOT, c.getCellStyle().getBorderTopEnum());
wb.close();
}
use of org.apache.poi.ss.usermodel.Workbook in project poi by apache.
the class XSSFRowShifter method shiftFormula.
/**
* Shift a formula using the supplied FormulaShifter
*
* @param row the row of the cell this formula belongs to. Used to get a reference to the parent workbook.
* @param formula the formula to shift
* @param shifter the FormulaShifter object that operates on the parsed formula tokens
* @return the shifted formula if the formula was changed,
* <code>null</code> if the formula wasn't modified
*/
private static String shiftFormula(Row row, String formula, FormulaShifter shifter) {
Sheet sheet = row.getSheet();
Workbook wb = sheet.getWorkbook();
int sheetIndex = wb.getSheetIndex(sheet);
final int rowIndex = row.getRowNum();
XSSFEvaluationWorkbook fpb = XSSFEvaluationWorkbook.create((XSSFWorkbook) wb);
try {
Ptg[] ptgs = FormulaParser.parse(formula, fpb, FormulaType.CELL, sheetIndex, rowIndex);
String shiftedFmla = null;
if (shifter.adjustFormula(ptgs, sheetIndex)) {
shiftedFmla = FormulaRenderer.toFormulaString(fpb, ptgs);
}
return shiftedFmla;
} catch (FormulaParseException fpe) {
// Log, but don't change, rather than breaking
logger.log(POILogger.WARN, "Error shifting formula on row ", row.getRowNum(), fpe);
return formula;
}
}
Aggregations