Example usage for org.apache.poi.ss.usermodel Row createCell

List of usage examples for org.apache.poi.ss.usermodel Row createCell

Introduction

In this page you can find the example usage for org.apache.poi.ss.usermodel Row createCell.

Prototype

Cell createCell(int column);

Source Link

Document

Use this to create new cells within the row and return it.

Usage

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

License:Apache License

/**
 * Converts {@link IDataSet} to {@link Workbook}.
 * The result {@link Workbook} is created from @param formatting.
 *//*from   ww w .java  2s  .  c om*/
static Workbook toWorkbook(final IDataSet dataSet, final Workbook formatting) {
    Workbook result = formatting == null ? ConverterUtils.newWorkbook()
            : ConverterUtils.clearContent(formatting);

    Sheet sheet = result.createSheet(dataSet.getName());
    for (IDsRow row : dataSet) {
        Row wbRow = sheet.createRow(row.index() - 1);
        for (IDsCell cell : row) {
            Cell wbCell = wbRow.createCell(cell.index() - 1);
            ConverterUtils.populateCellValue(wbCell, cell.getValue());
        }
    }

    return result;
}

From source file:com.dataart.spreadsheetanalytics.functions.poi.data.FuncexecFunction.java

License:Apache License

@Override
public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {

    log.debug("In evaluate() of FUNCEXEC function. Args = {}", Arrays.toString(args));

    if (!(args[0] instanceof StringEval) && !(args[0] instanceof RefEval)) {
        log.warn(/* w ww . j  a  v a 2 s  .c  o m*/
                "The first argument of FUNCEXEC function must be a string (or a reference to a cell) - name of DEFINE function.");
        return ErrorEval.VALUE_INVALID;
    }

    String defineFunctionName;
    try {
        defineFunctionName = (String) coerceValueTo(
                getSingleValue(args[0], ec.getRowIndex(), ec.getColumnIndex()));
    } catch (EvaluationException e) {
        log.error(String.format("Cannot get the value of DEFINE functuion name: %s", args[0]), e);
        return ErrorEval.VALUE_INVALID;
    }
    defineFunctionName = defineFunctionName.toUpperCase(Locale.getDefault());

    MetaFunctionAccessor defines = (MetaFunctionAccessor) ec.getCustomEvaluationContext()
            .get(MetaFunctionAccessor.class);
    if (defines == null) {
        defines = this.external.getMetaFunctionAccessor();
    }

    if (defines.get(defineFunctionName) == null) {
        log.warn("No DEFINE function with name {} is found.", defineFunctionName);
        return ErrorEval.NAME_INVALID;
    }

    List<ValueEval> inputValues = new LinkedList<>();
    List<ValueEval> inArgs = new ArrayList(Arrays.asList(args));
    inArgs.remove(0); //remove define function name

    try {
        for (ValueEval v : ICustomFunction.prepareQueryArgs(inArgs)) {
            inputValues.add(getSingleValue(v, ec.getRowIndex(), ec.getColumnIndex()));
        }
    } catch (EvaluationException e) {
        log.error("Cannot resolve input values for FUNCEXEC function", e);
        return ErrorEval.VALUE_INVALID;
    }

    final DefineFunctionMeta meta = (DefineFunctionMeta) defines.get(defineFunctionName);
    log.info("Found DEFINE function to invoke. Name = {}.", defineFunctionName);

    if (meta.getInputs().size() != inputValues.size()) {
        log.warn("Wrong number of input arguments for FUNCEXEC+DEFINE. Expected: {}, Actual: {}.",
                meta.getInputs().size(), args.length - 1);
        return ErrorEval.VALUE_INVALID;
    }

    List<IA1Address> inputAddresses = meta.getInputs();
    log.debug("Input Addresses for DEFINE: {}, Input Values for DEFINE: {}.", inputAddresses, inputValues);

    if (inputAddresses.size() != inputValues.size()) {
        log.warn("Wrong number of input arguments for {} function.", defineFunctionName);
        return ErrorEval.VALUE_INVALID;
    }

    DataModelAccessor dataModels = (DataModelAccessor) ec.getCustomEvaluationContext()
            .get(DataModelAccessor.class);
    if (dataModels == null) {
        dataModels = this.external.getDataModelAccessor();
    }

    IDataModel dmWithDefine = dataModels.get(meta.getDataModelId());

    Workbook book = toWorkbook(dmWithDefine);
    EvaluationWorkbook defineBook = toEvaluationWorkbook(book);

    Sheet s = book.getSheetAt(0); //TODO one sheet support
    for (int i = 0; i < inputAddresses.size(); i++) {

        Row defineRow = s.getRow(inputAddresses.get(i).row());
        if (defineRow == null) {
            defineRow = s.createRow(inputAddresses.get(i).row());
        }
        Cell defineCell = defineRow.getCell(inputAddresses.get(i).column());
        if (defineCell == null) {
            defineCell = defineRow.createCell(inputAddresses.get(i).column());
        }

        populateCellValue(defineCell, inputValues.get(i));
        updateCell(defineBook, defineCell);
    }

    WorkbookEvaluator defineEvaluator = new WorkbookEvaluator(defineBook,
            IStabilityClassifier.TOTALLY_IMMUTABLE, null);
    List<ValueEval> outputValues = meta.getOutputs().stream().map(
            a -> defineEvaluator.evaluate(getEvaluationCell(defineBook, a), ec.getCustomEvaluationContext()))
            .collect(Collectors.<ValueEval>toList());

    log.debug("Output Values of DEFINE execution: {}.", outputValues);

    return outputValues.size() == 1 ? outputValues.get(0) : toArrayEval(outputValues);
}

From source file:com.dawg6.web.dhcalc.server.ExportExcel.java

License:Open Source License

private Cell addTableCell(Row row, int col, String label) {
    Cell cell = row.createCell(col);
    cell.setCellValue(label);/*from  w  ww . jav  a2s .  c  o  m*/
    CellUtil.setAlignment(cell, row.getSheet().getWorkbook(), CellStyle.ALIGN_LEFT);

    return cell;
}

From source file:com.dawg6.web.dhcalc.server.ExportExcel.java

License:Open Source License

@SuppressWarnings("unused")
private Cell addTableCell(Row row, int col, Boolean value) {
    Cell cell = row.createCell(col);
    cell.setCellValue(String.valueOf(value));
    CellUtil.setAlignment(cell, row.getSheet().getWorkbook(), CellStyle.ALIGN_CENTER);

    return cell;/*from w  ww .  j  av  a 2s.  c o m*/
}

From source file:com.dawg6.web.dhcalc.server.ExportExcel.java

License:Open Source License

private Cell addTableCell(Row row, int col, int value) {
    Cell cell = row.createCell(col);
    cell.setCellValue(String.valueOf(value));
    cell.setCellStyle(intStyle);//from  w w  w .  jav  a  2  s  .co m
    CellUtil.setAlignment(cell, row.getSheet().getWorkbook(), CellStyle.ALIGN_RIGHT);

    return cell;
}

From source file:com.dawg6.web.dhcalc.server.ExportExcel.java

License:Open Source License

private Cell addTableCell(Row row, int col, double value, HSSFCellStyle style) {
    Cell cell = row.createCell(col);
    cell.setCellValue(value);//w  w  w  . j a  va2 s  . c  o  m
    cell.setCellStyle(style);
    CellUtil.setAlignment(cell, row.getSheet().getWorkbook(), CellStyle.ALIGN_RIGHT);

    return cell;
}

From source file:com.dawg6.web.dhcalc.server.ExportExcel.java

License:Open Source License

private Cell createTableHeader(HSSFSheet sheet, int col, String label) {

    int n = sheet.getPhysicalNumberOfRows();
    Row row = null;

    if (n < 1)
        row = sheet.createRow(0);//from w  ww.  j  ava2s.c o  m
    else
        row = sheet.getRow(0);

    Cell cell = row.createCell(col);
    cell.setCellValue(label);
    cell.setCellStyle(boldStyle);
    CellUtil.setAlignment(cell, sheet.getWorkbook(), CellStyle.ALIGN_CENTER);

    return cell;

}

From source file:com.dawg6.web.dhcalc.server.ExportExcel.java

License:Open Source License

private Cell createInputHeader(Sheet sheet, String label) {
    Row row = sheet.createRow(sheet.getPhysicalNumberOfRows());
    Cell cell1 = row.createCell(0);
    cell1.setCellValue(label);/*from w w w  .  j  a  va  2 s.  co m*/
    cell1.setCellStyle(boldStyle);

    return cell1;
}

From source file:com.dawg6.web.dhcalc.server.ExportExcel.java

License:Open Source License

private Cell createInputCell(Sheet sheet, String label) {
    Row row = sheet.createRow(sheet.getPhysicalNumberOfRows());
    Cell cell1 = row.createCell(0);
    cell1.setCellValue(label + ":");
    Cell cell2 = row.createCell(1);//  w  ww .j  av  a 2s  .  c o m
    CellUtil.setAlignment(cell2, sheet.getWorkbook(), CellStyle.ALIGN_RIGHT);

    inputCells.put(label, cell2);

    return cell2;
}

From source file:com.dbumama.market.web.core.render.excel.PoiExporter.java

License:Apache License

public Workbook export() {
    Preconditions.checkNotNull(data, "data can not be null");
    Preconditions.checkNotNull(headers, "headers can not be null");
    Preconditions.checkNotNull(columns, "columns can not be null");
    Preconditions.checkArgument(/*from w  w w .j av  a  2s  . c  o  m*/
            data.length == sheetNames.length && sheetNames.length == headers.length
                    && headers.length == columns.length,
            "data,sheetNames,headers and columns'length should be the same." + "(data:" + data.length
                    + ",sheetNames:" + sheetNames.length + ",headers:" + headers.length + ",columns:"
                    + columns.length + ")");
    Preconditions.checkArgument(cellWidth >= 0, "cellWidth can not be less than 0");
    Workbook wb;
    if (VERSION_2003.equals(version)) {
        wb = new HSSFWorkbook();
        if (data.length > 1) {
            for (int i = 0; i < data.length; i++) {
                List<?> item = data[i];
                Preconditions.checkArgument(item.size() < MAX_ROWS,
                        "Data [" + i + "] is invalid:invalid data size (" + item.size()
                                + ") outside allowable range (0..65535)");
            }
        } else if (data.length == 1 && data[0].size() > MAX_ROWS) {
            data = dice(data[0], MAX_ROWS).toArray(new List<?>[] {});
            String sheetName = sheetNames[0];
            sheetNames = new String[data.length];
            for (int i = 0; i < data.length; i++) {
                sheetNames[i] = sheetName + (i == 0 ? "" : (i + 1));
            }
            String[] header = headers[0];
            headers = new String[data.length][];
            for (int i = 0; i < data.length; i++) {
                headers[i] = header;
            }
            String[] column = columns[0];
            columns = new String[data.length][];
            for (int i = 0; i < data.length; i++) {
                columns[i] = column;
            }
        }
    } else {
        wb = new XSSFWorkbook();
    }
    if (data.length == 0) {
        return wb;
    }
    for (int i = 0; i < data.length; i++) {
        Sheet sheet = wb.createSheet(sheetNames[i]);
        Row row;
        Cell cell;
        if (headers[i].length > 0) {
            row = sheet.createRow(0);
            if (headerRow <= 0) {
                headerRow = HEADER_ROW;
            }
            headerRow = Math.min(headerRow, MAX_ROWS);
            for (int h = 0, lenH = headers[i].length; h < lenH; h++) {
                if (cellWidth > 0) {
                    sheet.setColumnWidth(h, cellWidth);
                }
                cell = row.createCell(h);
                cell.setCellValue(headers[i][h]);
            }
        }

        for (int j = 0, len = data[i].size(); j < len; j++) {
            row = sheet.createRow(j + headerRow);
            Object obj = data[i].get(j);
            if (obj == null) {
                continue;
            }
            if (obj instanceof Map) {
                processAsMap(columns[i], row, obj);
            } else if (obj instanceof Model) {
                processAsModel(columns[i], row, obj);
            } else if (obj instanceof Record) {
                processAsRecord(columns[i], row, obj);
            } else {
                throw new RuntimeException("Not support type[" + obj.getClass() + "]");
            }
        }
    }
    return wb;
}