Example usage for org.apache.poi.hssf.usermodel HSSFCell getStringCellValue

List of usage examples for org.apache.poi.hssf.usermodel HSSFCell getStringCellValue

Introduction

In this page you can find the example usage for org.apache.poi.hssf.usermodel HSSFCell getStringCellValue.

Prototype

public String getStringCellValue() 

Source Link

Document

get the value of the cell as a string - for numeric cells we throw an exception.

Usage

From source file:com.beyondb.io.ExcelReader.java

/**
 * ????//from  w ww  .  j a va 2s  . c o m
 * 
 * @param cell Excel?
 * @return String ??
 */
private String getStringCellValue(HSSFCell cell) {
    String strCell = "";
    switch (cell.getCellType()) {
    case HSSFCell.CELL_TYPE_STRING:
        strCell = cell.getStringCellValue();
        break;
    case HSSFCell.CELL_TYPE_NUMERIC:
        strCell = String.valueOf(cell.getNumericCellValue());
        break;
    case HSSFCell.CELL_TYPE_BOOLEAN:
        strCell = String.valueOf(cell.getBooleanCellValue());
        break;
    case HSSFCell.CELL_TYPE_BLANK:
        strCell = "";
        break;
    default:
        strCell = "";
        break;
    }
    if (strCell.equals("") || strCell == null) {
        return "";
    }
    if (cell == null) {
        return "";
    }
    return strCell;
}

From source file:com.bluecubs.xinco.index.filetypes.XincoIndexMicrosoftExcel.java

License:Apache License

public String getFileContentString(File f) {
    int i, j, j2, k;
    short k2;/*from   w w  w .  java2  s. c om*/
    HSSFWorkbook wb = null;
    HSSFSheet sheet = null;
    HSSFRow row = null;
    HSSFCell cell = null;
    InputStream is = null;
    String cell_string = "";
    try {
        is = new FileInputStream(f);
        POIFSFileSystem fs = new POIFSFileSystem(is);
        wb = new HSSFWorkbook(fs);
        for (i = 0; i < wb.getNumberOfSheets(); i++) {
            sheet = wb.getSheetAt(i);
            j2 = 0;
            for (j = 0; j < sheet.getPhysicalNumberOfRows(); j++) {
                while ((row = sheet.getRow(j2)) == null) {
                    j2++;
                }
                j2++;
                k2 = 0;
                for (k = 0; k < row.getPhysicalNumberOfCells(); k++) {
                    while ((cell = row.getCell(k2)) == null) {
                        k2++;
                    }
                    k2++;
                    switch (cell.getCellType()) {
                    case HSSFCell.CELL_TYPE_FORMULA:
                        break;
                    case HSSFCell.CELL_TYPE_NUMERIC:
                        cell_string = cell_string + cell.getNumericCellValue() + "\t";
                        break;
                    case HSSFCell.CELL_TYPE_STRING:
                        cell_string = cell_string + cell.getStringCellValue() + "\t";
                        break;
                    default:
                    }

                }
                cell_string = cell_string + "\n";
            }
            cell_string = cell_string + "\n\n\n";
        }
        is.close();
    } catch (Exception fe) {
        cell_string = null;
        if (is != null) {
            try {
                is.close();
            } catch (Exception ise) {
            }
        }
    }
    return cell_string;
}

From source file:com.cms.utils.ExcelReader.java

public static void copyCell(HSSFCell oldCell, HSSFCell newCell, Map<Integer, HSSFCellStyle> styleMap) {
    if (styleMap != null) {
        if (oldCell.getSheet().getWorkbook() == newCell.getSheet().getWorkbook()) {
            newCell.setCellStyle(oldCell.getCellStyle());
        } else {//  w  ww  . jav  a2  s  .  c  om
            int stHashCode = oldCell.getCellStyle().hashCode();
            HSSFCellStyle newCellStyle = styleMap.get(stHashCode);
            if (newCellStyle == null) {
                newCellStyle = newCell.getSheet().getWorkbook().createCellStyle();
                newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
                styleMap.put(stHashCode, newCellStyle);
            }
            newCell.setCellStyle(newCellStyle);
        }
    }
    switch (oldCell.getCellType()) {
    case HSSFCell.CELL_TYPE_STRING:
        newCell.setCellValue(oldCell.getStringCellValue());
        break;
    case HSSFCell.CELL_TYPE_NUMERIC:
        newCell.setCellValue(oldCell.getNumericCellValue());
        break;
    case HSSFCell.CELL_TYPE_BLANK:
        newCell.setCellType(HSSFCell.CELL_TYPE_BLANK);
        break;
    case HSSFCell.CELL_TYPE_BOOLEAN:
        newCell.setCellValue(oldCell.getBooleanCellValue());
        break;
    case HSSFCell.CELL_TYPE_ERROR:
        newCell.setCellErrorValue(oldCell.getErrorCellValue());
        break;
    case HSSFCell.CELL_TYPE_FORMULA:
        newCell.setCellFormula(oldCell.getCellFormula());
        break;
    default:
        break;
    }

}

From source file:com.cn.util.ExcelImport.java

/**
* ?2003excel//w w w  .  java2s  .c  o  m
* @param file
* @return
*/
private static List<List<Object>> read2003Excel(InputStream inputStream) throws IOException {
    List<List<Object>> dataList = new ArrayList<>();
    HSSFWorkbook wb = new HSSFWorkbook(inputStream);
    HSSFSheet sheet = wb.getSheetAt(0);
    HSSFRow row = null;
    HSSFCell cell = null;
    Object val = null;
    DecimalFormat df = new DecimalFormat("0");// ?
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// ?

    for (int i = sheet.getFirstRowNum(); i < sheet.getPhysicalNumberOfRows(); i++) {
        row = sheet.getRow(i);
        if (row == null) {
            continue;
        }
        List<Object> objList = new ArrayList<>();
        for (int j = row.getFirstCellNum(); j < row.getLastCellNum(); j++) {
            cell = row.getCell(j);
            if (cell == null) {
                val = null;
                objList.add(val);
                continue;
            }
            switch (cell.getCellType()) {
            case HSSFCell.CELL_TYPE_STRING:
                val = cell.getStringCellValue();
                break;
            case HSSFCell.CELL_TYPE_NUMERIC:
                if ("@".equals(cell.getCellStyle().getDataFormatString())) {
                    val = df.format(cell.getNumericCellValue());
                } else if ("General".equals(cell.getCellStyle().getDataFormatString())) {
                    val = df.format(cell.getNumericCellValue());
                } else {
                    val = sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue()));
                }
                break;
            case HSSFCell.CELL_TYPE_BOOLEAN:
                val = cell.getBooleanCellValue();
                break;
            case HSSFCell.CELL_TYPE_BLANK:
                val = "";
                break;
            default:
                val = cell.toString();
                break;
            }
            objList.add(val);
        }
        dataList.add(objList);
    }
    return dataList;
}

From source file:com.cn.util.ExcelImport.java

/**
 * ?excel//from  w  w w  . jav a 2  s . c  o m
 * @param file
 * @return
 * @throws IOException
 */
public static String[] readExcelHead(File file) throws IOException {
    HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(file));
    HSSFSheet sheet = wb.getSheetAt(0);
    HSSFRow row = null;
    HSSFCell cell = null;
    row = sheet.getRow(0);
    String[] buff = new String[row.getLastCellNum()];
    for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) {
        cell = row.getCell(i);
        buff[i] = cell.getStringCellValue();
    }
    return buff;
}

From source file:com.codecrate.shard.transfer.excel.AbstractExcelRowHandler.java

License:Apache License

/**
 *
 * @param row/*from  w  w w . j a  v  a2s . c  om*/
 * @param column zero indexed column number
 * @return
 */
protected String getStringFromRow(HSSFRow row, int column) {
    HSSFCell nameCell = row.getCell((short) column);
    if (null == nameCell) {
        LOG.info("No string value found for [row, column]: " + row.getRowNum() + ", " + column);
        return null;
    }
    return nameCell.getStringCellValue();
}

From source file:com.comcast.cats.config.ui.monitoring.reboot.UpTimeAndRebootStatusBean.java

License:Open Source License

public void postProcessXls(Object document) {
    logger.trace("postProcessXls start document " + document);
    if (document != null) {
        HSSFWorkbook workBook = (HSSFWorkbook) document;
        HSSFSheet sheet = workBook.getSheetAt(0);

        HSSFRow headerRow = sheet.getRow(0);

        for (int i = 0; i < headerRow.getPhysicalNumberOfCells(); i++) {
            sheet.setColumnWidth(i, 30 * 265); // width for 40 characters
        }//from   w  w  w . j  ava2 s  .  c om

        sheet.shiftRows(0, sheet.getLastRowNum(), 5); // shift rows 0 to n
                                                      // by 1 to get space
                                                      // for header
        sheet.addMergedRegion(CellRangeAddress.valueOf("A1:F3"));

        HSSFFont headerFont = workBook.createFont();
        headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

        HSSFCellStyle headerCellStyle = workBook.createCellStyle();
        headerCellStyle.setFillForegroundColor(HSSFColor.LIGHT_CORNFLOWER_BLUE.index);
        headerCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        headerCellStyle.setFont(headerFont);
        headerCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        headerCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        HSSFCell headerCell = headerRow.createCell(0);
        headerCell.setCellStyle(headerCellStyle);
        headerCell.setCellValue("CATS Uptime and Reboot Status : " + (new Date()));

        HSSFCellStyle metaDataCellStyle = workBook.createCellStyle();
        metaDataCellStyle.setFont(headerFont);

        HSSFRow metaDataRow = sheet.getRow(3);
        if (metaDataRow == null) {
            metaDataRow = sheet.createRow(3);
        }
        HSSFCell metaDataKey = metaDataRow.createCell(0);
        metaDataKey.setCellStyle(metaDataCellStyle);
        metaDataKey.setCellValue("CATS Instance");

        HSSFCell metaDataValue = metaDataRow.createCell(1);
        metaDataValue.setCellStyle(metaDataCellStyle);
        metaDataValue.setCellValue(AuthController.getHostAddress());

        HSSFCellStyle datatTableHeaderCellStyle = workBook.createCellStyle();
        datatTableHeaderCellStyle.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
        datatTableHeaderCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        datatTableHeaderCellStyle.setFont(headerFont);

        HSSFRow actualDataTableHeaderRow = sheet.getRow(5);
        for (int i = 0; i < actualDataTableHeaderRow.getPhysicalNumberOfCells(); i++) {
            HSSFCell cell = actualDataTableHeaderRow.getCell(i);
            if (cell != null) {
                String cellValue = cell.getStringCellValue();
                cellValue = cellValue.replace("<br/> ", ""); // replace
                                                             // any line
                                                             // breaks
                cell.setCellValue(cellValue);
                cell.setCellStyle(datatTableHeaderCellStyle);
            }
        }

    }
    logger.trace("postProcessXls end");
}

From source file:com.concursive.connect.web.modules.plans.utils.AssignmentExcelImporter.java

License:Open Source License

/**
 * Gets the value attribute of the AssignmentImporter class
 *
 * @param cell Description of the Parameter
 * @return The value value/*from w w w . ja va 2 s  .  c o  m*/
 */
private static String getValue(HSSFCell cell) {
    if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
        return String.valueOf(cell.getNumericCellValue());
    }
    if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
        return String.valueOf(cell.getBooleanCellValue());
    }
    if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
        return cell.getStringCellValue().trim();
    }
    if (cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
        return "";
    }
    if (System.getProperty("DEBUG") != null) {
        System.out.println("AssignmentExcelImporter-> NONE: " + cell.getCellType());
    }
    try {
        return cell.getStringCellValue().trim();
    } catch (Exception e) {
    }
    try {
        return String.valueOf(cell.getNumericCellValue());
    } catch (Exception e) {
    }
    try {
        return String.valueOf(cell.getBooleanCellValue());
    } catch (Exception e) {
    }
    return null;
}

From source file:com.demo.common.extreme.view.XlsView.java

License:Apache License

private void fixWidthAndPopulate(HSSFCell cell, double numeric, String value) {
    int valWidth = 0;

    if (numeric != NON_NUMERIC) {
        cell.setCellValue(numeric);/*www.j  a  v  a  2  s .c  o  m*/
        valWidth = (cell.getNumericCellValue() + "$,.").length() * WIDTH_MULT;
    } else {
        cell.setCellValue(value);
        valWidth = (cell.getStringCellValue() + "").length() * WIDTH_MULT;

        if (valWidth < (WIDTH_MULT * MIN_CHARS)) {
            valWidth = WIDTH_MULT * MIN_CHARS;
        }
    }

    if (valWidth > sheet.getColumnWidth(cell.getCellNum())) {
        sheet.setColumnWidth(cell.getCellNum(), (short) valWidth);
    }
}

From source file:com.duroty.lucene.parser.MSExcelParser.java

License:Open Source License

/**
 * DOCUMENT ME!//  w w w .j av  a2  s.c  o  m
 *
 * @return DOCUMENT ME!
 *
 * @throws ParserException DOCUMENT ME!
 */
private String getContents() throws ParserException {
    String contents = "";

    try {
        POIFSFileSystem fs = new POIFSFileSystem(input);
        HSSFWorkbook workbook = new HSSFWorkbook(fs);
        StringBuffer buffer = new StringBuffer();

        for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
            HSSFSheet sheet = workbook.getSheetAt(i);

            Iterator rows = sheet.rowIterator();

            while (rows.hasNext()) {
                HSSFRow row = (HSSFRow) rows.next();

                Iterator cells = row.cellIterator();

                while (cells.hasNext()) {
                    HSSFCell cell = (HSSFCell) cells.next();

                    switch (cell.getCellType()) {
                    case HSSFCell.CELL_TYPE_NUMERIC:

                        String num = Double.toString(cell.getNumericCellValue()).trim();

                        if (num.length() > 0) {
                            buffer.append(num + " ");
                        }

                        break;

                    case HSSFCell.CELL_TYPE_STRING:

                        String text = cell.getStringCellValue().trim();

                        if (text.length() > 0) {
                            buffer.append(text + " ");
                        }

                        break;
                    }
                }

                buffer.append("\n");

                /*if (sleep > 0) {
                    try {
                        Thread.sleep(sleep);
                    } catch (Exception ex) {
                    }
                }*/
            }
        }

        contents = buffer.toString();
    } catch (Exception ex) {
        throw new ParserException(ex);
    }

    return contents;
}