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.adobe.acs.commons.mcp.impl.ProcessErrorReportExcelServlet.java

License:Apache License

@SuppressWarnings("squid:S3776")
protected Workbook createSpreadsheet(ManagedProcess report) {
    Workbook wb = new XSSFWorkbook();

    String name = report.getName();
    for (char ch : new char[] { '\\', '/', '*', '[', ']', ':', '?' }) {
        name = StringUtils.remove(name, ch);
    }//from   ww w .  j  a va  2 s.  c om
    Sheet sheet = wb.createSheet(name);
    sheet.createFreezePane(0, 1, 0, 1);

    Row headerRow = sheet.createRow(0);
    CellStyle headerStyle = createHeaderStyle(wb);
    CellStyle dateStyle = wb.createCellStyle();
    CreationHelper createHelper = wb.getCreationHelper();
    dateStyle.setDataFormat(createHelper.createDataFormat().getFormat("yyy/mm/dd h:mm:ss"));

    for (String columnName : Arrays.asList("Time", "Path", "Error", "Stack trace")) {
        Cell headerCell = headerRow.createCell(headerRow.getPhysicalNumberOfCells());
        headerCell.setCellValue(columnName);
        headerCell.setCellStyle(headerStyle);
    }

    Collection<ArchivedProcessFailure> rows = report.getReportedErrorsList();
    //make rows, don't forget the header row
    for (ArchivedProcessFailure error : rows) {
        Row row = sheet.createRow(sheet.getPhysicalNumberOfRows());
        Cell c;

        c = row.createCell(0);
        c.setCellValue(error.time);
        c.setCellStyle(dateStyle);
        c = row.createCell(1);
        c.setCellValue(error.nodePath);
        c = row.createCell(2);
        c.setCellValue(error.error);
        c = row.createCell(3);
        c.setCellValue(error.stackTrace);
    }
    autosize(sheet, 4);
    sheet.setAutoFilter(new CellRangeAddress(0, 1 + rows.size(), 0, 3));
    return wb;
}

From source file:com.aistor.common.utils.excel.ExportExcel.java

License:Open Source License

/**
 * ?/*from   w w  w  .  j  a  v a 2s .c  om*/
 * @param row 
 * @param column ?
 * @param val 
 * @param align ??1?23??
 * @return ?
 */
public Cell addCell(Row row, int column, Object val, int align, Class<?> fieldType) {
    Cell cell = row.createCell(column);
    CellStyle style = wb.createCellStyle();
    style.cloneStyleFrom(styles.get("data"));
    if (align == 1) {
        style.setAlignment(CellStyle.ALIGN_LEFT);
    } else if (align == 2) {
        style.setAlignment(CellStyle.ALIGN_CENTER);
    } else if (align == 3) {
        style.setAlignment(CellStyle.ALIGN_RIGHT);
    }
    try {
        if (val == null) {
            cell.setCellValue("");
        } else if (val instanceof String) {
            cell.setCellValue((String) val);
        } else if (val instanceof Integer) {
            cell.setCellValue((Integer) val);
        } else if (val instanceof Long) {
            cell.setCellValue((Long) val);
        } else if (val instanceof Double) {
            cell.setCellValue((Double) val);
        } else if (val instanceof Float) {
            cell.setCellValue((Float) val);
        } else if (val instanceof Date) {
            DataFormat format = wb.createDataFormat();
            style.setDataFormat(format.getFormat("yyyy-MM-dd"));
            cell.setCellValue((Date) val);
        } else {
            if (fieldType != Class.class) {
                cell.setCellValue((String) fieldType.getMethod("setValue", Object.class).invoke(null, val));
            } else {
                cell.setCellValue((String) Class
                        .forName(this.getClass().getName().replaceAll(this.getClass().getSimpleName(),
                                "fieldtype." + val.getClass().getSimpleName() + "Type"))
                        .getMethod("setValue", Object.class).invoke(null, val));
            }
        }
    } catch (Exception ex) {
        log.info("Set cell value [" + row.getRowNum() + "," + column + "] error: " + ex.toString());
        cell.setCellValue(val.toString());
    }
    cell.setCellStyle(style);
    return cell;
}

From source file:com.alibaba.ims.platform.util.ExcelUtil.java

License:Open Source License

private static void write(List<List<String>> rows, File file) throws IOException {
    Workbook workbook = new XSSFWorkbook();
    Sheet sheet = workbook.createSheet("sheet1");

    for (int i = 0; i < rows.size(); i++) {
        Row row = sheet.createRow(i);//from   w w  w .j ava 2s. c o  m
        for (int j = 0; j < rows.get(i).size(); j++) {
            Cell cell = row.createCell(j);
            cell.setCellValue(rows.get(i).get(j));
        }
    }

    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(file);
        workbook.write(fos);
    } finally {
        if (fos != null) {
            IOUtils.closeQuietly(fos);
        }
    }
}

From source file:com.anritsu.mcrepositorymanager.utils.GenerateRSS.java

public String getRSS() {
    FileInputStream file = null;/*from  ww  w  .  j av a  2 s  . c om*/
    String rssFileName = rssTemplateFileName.replaceAll("template", mcVersion);
    try {
        file = new FileInputStream(
                new File(Configuration.getInstance().getRssTemplatePath() + rssTemplateFileName));
        XSSFWorkbook workbook = new XSSFWorkbook(file);
        workbook.setSheetName(workbook.getSheetIndex("MC X.X.X"), "MC " + mcVersion);
        XSSFSheet sheet = workbook.getSheet("MC " + mcVersion);
        CreationHelper createHelper = workbook.getCreationHelper();

        Cell cell = null;

        // Update the sheet title
        cell = sheet.getRow(0).getCell(0);
        cell.setCellValue(cell.getStringCellValue().replaceAll("template", mcVersion));

        XSSFCellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);
        cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);

        XSSFCellStyle hlinkstyle = workbook.createCellStyle();
        XSSFFont hlinkfont = workbook.createFont();
        hlinkfont.setUnderline(XSSFFont.U_SINGLE);
        hlinkfont.setColor(HSSFColor.BLUE.index);
        hlinkstyle.setFont(hlinkfont);
        hlinkstyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);
        hlinkstyle.setBorderTop(XSSFCellStyle.BORDER_THIN);
        hlinkstyle.setBorderRight(XSSFCellStyle.BORDER_THIN);
        hlinkstyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);

        XSSFCellStyle dateCellStyle = workbook.createCellStyle();
        dateCellStyle.setDataFormat(createHelper.createDataFormat().getFormat("dd-MMMM-yyyy"));
        dateCellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);
        dateCellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);
        dateCellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);
        dateCellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);

        // Populate the table
        int rowCount = 1;
        for (RecommendedMcPackage rmcp : sortedMcPackages) {
            if (rmcp.getRecommendedVersion() != null && rmcp.isShowInTable()) {
                Row row = sheet.createRow(rowCount + 1);
                rowCount++;

                cell = row.createCell(0);
                cell.setCellValue(rmcp.getTier().replaceAll("Anritsu/MasterClaw/", ""));
                cell.setCellStyle(cellStyle);

                cell = row.createCell(1);
                cell.setCellValue(rmcp.getGroup());
                cell.setCellStyle(cellStyle);

                cell = row.createCell(2);
                cell.setCellValue(rmcp.getPackageName());

                UrlValidator defaultValidator = new UrlValidator(UrlValidator.ALLOW_LOCAL_URLS);

                if (rmcp.getRecommendedVersion().getReleaseNote() != null
                        && defaultValidator.isValid(rmcp.getRecommendedVersion().getReleaseNote())) {
                    XSSFHyperlink releaseNotelink = (XSSFHyperlink) createHelper
                            .createHyperlink(Hyperlink.LINK_URL);
                    releaseNotelink.setAddress(rmcp.getRecommendedVersion().getReleaseNote());
                    //System.out.println("Inside(if) RN: " + rmcp.getRecommendedVersion().getReleaseNote() + " Valid: " + defaultValidator.isValid(rmcp.getRecommendedVersion().getReleaseNote()));

                    cell.setHyperlink(releaseNotelink);
                }
                cell.setCellStyle(hlinkstyle);

                cell = row.createCell(3);
                cell.setCellValue(rmcp.getRecommendedVersion().getPackageVersion());
                cell.setCellStyle(cellStyle);

                cell = row.createCell(4);
                cell.setCellValue(rmcp.getAvailability());
                cell.setCellStyle(cellStyle);

                cell = row.createCell(5);
                String customers = Arrays.asList(rmcp.getRecommendedVersion().getCustomerList().toArray())
                        .toString();
                if (customers.equalsIgnoreCase("[All]")) {
                    customers = "";
                }
                cell.setCellValue(customers);
                cell.setCellStyle(cellStyle);

                cell = row.createCell(6);
                cell.setCellValue(rmcp.getRecommendedVersion().getRisk());
                cell.setCellStyle(cellStyle);

                cell = row.createCell(7);
                cell.setCellValue(rmcp.getPackageName());
                XSSFHyperlink link = (XSSFHyperlink) createHelper.createHyperlink(Hyperlink.LINK_URL);
                link.setAddress(rmcp.getRecommendedVersion().getDownloadLinks().iterator().next());
                cell.setHyperlink((XSSFHyperlink) link);
                cell.setCellStyle(hlinkstyle);

                cell = row.createCell(8);
                cell.setCellValue((rmcp.getRecommendedVersion() != null
                        && rmcp.getRecommendedVersion().isLessRecommended()) ? "#" : "");
                cell.setCellStyle(cellStyle);

                cell = row.createCell(9);
                cell.setCellValue(rmcp.getRecommendedVersion().getNotes());
                cell.setCellStyle(cellStyle);

                StringBuilder newFeatures = new StringBuilder();
                for (MCPackageActivities mcpa : rmcp.getRecommendedVersion().getActivities()) {
                    if (!mcpa.getActivityType().equalsIgnoreCase("epr")) {
                        newFeatures.append(mcpa.getActivityType() + " " + mcpa.getActivityId() + "; ");
                    }
                }
                cell = row.createCell(10);
                cell.setCellValue(newFeatures.toString());
                cell.setCellStyle(cellStyle);

                cell = row.createCell(11);
                cell.setCellValue(rmcp.getRecommendedVersion().getReleaseDate());
                cell.setCellStyle(dateCellStyle);
            }
            sheet.autoSizeColumn(0);
            sheet.autoSizeColumn(1);
            sheet.autoSizeColumn(2);
            sheet.autoSizeColumn(3);
            sheet.autoSizeColumn(4);
            sheet.autoSizeColumn(6);
            sheet.autoSizeColumn(7);
            sheet.autoSizeColumn(8);
            sheet.autoSizeColumn(11);

        }

        FileOutputStream outFile = new FileOutputStream(
                new File(Configuration.getInstance().getRssTemplatePath() + rssFileName));
        workbook.write(outFile);
        outFile.close();
        return Configuration.getInstance().getRssTemplatePath() + rssFileName;

    } catch (FileNotFoundException ex) {
        Logger.getLogger(GenerateRSS.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(GenerateRSS.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            file.close();
        } catch (IOException ex) {
            Logger.getLogger(GenerateRSS.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    return "";
}

From source file:com.asakusafw.testdata.generator.excel.SheetEditor.java

License:Apache License

/**
 * Creates a data sheet with specified name.
 * @param name sheet name/*from   w w  w  .j a  v  a 2s  . c  o  m*/
 * @param model the target model
 * @throws IllegalArgumentException if some parameters were {@code null}
 */
public void addData(String name, ModelDeclaration model) {
    if (name == null) {
        throw new IllegalArgumentException("name must not be null"); //$NON-NLS-1$
    }
    if (model == null) {
        throw new IllegalArgumentException("model must not be null"); //$NON-NLS-1$
    }
    Sheet sheet = info.workbook.createSheet(name);
    Row titleRow = sheet.createRow(0);
    Row valueRow = sheet.createRow(1);
    int index = 0;
    for (PropertyDeclaration property : model.getDeclaredProperties()) {
        if (index >= info.version.getMaxColumns()) {
            LOG.warn(MessageFormat.format(Messages.getString("SheetEditor.warnExceedColumnCount"), //$NON-NLS-1$
                    info.version.getMaxColumns(), model.getName()));
            break;
        }
        Cell title = titleRow.createCell(index);
        title.setCellStyle(info.titleStyle);
        title.setCellValue(property.getName().identifier);

        Cell value = valueRow.createCell(index);
        value.setCellStyle(info.dataStyle);
        if (property.getType() instanceof BasicType) {
            BasicType type = (BasicType) property.getType();
            switch (type.getKind()) {
            case DATE:
                value.setCellStyle(info.dateDataStyle);
                break;
            case DATETIME:
                value.setCellStyle(info.datetimeDataStyle);
                break;
            default:
                break;
            }
        }
        index++;
    }
    adjustDataWidth(sheet);
}

From source file:com.asakusafw.testdata.generator.excel.SheetEditor.java

License:Apache License

private void fillRuleFormat(Sheet sheet) {
    assert sheet != null;
    Cell value = getCell(sheet, RuleSheetFormat.FORMAT, 0, 1);
    value.setCellStyle(info.lockedStyle);
    value.setCellValue(RuleSheetFormat.FORMAT_VERSION);
}

From source file:com.asakusafw.testdata.generator.excel.SheetEditor.java

License:Apache License

private void fillRuleTotalCondition(Sheet sheet) {
    assert sheet != null;
    Cell value = getCell(sheet, RuleSheetFormat.TOTAL_CONDITION, 0, 1);
    value.setCellStyle(info.optionsStyle);
    String[] options = TotalConditionKind.getOptions();
    value.setCellValue(options[0]);
    setExplicitListConstraint(sheet, options, value.getRowIndex(), value.getColumnIndex(), value.getRowIndex(),
            value.getColumnIndex());/* w  w w. ja  v a2 s  .co  m*/
}

From source file:com.asakusafw.testdata.generator.excel.SheetEditor.java

License:Apache License

private void setTitle(Sheet sheet, RuleSheetFormat item) {
    assert sheet != null;
    assert item != null;
    Cell cell = getCell(sheet, item.getRowIndex(), item.getColumnIndex());
    cell.setCellStyle(info.titleStyle);/*ww w.ja va2  s  . c  om*/
    cell.setCellValue(item.getTitle());
}

From source file:com.asakusafw.testdata.generator.excel.SheetEditor.java

License:Apache License

private void fillRulePropertyConditions(Sheet sheet, ModelDeclaration model) {
    int index = 1;
    for (PropertyDeclaration property : model.getDeclaredProperties()) {
        Cell name = getCell(sheet, RuleSheetFormat.PROPERTY_NAME, index, 0);
        name.setCellStyle(info.lockedStyle);
        name.setCellValue(property.getName().identifier);

        Cell value = getCell(sheet, RuleSheetFormat.VALUE_CONDITION, index, 0);
        value.setCellStyle(info.optionsStyle);
        if (index == 1) {
            value.setCellValue(ValueConditionKind.KEY.getText());
        } else {//from ww w .ja  v  a2s.c  om
            value.setCellValue(ValueConditionKind.ANY.getText());
        }

        Cell nullity = getCell(sheet, RuleSheetFormat.NULLITY_CONDITION, index, 0);
        nullity.setCellStyle(info.optionsStyle);
        nullity.setCellValue(NullityConditionKind.NORMAL.getText());

        Cell comments = getCell(sheet, RuleSheetFormat.COMMENTS, index, 0);
        comments.setCellStyle(info.dataStyle);
        comments.setCellValue(property.getDescription() == null ? property.getType().toString()
                : property.getDescription().getText());

        Cell options = getCell(sheet, RuleSheetFormat.EXTRA_OPTIONS, index, 0);
        options.setCellStyle(info.dataStyle);

        index++;
    }

    int start = RuleSheetFormat.PROPERTY_NAME.getRowIndex() + 1;
    int end = RuleSheetFormat.PROPERTY_NAME.getRowIndex() + index;
    setExplicitListConstraint(sheet, ValueConditionKind.getOptions(), start,
            RuleSheetFormat.VALUE_CONDITION.getColumnIndex(), end,
            RuleSheetFormat.VALUE_CONDITION.getColumnIndex());
    setExplicitListConstraint(sheet, NullityConditionKind.getOptions(), start,
            RuleSheetFormat.NULLITY_CONDITION.getColumnIndex(), end,
            RuleSheetFormat.NULLITY_CONDITION.getColumnIndex());
}

From source file:com.axelor.apps.admin.service.ViewDocExportService.java

License:Open Source License

private void writeCell(XSSFRow row, Integer oldRowIndex, int count, XSSFCellStyle cellStyle) {

    XSSFRow oldRow = oldSheet.getRow(oldRowIndex);

    while (count < oldRow.getLastCellNum()) {
        XSSFCell oldCell = oldRow.getCell(count);
        Cell cell = row.createCell(count);
        cell.setCellStyle(cellStyle);//from   www.j  a  v  a  2  s  .c om
        cell.setCellValue(oldCell.getStringCellValue());
        count++;
    }

}