Example usage for org.apache.poi.xssf.usermodel XSSFEvaluationWorkbook create

List of usage examples for org.apache.poi.xssf.usermodel XSSFEvaluationWorkbook create

Introduction

In this page you can find the example usage for org.apache.poi.xssf.usermodel XSSFEvaluationWorkbook create.

Prototype

public static XSSFEvaluationWorkbook create(XSSFWorkbook book) 

Source Link

Usage

From source file:com.blackducksoftware.tools.commonframework.standard.protex.report.template.TemplateWriter.java

License:Apache License

/**
 * Handles copying the formula for the provided sheet and active row
 * //from   w w w .ja va 2s. co  m
 * @param sheet
 *            the provided sheet
 * @param targetCell
 *            the target cell to copy the formula
 * @param activeRow
 *            the active row
 * @param column
 *            the TemplateColumn to be used for
 */
private void copyFormula(Sheet sheet, Cell targetCell, Row activeRow, TemplateColumn column) {
    if (targetCell == null || sheet == null || targetCell.getCellType() != Cell.CELL_TYPE_FORMULA) {
        return;
    }

    String formula = column.getCellFormula();

    int shiftRows = activeRow.getRowNum() - 1;
    int shiftCols = 0;

    XSSFEvaluationWorkbook workbookWrapper = XSSFEvaluationWorkbook.create((XSSFWorkbook) sheet.getWorkbook());

    Ptg[] ptgs = FormulaParser.parse(formula, workbookWrapper, FormulaType.CELL,
            sheet.getWorkbook().getSheetIndex(sheet));

    for (Ptg ptg : ptgs) {
        if (ptg instanceof RefPtgBase) {
            RefPtgBase ref = (RefPtgBase) ptg;
            if (ref.isColRelative()) {
                ref.setColumn(ref.getColumn() + shiftCols);
            }
            if (ref.isRowRelative()) {
                ref.setRow(ref.getRow() + shiftRows);
            }
        } else if (ptg instanceof AreaPtg) {
            AreaPtg ref = (AreaPtg) ptg;
            if (ref.isFirstColRelative()) {
                ref.setFirstColumn(ref.getFirstColumn() + shiftCols);
            }
            if (ref.isLastColRelative()) {
                ref.setLastColumn(ref.getLastColumn() + shiftCols);
            }
            if (ref.isFirstRowRelative()) {
                ref.setFirstRow(ref.getFirstRow() + shiftRows);
            }
            if (ref.isLastRowRelative()) {
                ref.setLastRow(ref.getLastRow() + shiftRows);
            }
        }
    }

    formula = FormulaRenderer.toFormulaString(workbookWrapper, ptgs);
    targetCell.setCellFormula(formula);
    log.debug("Set Formula for row " + activeRow.getRowNum() + " : " + formula);
    targetCell.setAsActiveCell();
}

From source file:com.dataart.spreadsheetanalytics.engine.ConverterUtils.java

License:Apache License

/** Creates an instance of new {@link EvaluationWorkbook} from {@link Workbook} */
public static EvaluationWorkbook newEvaluationWorkbook(Workbook workbook) {
    return XSSFEvaluationWorkbook.create((XSSFWorkbook) workbook);
}

From source file:com.dataart.spreadsheetanalytics.engine.graph.PoiDependencyGraphBuilder.java

License:Apache License

protected PoiDependencyGraphBuilder(IDataModel model) {
    this.poiBook = Converters.toWorkbook(model);
    this.poiFormulaBook = XSSFEvaluationWorkbook.create((XSSFWorkbook) this.poiBook);
    this.state = new ExecutionGraph();
}

From source file:com.dataart.spreadsheetanalytics.engine.PoiWorkbookConverters.java

License:Apache License

public PoiProxyWorkbook(final Workbook wb) {
    ewb = XSSFEvaluationWorkbook.create(XSSFWorkbook.class.cast(wb));
    this.sheet = makeSheet(wb, (FormulaParsingWorkbook) ewb);
    this.names = makeNames(wb, ewb);
}

From source file:org.tiefaces.components.websheet.TieWebSheetBean.java

License:MIT License

/**
 * Set up workbook. Also create evaluation wrapper.
 * //from w  w w .j a  va 2s . co  m
 * @param pWb
 *            workbook.
 */

public void setWb(final Workbook pWb) {

    this.getSerialWb().setWb(pWb);
    this.wbWrapper = XSSFEvaluationWorkbook.create((XSSFWorkbook) pWb);
}

From source file:org.tiefaces.components.websheet.TieWebSheetBean.java

License:MIT License

/**
 * Return evaluation wrapper if needed.//from  w w  w  . j  av  a  2s  .c o m
 * 
 * @return wbwrapper.
 */
public XSSFEvaluationWorkbook getWbWrapper() {
    if ((this.wbWrapper == null) && (this.getWb() != null)) {
        this.wbWrapper = XSSFEvaluationWorkbook.create((XSSFWorkbook) this.getWb());
    }
    return wbWrapper;
}

From source file:uk.ac.liverpool.spreadsheet.ToXML.java

License:Apache License

private void printSheetContent(Sheet sheet) {
    ensureColumnBounds(sheet);//from   w w w. j a va2 s  . c  o m
    printColumnHeads();

    cellsToFormula = new HashMap<String, List<String>>();
    cellToFormulaConverted = new HashMap<String, String>();
    crToParent = new HashMap<String, List<String>>();
    FormulaParsingWorkbook fpwb;
    FormulaRenderingWorkbook frwb;
    if (xswb != null) {
        XSSFEvaluationWorkbook w = XSSFEvaluationWorkbook.create(xswb);
        frwb = w;
        fpwb = w;
    } else if (hswb != null) {
        HSSFEvaluationWorkbook w = HSSFEvaluationWorkbook.create(hswb);
        frwb = w;
        fpwb = w;
    }

    else
        return;
    // first we need to determine all the dependencies ofr each formula
    Iterator<Row> rows = sheet.rowIterator();
    while (rows.hasNext()) {
        Row row = rows.next();
        for (int i = firstColumn; i < endColumn; i++) {
            if (i >= row.getFirstCellNum() && i < row.getLastCellNum()) {
                Cell cell = row.getCell(i);
                if (cell != null) {
                    if (cell.getCellType() == Cell.CELL_TYPE_FORMULA)
                        try {
                            parseFormula(cell, fpwb, frwb);

                        } catch (Exception x) {

                        }
                }
            }
        }
    }
    rows = sheet.rowIterator();

    while (rows.hasNext()) {
        Row row = rows.next();
        int rowNumber = row.getRowNum() + 1;
        out.format("  <TableRow>%n");
        out.format("    <RowHeader>%d</RowHeader>%n", rowNumber);
        out.format("  <TableCells>%n");
        for (int i = firstColumn; i < endColumn; i++) {
            String content = "0";
            String attrs = "";
            CellStyle style = null;
            String valueType = "float";
            Cell cell = row.getCell(i);
            CellReference c = new CellReference(rowNumber - 1, i);
            attrs += " cellID=\"." + c.formatAsString() + "\"";

            String cr = c.formatAsString();
            // if (i >= row.getFirstCellNum() && i < row.getLastCellNum()) {

            if (cell != null && cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
                attrs += " readOnly=\"readOnly\"";
                try {
                    attrs += " cellFormula=\"" + StringEscapeUtils.escapeXml(cell.getCellFormula()) + "\"";
                } catch (Exception x) {
                    attrs += " cellFormula=\"FORMULA ERROR\"";
                }
            } else {
                List<String> cfrl = cellsToFormula.get(cr);
                StringBuffer formula = new StringBuffer("");

                if (cfrl != null) {
                    List<String> refs = new LinkedList<String>();
                    visit(cfrl, refs);
                    System.out.println(refs);
                    cleanup(refs);
                    for (String s : refs) {
                        formula.append(StringEscapeUtils.escapeXml(cellToFormulaConverted.get(s)));
                        formula.append(" || ");
                    }
                }
                if (formula.length() > 0)
                    attrs += " formula=\"" + formula.substring(0, formula.length() - 4) + "\"";
            }
            if (cell != null) {
                style = cell.getCellStyle();
                // Set the value that is rendered for the cell
                // also applies the format

                try {
                    CellFormat cf = CellFormat.getInstance(style.getDataFormatString());
                    CellFormatResult result = cf.apply(cell);
                    content = result.text;
                } catch (Exception x) {
                    content = "DATA FORMULA ERROR ";
                }

            }
            // }
            attrs += " value_type=\"" + valueType + "\"";
            attrs += " value=\"" + StringEscapeUtils.escapeXml(content) + "\"";
            out.format("    <TableCell  %s>%s</TableCell>%n", // class=%s
                    // styleName(style),
                    attrs, StringEscapeUtils.escapeXml(content));
        }
        out.format(" </TableCells> </TableRow>%n%n");
    }
}