Example usage for org.apache.poi.ss.usermodel Cell setCellValue

List of usage examples for org.apache.poi.ss.usermodel Cell setCellValue

Introduction

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

Prototype

void setCellValue(boolean value);

Source Link

Document

Set a boolean value for the cell

Usage

From source file:com.github.crab2died.handler.SheetTemplateHandler.java

License:Open Source License

/**
 * <p>Excel??</p>/*  w  ww . ja va  2  s.  co m*/
 *
 * @param value    
 * @param styleKey ?
 */
public static void createCell(SheetTemplate template, Object value, String styleKey) {
    Cell cell = template.currentRow.createCell(template.currentColumnIndex);
    setCellStyle(template, cell, styleKey);
    if (null == value || "".equals(value)) {
        template.currentColumnIndex++;
        return;
    }

    if (String.class == value.getClass()) {
        cell.setCellValue((String) value);
        template.currentColumnIndex++;
        return;
    }

    if (int.class == value.getClass()) {
        cell.setCellValue((int) value);
        template.currentColumnIndex++;
        return;
    }

    if (Integer.class == value.getClass()) {
        cell.setCellValue((Integer) value);
        template.currentColumnIndex++;
        return;
    }

    if (double.class == value.getClass()) {
        cell.setCellValue((double) value);
        template.currentColumnIndex++;
        return;
    }

    if (Double.class == value.getClass()) {
        cell.setCellValue((Double) value);
        template.currentColumnIndex++;
        return;
    }

    if (Date.class == value.getClass()) {
        cell.setCellValue((Date) value);
        template.currentColumnIndex++;
        return;
    }

    if (boolean.class == value.getClass()) {
        cell.setCellValue((boolean) value);
        template.currentColumnIndex++;
        return;
    }

    if (Boolean.class == value.getClass()) {
        cell.setCellValue((Boolean) value);
        template.currentColumnIndex++;
        return;
    }

    if (Calendar.class == value.getClass()) {
        cell.setCellValue((Calendar) value);
        template.currentColumnIndex++;
        return;
    }

    if (RichTextString.class == value.getClass()) {
        cell.setCellValue((RichTextString) value);
        template.currentColumnIndex++;
        return;
    }
    // default value#toString
    cell.setCellValue(value.toString());
    template.currentColumnIndex++;
}

From source file:com.github.cutstock.excel.model.SheetBuilder.java

License:Apache License

public SheetBuilder createColumns(ICellInfo columns) {
    IExcelRectangle rect = columns.getRect();
    int rowLine = rect.getStartRow();
    Row row = createRow(rowLine);/*from  w  w  w .  ja va 2 s .c o m*/
    // String colName = columns.getText();
    // String[] colNames = colName.split(",");
    Object[] colNames = columns.getColumns();
    for (int i = rect.getStartCol(), j = rect.getEndCol(), index = 0; i <= j; i++, index++) {
        Cell colCell = row.createCell(i);
        // cut num should cast to number 5,13
        if (colNames[index] instanceof BigDecimal || colNames[index] instanceof Integer) {
            colCell.setCellValue(Double.parseDouble(colNames[index].toString()));
            colCell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
        } else {
            colCell.setCellValue(colNames[index].toString());
        }
        CellStyle style = styles.get(columns.getCellType().typeValue());
        colCell.setCellStyle(style);
    }

    Row preRow = createRow(rowLine - 1);
    if (preRow != null) {
        Cell nameCel = preRow.getCell(rect.getStartCol());
        if (nameCel != null) {
            if (nameCel.getStringCellValue().equals(row.getCell(rect.getStartCol()).getStringCellValue())) {
                mergeRegion(ExcelModelFactory.createCellRect(rect.getStartCol(), rect.getStartCol(),
                        rowLine - 1, rowLine));
            }
        }
    }
    return this;
}

From source file:com.github.cutstock.excel.model.SheetBuilder.java

License:Apache License

public void createTitle(ICellInfo title) {
    IExcelRectangle titleRect = title.getRect();
    int row = titleRect.getStartRow();
    Row titleRow = createRow(row);/*from  w w w .  j  a v  a  2 s. c  om*/
    titleRow.setHeightInPoints(titleRect.getHeight());
    int startCol = titleRect.getStartCol();
    Cell titleCell = titleRow.getCell(startCol);
    if (titleCell == null) {
        titleCell = titleRow.createCell(startCol);
    }
    titleCell.setCellValue(title.getColumns()[0].toString());
    mergeRegion(titleRect);
    CellStyle style = styles.get(title.getCellType().typeValue());
    titleCell.setCellStyle(style);
}

From source file:com.github.gaborfeher.grantmaster.framework.base.ExcelExporter.java

License:Open Source License

private void setExcelCell(HSSFWorkbook workbook, Object cellValue, Cell excelCell) {
    if (cellValue instanceof BigDecimal) {
        double doubleValue = ((BigDecimal) cellValue).doubleValue();
        excelCell.setCellValue(doubleValue);
        HSSFCellStyle cellStyle = workbook.createCellStyle();
        HSSFDataFormat hssfDataFormat = workbook.createDataFormat();
        cellStyle.setDataFormat(hssfDataFormat.getFormat("#,##0.00"));
        excelCell.setCellStyle(cellStyle);
        excelCell.setCellType(Cell.CELL_TYPE_NUMERIC);
    } else if (cellValue instanceof LocalDate) {
        LocalDate localDate = (LocalDate) cellValue;
        Calendar calendar = Calendar.getInstance();
        calendar.set(localDate.getYear(), localDate.getMonthValue() - 1, localDate.getDayOfMonth());
        excelCell.setCellValue(calendar);

        String excelFormatPattern = DateFormatConverter.convert(Locale.US, "yyyy-MM-DD");
        CellStyle cellStyle = workbook.createCellStyle();
        DataFormat poiFormat = workbook.createDataFormat();
        cellStyle.setDataFormat(poiFormat.getFormat(excelFormatPattern));
        excelCell.setCellStyle(cellStyle);
    } else if (cellValue != null) {
        excelCell.setCellValue(cellValue.toString());
    }//from  ww  w .ja  v  a 2s.c o m
}

From source file:com.github.igor_kudryashov.utils.excel.ExcelWriter.java

License:Apache License

/**
 * Creates new row in the worksheet//from  www  . j a  va  2  s.  co  m
 *
 * @param sheet
 *            Sheet
 * @param values
 *            the value of the new cell line
 * @param header
 *            <code>true</code> if this row is the header, otherwise
 *            <code>false</code>
 * @param withStyle
 *            <code>true</code> if in this row will be applied styles for
 *            the cells, otherwise <code>false</code>
 * @return created row
 */
public Row createRow(Sheet sheet, Object[] values, boolean header, boolean withStyle) {
    Row row;
    String sheetName = sheet.getSheetName();
    int rownum = 0;
    if (rows.containsKey(sheetName)) {
        rownum = rows.get(sheetName);
    }
    // create new row
    row = sheet.createRow(rownum);
    // create a cells of row
    for (int x = 0; x < values.length; x++) {
        Object o = values[x];
        Cell cell = row.createCell(x);
        if (o != null) {
            if (o.getClass().getName().contains("String")) {
                String value = (String) values[x];
                cell.setCellValue(value);
            } else if (o.getClass().getName().contains("Double")) {
                cell.setCellValue((Double) values[x]);
            } else if (o.getClass().getName().contains("Integer")) {
                cell.setCellValue((Integer) values[x]);
            } else if (o.getClass().getName().contains("Date")) {
                cell.setCellValue((Date) values[x]);
            }
            if (withStyle) {
                cell.setCellStyle(getCellStyle(rownum, values[x], header));
            }
        }
        // save max column width
        if (!header) {
            saveColumnWidth(sheet, x, o);
        }
    }
    // save the last number of row for this worksheet
    rows.put(sheetName, ++rownum);
    return row;
}

From source file:com.github.jaydsolanki.excelio.ExcelIO.java

private boolean insertCell(Object obj, Sheet sheet, int rowNo, int cellNo) {

    if (sheet == null) {
        return false;
    }//from w w  w  .  j a  va 2  s .  com

    Row row = sheet.getRow(rowNo);
    if (row == null) {
        row = sheet.createRow(rowNo);
    }
    Cell cell = row.getCell(cellNo);
    if (cell == null) {
        cell = row.createCell(cellNo);
    }
    cell.setCellValue(obj.toString());
    return true;
}

From source file:com.github.jferard.spreadsheetwrapper.xls.poi.XlsPoiWriter.java

License:Open Source License

/**
 * {@inheritDoc}//from w  w w  .  j a  v  a2s  .co m
 *
 * @return
 */
@Override
public Boolean setBoolean(final int r, final int c, final Boolean value) {
    final Cell cell = this.getOrCreatePOICell(r, c);
    cell.setCellValue(value);
    return value;
}

From source file:com.github.jferard.spreadsheetwrapper.xls.poi.XlsPoiWriter.java

License:Open Source License

/**
 * @return/*from ww  w.j ava 2s  .c om*/
 */
@Override
public Date setDate(final int r, final int c, final Date date) {
    final Cell cell = this.getOrCreatePOICell(r, c);
    cell.setCellValue(date);
    if (this.dateCellStyle != null)
        cell.setCellStyle(this.dateCellStyle);
    return date;
}

From source file:com.github.jferard.spreadsheetwrapper.xls.poi.XlsPoiWriter.java

License:Open Source License

/**
 * @return/*from w  w  w  .  j  a va  2s.  c  o m*/
 */
@Override
public Double setDouble(final int r, final int c, final Number value) {
    final Cell cell = this.getOrCreatePOICell(r, c);
    final double retValue = value.doubleValue();
    cell.setCellValue(retValue);
    return retValue;
}

From source file:com.github.jferard.spreadsheetwrapper.xls.poi.XlsPoiWriter.java

License:Open Source License

/**
 * @return//w w w  .jav a  2s  .  co  m
 */
@Override
public Integer setInteger(final int r, final int c, final Number value) {
    final Cell cell = this.getOrCreatePOICell(r, c);
    final int retValue = value.intValue();
    cell.setCellValue(retValue);
    return retValue;
}