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.devnexus.ting.web.controller.admin.RegistrationController.java

License:Apache License

private void addTicketTypesToSheet(String[] ticketTypes, Sheet ticketTypeSheet) {
    for (int i = 0, length = ticketTypes.length; i < length; i++) {
        String ticketType = ticketTypes[i];
        Row row = ticketTypeSheet.createRow(i);
        Cell cell = row.createCell(0);
        cell.setCellValue(ticketType);
    }/*from  w w w .  jav a 2s  .  c o  m*/
}

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/*  www. j  a  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

/***
 * ?hash?Excel/* w  ww  .  j  a  v  a  2  s . co m*/
 * @param sheet
 * @param hashCode
 */
public static void setHashVal(Sheet sheet, long hashCode) {
    Row sheetRow = sheet.getRow(HASH_ROW);
    Cell cell = sheetRow.createCell(0);
    cell.setCellValue(hashCode);
    sheetRow.setHeight(Short.valueOf("0"));
}

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

License:Apache License

/**
 * sheet ?//from   w  ww .  j  a  va 2  s  .c o  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 ww w  .  j a v  a2s . c om
 * @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

/**
 * ?/* www .j av a2s  . c  o  m*/
 * @param fieldValue
 * @param fieldName
 * @param cell
 * @throws AdapterException
 */
public void outputDicCodeAdapter(DataBean dataBean, Object fieldValue, String fieldName, Cell cell)
        throws AdapterException {
    log.debug("in DefaultOutputAdapter:outputDicCodeAdapter fieldName:{} fieldValue:{}", fieldName, fieldValue);
    OutputDicConfig config = dataBean.getOutputConfig(fieldName);
    String dicCode = config.dicCode();
    if (fieldValue == null) {
        log.debug("fieldValue is null return");
        cell.setCellValue("");
        return;
    } else {
        String byKey = dicCodePool.getByKey(dicCode, fieldValue.toString());
        if (byKey == null) {
            if (AdapterUtil.getAllMatch(config)) {
                throw new AdapterException(Message.DIC_ERROR, cell);
            } else {
                cell.setCellValue(fieldValue.toString());
            }
        } else {
            cell.setCellValue(byKey);
        }
    }
}

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

License:Apache License

/**
 * ?/*from   ww w.  java  2s. c  om*/
 *
 * @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;//ww w . ja  v a  2 s .  c  o m
    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);
}

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

License:Apache License

public void outputIntAdapter(DataBean dataBean, Object fieldValue, String fieldName, Cell cell)
        throws AdapterException {
    log.debug("in DefaultOutputAdapter:outputIntAdapter fieldName:{} fieldValue:{}", fieldName, fieldValue);
    if (ObjectHelper.isNullOrEmptyString(fieldValue))
        return;//w ww .ja  v a  2 s  .c o m
    Workbook workbook = cell.getSheet().getWorkbook();
    CellStyle cellStyle = workbook.createCellStyle();
    CreationHelper createHelper = workbook.getCreationHelper();
    cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("#"));
    cell.setCellValue(NumberUtils.format(fieldValue, 0));
    cell.setCellStyle(cellStyle);
}

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

License:Apache License

/**
 * ?//w w  w .ja v a 2  s.co m
 *
 * @param fieldValue
 * @param fieldName
 * @return
 * @throws IllegalAccessException
 */
@DefaultAdapterMethod
public void defaultOutputAdapter(DataBean dataBean, Object fieldValue, String fieldName, Cell cell)
        throws AdapterException {
    log.debug("in DefaultOutputAdapter:defaultOutputAdapter fieldName:{} fieldValue:{}", fieldName, fieldValue);
    cell.setCellValue(fieldValue == null ? "" : fieldValue.toString());
}