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

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

Introduction

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

Prototype

void setCellStyle(CellStyle style);

Source Link

Document

Set the style for the cell.

Usage

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;// w w  w . j  a  va2  s.c  om

    if (n < 1)
        row = sheet.createRow(0);
    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  v  a  2s  .co  m*/
    cell1.setCellStyle(boldStyle);

    return cell1;
}

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

License:Open Source License

private Cell createInput(Sheet sheet, double value, String label, HSSFCellStyle style) {
    Cell cell = createInputCell(sheet, label);
    cell.setCellValue(value);/*from w w  w  .  j a  va  2s  .c om*/
    cell.setCellStyle(style);

    return cell;
}

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

License:Open Source License

private Cell createInput(Sheet sheet, int value, String label) {
    Cell cell = createInputCell(sheet, label);
    cell.setCellValue(value);/*from   w  w w .ja v a  2 s.c o  m*/
    cell.setCellStyle(intStyle);

    return cell;
}

From source file:com.dfpray.formatter.CardModel.java

/**
 * Exports List of B.C to Excel file, Path should include name and format .xlsx ending
 * @param path//  w ww  . ja v a  2 s  .  c o  m
 * @throws IOException 
 */
public void exportToExcel(String path) throws IOException {
    System.out.println("Called ");

    BusinessCard card;
    Cell cell;
    String[] info;
    Double number;
    String cardInfo;
    Row row;

    //Create Blank workbook/sheet
    @SuppressWarnings("resource")
    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheet = workbook.createSheet("Business Data");
    String[] tmpArray = { "CompanyName", "ContactFirstName", "ContactLastName", "Title", "Street Address",
            "Suite/PO Box", "City", "State", "ZipCode", "Country", "PhoneNumber", "Extension", "MobilePhone",
            "FaxNumber", "EmailAddress", "Website", "CsiCodes", "CompanyFunction", "MBEAffiliations", "Labor",
            "ServiceArea", "CompanyNotes", "ContactLists", "CF_Alternate Email", "CF_Do Not Use",
            "CF_Supplier/Manuf", "CF_Trade", "CF_Union Value", "CF_Unlicensed States", "CF_Will Not Bid" };

    Font headerFont = workbook.createFont();
    headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);

    XSSFCellStyle cellStyle = workbook.createCellStyle();
    cellStyle.setFont(headerFont);

    XSSFCellStyle cellStyle2 = workbook.createCellStyle();
    cellStyle2.setFont(headerFont);
    cellStyle2.setFillPattern(CellStyle.SOLID_FOREGROUND);
    cellStyle2.setFillForegroundColor(IndexedColors.YELLOW.getIndex());

    //Write Template

    row = sheet.createRow(0);
    for (int k = 0; k < 30; k++) {
        cell = row.createCell(k);
        cell.setCellStyle(cellStyle);

        if (k == 0 || k == 13 || k == 14 || k == 16 || k == 17) {
            cell.setCellStyle(cellStyle2);
        }

        cell.setCellValue(tmpArray[k]);
    }

    //Row = Business
    for (int i = 1; i <= amtCards(); i++) {
        row = sheet.createRow(i);
        card = cards.get(i - 1);
        info = card.infoToArray();

        //Create Column = Data for each Business
        for (int k = 0; k < 30; k++) {
            cardInfo = info[k];
            cell = row.createCell(k);

            if (k == 24)
                continue;

            try {
                number = Double.parseDouble(cardInfo);
                cell.setCellValue(number);
            } catch (NumberFormatException e) {
                cell.setCellValue(cardInfo);
            }

        }
        card.setExported(true);
    }

    //Create file system using specific name
    FileOutputStream out;
    try {
        out = new FileOutputStream(new File(path));
    } catch (FileNotFoundException e) {

        //Reset cards to not exported
        for (BusinessCard cardR : cards) {
            cardR.setExported(false);
        }
        throw new IOException();
    }
    workbook.write(out);
    out.close();

}

From source file:com.dituiba.excel.BaseExcelService.java

License:Apache License

/**
 * ?//from  w  w  w .jav a2  s.c o  m
 * @param cell
 */
public static void setErrorStyle(Cell cell) {
    if (cell != null) {
        CellStyle newstyle = cell.getSheet().getWorkbook().createCellStyle();
        CellStyle style = cell.getCellStyle();
        if (style != null) {
            newstyle.cloneStyleFrom(style);
        }
        newstyle.setFillForegroundColor(IndexedColors.RED.getIndex());
        newstyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
        cell.setCellStyle(newstyle);
    }
}

From source file:com.dituiba.excel.BaseExcelService.java

License:Apache License

/**
 * sheet ?//w  w w .j  a  v  a2  s .  co m
 * @param sheet
 * @param row
 * @param length
 * @param data
 */
public static void addTitle(Sheet sheet, int row, int length, String data) {
    if (data == null || data.equals("") || data.equals("null")) {
        return;
    }
    Row sheetRow = sheet.createRow(row);
    for (int i = 0; i < length; i++) {
        sheetRow.createCell(i);
    }
    CellStyle style = sheet.getWorkbook().createCellStyle(); // ?
    style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 
    CellRangeAddress cellRangeAddress = new CellRangeAddress(row, row, 0, length - 1);
    sheet.addMergedRegion(cellRangeAddress);
    Cell cell = sheetRow.getCell(0);
    cell.setCellStyle(style);
    cell.setCellValue(data);
}

From source file:com.dituiba.excel.BaseExcelService.java

License:Apache License

/**
 * ?/*from w w  w .j a  v a2 s .com*/
 * @param sheet
 * @param row
 * @param data
 * @return
 */
public static Row addRow(Sheet sheet, int row, String[] data) {
    Row sheetRow = sheet.createRow(row);
    CellStyle style = sheet.getWorkbook().createCellStyle(); // ?
    style.setWrapText(true);
    for (int i = 0; i < data.length; i++) {
        Cell cell = sheetRow.createCell(i);
        cell.setCellValue(data[i]);
        cell.setCellStyle(style);
    }
    return sheetRow;
}

From source file:com.dituiba.excel.DefaultOutputAdapter.java

License:Apache License

/**
 * ?// w  ww . jav  a  2 s .  com
 *
 * @param fieldValue
 * @param fieldName
 * @return
 * @throws AdapterException
 */
public void outputDateAdapter(DataBean dataBean, Object fieldValue, String fieldName, Cell cell)
        throws AdapterException {
    log.debug("in DefaultOutputAdapter:outputDateAdapter fieldName:{} fieldValue:{}", fieldName, fieldValue);
    Date date = null;
    if (fieldValue == null) {
        log.debug("fieldValue is null return");
        cell.setCellValue("");
        return;
    } else if (fieldValue instanceof Date) {
        log.debug("fieldValue instanceof Date ");
        date = (Date) fieldValue;
    } else if (fieldValue instanceof String) {
        log.debug("fieldValue instanceof String ");
        InputDateConfig config = dataBean.getInputConfig(fieldName);
        try {
            date = DateUtil.formatToDate((String) fieldValue, config.format());
        } catch (ParseException e) {
            throw new AdapterException(fieldName, Message.DATE_TYPE_ERROR, cell);
        }
    } else if (fieldValue instanceof Long) {
        log.debug("fieldValue instanceof Long ");
        date = new Date((Long) fieldValue);
    } else {
        throw new AdapterException(fieldName, Message.DATE_TYPE_ERROR, cell);
    }
    Workbook workbook = cell.getSheet().getWorkbook();
    OutputDateConfig outputConfig = dataBean.getOutputConfig(fieldName);
    CellStyle cellStyle = cell.getCellStyle();
    if (cellStyle == null)
        cellStyle = workbook.createCellStyle();
    CreationHelper createHelper = workbook.getCreationHelper();
    cellStyle.setDataFormat(createHelper.createDataFormat().getFormat(outputConfig.format()));
    cell.setCellStyle(cellStyle);
    cell.setCellValue(date);
}

From source file:com.dituiba.excel.DefaultOutputAdapter.java

License:Apache License

public void outputNumericAdapter(DataBean dataBean, Object fieldValue, String fieldName, Cell cell)
        throws AdapterException {
    log.debug("in DefaultOutputAdapter:outputNumericAdapter fieldName:{} fieldValue:{}", fieldName, fieldValue);
    if (ObjectHelper.isNullOrEmptyString(fieldValue))
        return;/*from  w  w w .  j  a  va2s.  c  om*/
    OutputNumericConfig config = dataBean.getOutputConfig(fieldName);
    Workbook workbook = cell.getSheet().getWorkbook();
    CellStyle cellStyle = workbook.createCellStyle();
    CreationHelper createHelper = workbook.getCreationHelper();
    StringBuilder format = new StringBuilder("0");
    for (int i = 0; i < config.floatCount(); i++) {
        if (i == 0)
            format.append(".");
        format.append("0");
    }
    cellStyle.setDataFormat(createHelper.createDataFormat().getFormat(format.toString()));
    cell.setCellValue(NumberUtils.format(fieldValue, config.floatCount()));
    cell.setCellStyle(cellStyle);
}