Search in sources :

Example 96 with Workbook

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();
}
Also used : Row(org.apache.poi.ss.usermodel.Row) Sheet(org.apache.poi.ss.usermodel.Sheet) Workbook(org.apache.poi.ss.usermodel.Workbook) Test(org.junit.Test)

Example 97 with Workbook

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();
}
Also used : HashMap(java.util.HashMap) CellStyle(org.apache.poi.ss.usermodel.CellStyle) Cell(org.apache.poi.ss.usermodel.Cell) Workbook(org.apache.poi.ss.usermodel.Workbook) Test(org.junit.Test)

Example 98 with Workbook

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();
}
Also used : Row(org.apache.poi.ss.usermodel.Row) CellStyle(org.apache.poi.ss.usermodel.CellStyle) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) Workbook(org.apache.poi.ss.usermodel.Workbook) Test(org.junit.Test)

Example 99 with Workbook

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();
}
Also used : Row(org.apache.poi.ss.usermodel.Row) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) Workbook(org.apache.poi.ss.usermodel.Workbook) Test(org.junit.Test)

Example 100 with Workbook

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;
    }
}
Also used : FormulaParseException(org.apache.poi.ss.formula.FormulaParseException) XSSFEvaluationWorkbook(org.apache.poi.xssf.usermodel.XSSFEvaluationWorkbook) Ptg(org.apache.poi.ss.formula.ptg.Ptg) AreaErrPtg(org.apache.poi.ss.formula.ptg.AreaErrPtg) AreaPtg(org.apache.poi.ss.formula.ptg.AreaPtg) Sheet(org.apache.poi.ss.usermodel.Sheet) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFEvaluationWorkbook(org.apache.poi.xssf.usermodel.XSSFEvaluationWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook)

Aggregations

Workbook (org.apache.poi.ss.usermodel.Workbook)296 Sheet (org.apache.poi.ss.usermodel.Sheet)209 Test (org.junit.Test)187 Cell (org.apache.poi.ss.usermodel.Cell)139 Row (org.apache.poi.ss.usermodel.Row)121 HSSFWorkbook (org.apache.poi.hssf.usermodel.HSSFWorkbook)95 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)90 FileOutputStream (java.io.FileOutputStream)33 FormulaEvaluator (org.apache.poi.ss.usermodel.FormulaEvaluator)30 CellStyle (org.apache.poi.ss.usermodel.CellStyle)25 InternalWorkbook (org.apache.poi.hssf.model.InternalWorkbook)24 File (java.io.File)22 IOException (java.io.IOException)22 XSSFChart (org.apache.poi.xssf.usermodel.XSSFChart)20 ByteArrayInputStream (java.io.ByteArrayInputStream)19 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)17 InternalSheet (org.apache.poi.hssf.model.InternalSheet)15 XSSFDrawing (org.apache.poi.xssf.usermodel.XSSFDrawing)15 CTChart (org.openxmlformats.schemas.drawingml.x2006.chart.CTChart)15 SXSSFWorkbook (org.apache.poi.xssf.streaming.SXSSFWorkbook)14