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

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

Introduction

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

Prototype

RichTextString getRichStringCellValue();

Source Link

Document

Get the value of the cell as a XSSFRichTextString

For numeric cells we throw an exception.

Usage

From source file:com.tecacet.jflat.excel.PoiExcelReader.java

License:Apache License

private String getCellContentAsString(Cell cell) {
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        return cell.getRichStringCellValue().getString();
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            return cell.getDateCellValue().toString();
        } else {//from w ww. j av  a 2s  .  c  om
            double d = cell.getNumericCellValue();
            // TODO find a flexible enough format for all numeric types
            return numberFormat.format(d);
            // return Double.toString(d);
        }
    case Cell.CELL_TYPE_BOOLEAN:
        boolean b = cell.getBooleanCellValue();
        return Boolean.toString(b);
    case Cell.CELL_TYPE_FORMULA:
        return cell.getCellFormula();
    case Cell.CELL_TYPE_BLANK:
        return "";
    case Cell.CELL_TYPE_ERROR:
        byte bt = cell.getErrorCellValue();
        return Byte.toString(bt);
    default:
        return cell.getStringCellValue();

    }
}

From source file:com.vermeg.convertisseur.service.ConvServiceImpl.java

/**
 *
 * @param file//from   w  w  w . j  a v a  2  s  . c  o m
 * @return
 * @throws FileNotFoundException
 * @throws InvalidFormatException
 * @throws IOException
 */
/*this method convert a multipart file to json object */
@Override
public JSONObject convert(MultipartFile file, String name)
        throws FileNotFoundException, InvalidFormatException, IOException {

    // File file = new File("C:\\Users\\Ramzi\\Documents\\PFE\\developpement\\avancement.xlsx");

    File filez = File.createTempFile("fichier", "xslx");
    file.transferTo(filez);
    FileInputStream inp = new FileInputStream(filez);
    Workbook workbook = WorkbookFactory.create(inp);
    //Sheet sheet = workbook.getSheetAt( 0 );
    Sheet sheet = workbook.getSheet(name);
    // Start constructing JSON.
    JSONObject json = new JSONObject();

    // Iterate through the rows.
    JSONArray rows = new JSONArray();
    for (Iterator<Row> rowsIT = sheet.rowIterator(); rowsIT.hasNext();) {
        Row row = rowsIT.next();
        JSONObject jRow = new JSONObject();

        // Iterate through the cells.
        JSONArray cells = new JSONArray();
        for (Iterator<Cell> cellsIT = row.cellIterator(); cellsIT.hasNext();) {
            Cell cell = cellsIT.next();
            // System.out.println(cell.getCellType());
            //           cells.put(cell.getDateCellValue());
            switch (cell.getCellType()) {
            case Cell.CELL_TYPE_STRING:
                cells.put(cell.getRichStringCellValue().getString());
                break;
            case Cell.CELL_TYPE_NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    cells.put(cell.getDateCellValue());
                } else {
                    cells.put(cell.getNumericCellValue());
                }
                break;
            case Cell.CELL_TYPE_BOOLEAN:
                cells.put(cell.getBooleanCellValue());
                break;
            case Cell.CELL_TYPE_FORMULA:
                cells.put(cell.getCellFormula());
                break;
            default:
                System.out.println();
            }
        }
        jRow.put("cell", cells);
        rows.put(cells);
        //rows.put( jRow );
    }

    // Create the JSON.
    json.put("rows", rows);
    System.out.println(json.toString());
    return json;
}

From source file:com.vertec.daoimpl.AttendanceDAOImpl.java

public List<Object> readexcel(String path) {
    //        String data = "";
    List<Object> table = new ArrayList<Object>();
    try {//from www .  j  ava  2s  .com
        // Get the workbook instance for XLSX file
        XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(path));

        // Get first sheet from the workbook
        XSSFSheet sheet = wb.getSheetAt(0);

        Row row;
        Cell cell;

        // Iterate through each rows from first sheet
        Iterator<Row> rowIterator = sheet.iterator();

        while (rowIterator.hasNext()) {
            row = rowIterator.next();

            // For each row, iterate through each columns
            Iterator<Cell> cellIterator = row.cellIterator();
            List<String> rows = new ArrayList<String>();
            while (cellIterator.hasNext()) {
                cell = cellIterator.next();

                switch (cell.getCellType()) {
                case Cell.CELL_TYPE_STRING:
                    rows.add(cell.getRichStringCellValue().getString());
                    //                            data+=cell.getRichStringCellValue().getString();
                    //                            System.out.print(cell.getRichStringCellValue().getString());
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    if (DateUtil.isCellDateFormatted(cell)) {

                        rows.add(cell.getDateCellValue() + "");
                        //                                data+=cell.getDateCellValue();
                        //                                System.out.print(cell.getDateCellValue());
                    } else {
                        rows.add(cell.getNumericCellValue() + "");
                        //                                data+=cell.getNumericCellValue();
                        //                                System.out.print(cell.getNumericCellValue());
                    }
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    rows.add(cell.getBooleanCellValue() + "");
                    //                            data+=cell.getBooleanCellValue();
                    //                            System.out.print(cell.getBooleanCellValue());
                    break;
                case Cell.CELL_TYPE_FORMULA:
                    rows.add(cell.getCellFormula() + "");
                    //                            data+=cell.getCellFormula();
                    //                            System.out.print(cell.getCellFormula());
                    break;
                default:
                    //                            System.out.print("");
                }
                //                    data += "-";
                //                    System.out.print(" - ");
            }
            table.add(rows);
            //                data += ";;;";
            //                System.out.println(";;;");
        }
    } catch (Exception e) {
        System.err.println("Exception :" + e.getMessage());
    }
    return table;
}

From source file:com.vertec.daoimpl.AttendanceDAOImpl.java

public String readexcel2(String path) {
    try {//from ww w .  ja  v a2 s .c  o m
        // Get the workbook instance for XLSX file
        XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(path));

        // Get first sheet from the workbook
        XSSFSheet sheet = wb.getSheetAt(0);

        Row row;
        Cell cell;

        // Iterate through each rows from first sheet
        Iterator<Row> rowIterator = sheet.iterator();

        while (rowIterator.hasNext()) {
            row = rowIterator.next();

            // For each row, iterate through each columns
            Iterator<Cell> cellIterator = row.cellIterator();

            while (cellIterator.hasNext()) {
                cell = cellIterator.next();

                switch (cell.getCellType()) {
                case Cell.CELL_TYPE_STRING:
                    System.out.print(cell.getRichStringCellValue().getString());
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    if (DateUtil.isCellDateFormatted(cell)) {
                        System.out.print(cell.getDateCellValue());
                    } else {
                        System.out.print(cell.getNumericCellValue());
                    }
                    break;
                case Cell.CELL_TYPE_BOOLEAN:
                    System.out.print(cell.getBooleanCellValue());
                    break;
                case Cell.CELL_TYPE_FORMULA:
                    System.out.print(cell.getCellFormula());
                    break;
                default:
                    System.out.print("");
                }
                System.out.print(" - ");
            }
            System.out.println(";;;");
        }
    } catch (Exception e) {
        System.err.println("Exception :" + e.getMessage());
    }
    return null;
}

From source file:com.verticon.treatment.poi.handlers.PoiUtils.java

License:Open Source License

static boolean isWorkSheetMatch(String[] header, HSSFSheet workSheet) {
    int rowNum = workSheet.getFirstRowNum();
    Row row = workSheet.getRow(rowNum);/*from   w w w .  j  ava 2  s .co m*/
    for (int i = 0; i < header.length; i++) {
        Cell cell = row.getCell(i);
        // Must be a String
        if (cell.getCellType() != Cell.CELL_TYPE_STRING) {
            System.out.println(
                    "Header Column Type Error: " + cell.getCellType() + " != " + Cell.CELL_TYPE_STRING);
            return false;
        }
        if (!header[i].equalsIgnoreCase(cell.getRichStringCellValue().getString())) {
            // System.out.println("Header Column Name Error: " + header[i]
            // + " != " + cell.getRichStringCellValue().getString());
            return false;
        }

    }
    return true;
}

From source file:com.wabacus.system.dataimport.filetype.XlsFileProcessor.java

License:Open Source License

private Object getCellValue(Cell cell) {
    if (cell == null)
        return null;
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        return cell.getRichStringCellValue().getString();
    case Cell.CELL_TYPE_BOOLEAN:
        return String.valueOf(cell.getBooleanCellValue());
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            return cell.getDateCellValue();
        } else {//from www  .j a  v a2  s  .c o  m
            return String.valueOf(cell.getNumericCellValue());
            /*double d=cell.getNumericCellValue();
            if(d-(int)d<Double.MIN_VALUE)
            { // ?int  
                return (int)d;
            }else
            { 
                return cell.getNumericCellValue();
            }*/
        }
    case Cell.CELL_TYPE_BLANK:
        return "";
    default:
        return null;
    }
}

From source file:com.xn.interfacetest.service.impl.TestCaseServiceImpl.java

License:Open Source License

/**
 * ?Cell?//from   www . j a va2s. c o m
 * @param cell
 * @return
 */
private Object getCellFormatValue(Cell cell) {
    if (null == cell) {
        return "";
    }
    DataFormatter formatter = new DataFormatter();
    switch (cell.getCellTypeEnum()) {
    case STRING:
        return cell.getRichStringCellValue().getString();
    case NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            return cell.getDateCellValue();
        } else {
            return Math.round(cell.getNumericCellValue());
        }
    case BOOLEAN:
        return cell.getBooleanCellValue();
    case FORMULA:
        return cell.getCellFormula();
    case BLANK:
        return "";
    default:
        return "";
    }

}

From source file:com.yyl.common.utils.excel.ExcelTools.java

/**
 * ?excel//from w  ww .  ja va  2 s  .  c o m
 * @param cell
 * @return
 */
private static String getCellValue(Cell cell) {
    String cellValue = "";
    DecimalFormat df = new DecimalFormat("#");
    if (cell == null || cell.equals("") || cell.getCellType() == Cell.CELL_TYPE_BLANK) {
        System.out.println(cellValue);
        return cellValue;
    }
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        cellValue = cell.getRichStringCellValue().getString().trim();
        break;
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            cellValue = cell.getDateCellValue().toString();
        } else {
            cellValue = df.format(cell.getNumericCellValue()).toString();
        }
        break;
    case Cell.CELL_TYPE_BOOLEAN:
        cellValue = String.valueOf(cell.getBooleanCellValue()).trim();
        break;
    default:
        cellValue = "";
    }
    return cellValue;
}

From source file:common.ReadExcelData.java

License:Apache License

public String getCellValue(int index, String heading) {
    String cellValue = "";
    try {/*from ww  w  .  ja  v  a2  s .  co  m*/
        sheet = workbook.getSheet(sheetName);
        row = sheet.getRow(0);
        int cellNumber = 0;
        for (Cell cell : row) {
            if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
                if (cell.getRichStringCellValue().getString().trim().equals(heading)) {
                    cellNumber = cell.getColumnIndex();
                }
            }
        }
        row = sheet.getRow(findRow(sheet, index));
        cell = row.getCell(cellNumber);
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_NUMERIC:
            cellValue = String.valueOf(((long) cell.getNumericCellValue()));
            break;
        case Cell.CELL_TYPE_STRING:
            cellValue = cell.getStringCellValue();
            break;
        case Cell.CELL_TYPE_BOOLEAN:
            cellValue = String.valueOf(cell.getBooleanCellValue());
            break;
        case Cell.CELL_TYPE_BLANK:
            cellValue = null;
        }
    } catch (NullPointerException e) {
        cellValue = null;
    }
    return cellValue;
}

From source file:das.pf.io.IOExcel.java

License:Open Source License

private void createHeaderValues(Sheet sheet, Sheet source, CellStyle style, int start, int end,
        int indexSource) {
    Row rowSource = source.getRow(9);/* w  w w .  j  a  va2 s . com*/
    Row row = sheet.getRow(1) != null ? sheet.getRow(1) : sheet.createRow(1);

    for (int index = start; index < end; index++) {
        Cell cellSource = rowSource.getCell(indexSource++);
        Cell cell = row.createCell(index);

        cell.setCellType(Cell.CELL_TYPE_STRING);
        cell.setCellValue(cellSource.getRichStringCellValue());

        cell.setCellStyle(style);

        cell = null;
        cellSource = null;
    }

    row = null;
    rowSource = null;
}