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:org.talend.mdm.webapp.browserecords.server.service.UploadService.java

License:Open Source License

protected String[] readHeader(Row headerRow, String[] headerRecord) throws UploadException {
    List<String> headers = new LinkedList<String>();
    String header;// w  w  w .  j  a  va 2  s  .c om
    int index = 0;
    if (headersOnFirstLine) {
        if (FILE_TYPE_EXCEL_SUFFIX.equals(fileType.toLowerCase())
                || FILE_TYPE_EXCEL2010_SUFFIX.equals(fileType.toLowerCase())) {
            Iterator<Cell> headerIterator = headerRow.cellIterator();
            while (headerIterator.hasNext()) {
                Cell cell = headerIterator.next();
                if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
                    header = cell.getRichStringCellValue().getString();
                    headers.add(handleHeader(header, index));
                }
                index++;
            }
        } else if (FILE_TYPE_CSV_SUFFIX.equals(fileType.toLowerCase())) {
            for (String element : headerRecord) {
                headers.add(handleHeader(element, index));
                index++;
            }
        }
    } else {
        Iterator<String> headerIterator = headerVisibleMap.keySet().iterator();
        while (headerIterator.hasNext()) {
            header = headerIterator.next();
            registXsiType(header, index);
            headers.add(header);
            index++;
        }
    }
    return headers.toArray(new String[headers.size()]);
}

From source file:org.talend.mdm.webapp.browserecords.server.service.UploadService.java

License:Open Source License

protected String getExcelFieldValue(Cell cell) throws Exception {
    String fieldValue = null;//from   www . j a v  a2 s.  c o m
    int cellType = cell.getCellType();
    switch (cellType) {
    case Cell.CELL_TYPE_NUMERIC: {
        double tmp = cell.getNumericCellValue();
        fieldValue = getStringRepresentation(tmp);
        break;
    }
    case Cell.CELL_TYPE_STRING: {
        fieldValue = cell.getRichStringCellValue().getString();
        break;
    }
    case Cell.CELL_TYPE_BOOLEAN: {
        boolean tmp = cell.getBooleanCellValue();
        if (tmp) {
            fieldValue = "true"; //$NON-NLS-1$
        } else {
            fieldValue = "false";//$NON-NLS-1$
        }
        break;
    }
    case Cell.CELL_TYPE_FORMULA: {
        fieldValue = cell.getCellFormula();
        break;
    }
    case Cell.CELL_TYPE_ERROR: {
        break;
    }
    case Cell.CELL_TYPE_BLANK: {
        fieldValue = ""; //$NON-NLS-1$
    }
    default: {
    }
    }
    return fieldValue;
}

From source file:org.unhcr.eg.odk.utilities.xlsform.excel.ExcelFileUtility.java

protected static void copyContent(Sheet sheetSource, Sheet sheetDestination) {
    //Iterate through each rows from first sheet
    Iterator<Row> rowIterator = sheetSource.iterator();
    int i = 0;//from   w w  w.  java 2  s  .  c  o  m
    while (rowIterator.hasNext()) {
        Row row = rowIterator.next();
        Row rowDestination = sheetDestination.createRow(i);
        i++;
        //For each row, iterate through each columns
        Iterator<Cell> cellIterator = row.cellIterator();
        int j = 0;
        while (cellIterator.hasNext()) {
            Cell cell = cellIterator.next();
            Cell cellDestination = rowDestination.createCell(j);
            j++;
            cellDestination.setCellComment(cell.getCellComment());
            //                cellDestination.setCellStyle(cell.getCellStyle());
            cellDestination.setCellType(cell.getCellType());
            switch (cell.getCellType()) {
            case Cell.CELL_TYPE_BOOLEAN:
                cellDestination.setCellValue(cell.getBooleanCellValue());
                break;
            case Cell.CELL_TYPE_NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    cellDestination.setCellValue(cell.getDateCellValue());
                } else {
                    cellDestination.setCellValue(cell.getNumericCellValue());
                }
                break;
            case Cell.CELL_TYPE_STRING:
                cellDestination.setCellValue(cell.getRichStringCellValue());
                break;
            case Cell.CELL_TYPE_BLANK:
                cellDestination.setCellValue(cell.getStringCellValue());
                break;
            case Cell.CELL_TYPE_ERROR:
                cellDestination.setCellValue(cell.getErrorCellValue());
                break;
            case Cell.CELL_TYPE_FORMULA:
                cellDestination.setCellFormula(cell.getCellFormula());
                break;
            }

        }

    }

}

From source file:org.wandora.application.tools.extractors.excel.AbstractExcelExtractor.java

License:Open Source License

protected String getCellValueAsString(Cell cell, int type) {
    if (cell != null) {
        switch (type) {
        case Cell.CELL_TYPE_ERROR: {
            return "ERROR" + cell.getErrorCellValue();
        }// w  w  w  .  j  a v a 2 s.  c om
        case Cell.CELL_TYPE_BOOLEAN: {
            return "" + cell.getBooleanCellValue();
        }
        case Cell.CELL_TYPE_NUMERIC: {
            if (DateUtil.isCellDateFormatted(cell)) {
                return dateFormat.format(cell.getDateCellValue());
            } else {
                double value = cell.getNumericCellValue();
                String formatString = cell.getCellStyle().getDataFormatString();
                int formatIndex = cell.getCellStyle().getDataFormat();
                return formatter.formatRawCellContents(value, formatIndex, formatString);
            }
        }
        case Cell.CELL_TYPE_STRING: {
            return cell.getRichStringCellValue().getString();
        }
        }
    }
    return null;
}

From source file:org.wso2.carbon.dataservices.core.description.query.ExcelQuery.java

License:Open Source License

private String[] extractRowData(Row row) {
    if (row == null || row.getLastCellNum() == -1) {
        return null;
    }//from  w  w  w  . ja  v a  2s . c o  m
    String[] data = new String[row.getLastCellNum()];
    Cell cell;
    for (int i = 0; i < data.length; i++) {
        cell = row.getCell(i);
        if (cell == null) {
            data[i] = "";
            continue;
        }
        switch (cell.getCellType()) {
        case HSSFCell.CELL_TYPE_STRING:
            data[i] = cell.getRichStringCellValue().getString();
            break;
        case HSSFCell.CELL_TYPE_BLANK:
            data[i] = "";
            break;
        case HSSFCell.CELL_TYPE_BOOLEAN:
            data[i] = String.valueOf(cell.getBooleanCellValue());
            break;
        case HSSFCell.CELL_TYPE_FORMULA:
            data[i] = "{formula}";
            break;
        case HSSFCell.CELL_TYPE_NUMERIC:
            data[i] = processNumericValue(cell.getNumericCellValue());
            break;
        }
    }
    return data;
}

From source file:Principal.Main.java

private static void showExelData(List sheetData) {
    ////from  w w w . ja  v a  2 s. c om
    // Iterates the data and print it out to the console.
    //
    for (int i = 1; i < sheetData.size(); i++) {
        List list = (List) sheetData.get(i);
        for (int j = 0; j < list.size(); j++) {
            Cell cell = (Cell) list.get(j);
            if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                System.out.println(cell.getNumericCellValue());
            } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
                System.out.println(cell.getRichStringCellValue().getString().replaceAll(", ", ""));
            } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
                System.out.println(cell.getBooleanCellValue());
            }
            if (j < list.size() - 1) {
                System.out.print(", ");
            }
        }
        System.out.println("");
    }
}

From source file:ro.dabuno.office.integration.Xlsx2Word.java

public static void main(String[] args) throws Exception {
    log.info("starting app");
    //        Workbook wb = new XSSFWorkbook(new FileInputStream(args[0]));
    Workbook wb = new XSSFWorkbook(new FileInputStream("office-files/Input.xlsx"));

    DataFormatter formatter = new DataFormatter();

    for (int i = 0; i < wb.getNumberOfSheets(); i++) {
        Sheet sheet = wb.getSheetAt(i);//from  w w  w .  j  a  v a 2s .c  o m
        System.out.println(wb.getSheetName(i));
        int j = 4;
        for (Row row : sheet) {
            System.out.println("rownum: " + row.getRowNum());
            for (Cell cell : row) {
                CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex());
                System.out.print(cellRef.formatAsString());
                System.out.print(" - ");
                // get the text that appears in the cell by getting the cell value and applying any data formats (Date, 0.00, 1.23e9, $1.23, etc)
                String text = formatter.formatCellValue(cell);
                System.out.println(text);

                System.out.println("------------");
                // Alternatively, get the value and format it yourself
                switch (cell.getCellTypeEnum()) {
                case STRING:
                    System.out.println(cell.getRichStringCellValue().getString());
                    break;
                case NUMERIC:
                    if (DateUtil.isCellDateFormatted(cell)) {
                        System.out.println(cell.getDateCellValue());
                    } else {
                        System.out.print(cellRef.formatAsString());
                        System.out.print(" - ");
                        System.out.println((long) cell.getNumericCellValue());
                    }
                    break;
                case BOOLEAN:
                    System.out.println(cell.getBooleanCellValue());
                    break;
                case FORMULA:
                    System.out.println(cell.getCellFormula());
                    break;
                case BLANK:
                    System.out.println();
                    break;
                default:
                    System.out.println();
                }

            }
            j--;
            if (j == 0) {
                break;
            }
        }
    }

    XWPFDocument doc = new XWPFDocument();

    XWPFParagraph p0 = doc.createParagraph();
    XWPFRun r0 = p0.createRun();
    r0.setBold(false);
    r0.setText("Domnule");
    XWPFRun r00 = p0.createRun();
    r00.setBold(true);
    r00.setText(" Ionescu Ion");

    FileOutputStream out = new FileOutputStream("out/xlsx2word.docx");
    doc.write(out);
    out.close();
}

From source file:ru.icc.cells.ssdc.DataLoader.java

License:Apache License

private String extractCellValue(Cell excelCell) {
    String value = null;/*from  w ww. j a v  a  2  s  .  com*/

    switch (excelCell.getCellType()) {
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(excelCell))
            value = "DATE"; // TODO - ? 
        else
            value = Double.toString(excelCell.getNumericCellValue());
        break;

    case Cell.CELL_TYPE_STRING:
        value = excelCell.getRichStringCellValue().getString();
        break;

    case Cell.CELL_TYPE_BOOLEAN:
        value = Boolean.toString(excelCell.getBooleanCellValue());
        break;

    case Cell.CELL_TYPE_FORMULA:
        //value = excelCell.getCellFormula();
        value = extractCellFormulaValue(excelCell);
        break;

    case Cell.CELL_TYPE_BLANK:
        break;

    case Cell.CELL_TYPE_ERROR:
        break;
    }
    return value;
}

From source file:ru.icc.cells.ssdc.DataLoader.java

License:Apache License

private boolean hasSuperscriptText(Cell excelCell) {
    if (excelCell.getCellType() != Cell.CELL_TYPE_STRING)
        return false;

    RichTextString richTextString = excelCell.getRichStringCellValue();
    if (null == richTextString)
        return false;

    int index = 0;
    int length = 0;
    XSSFFont font = null;/*from  w  w  w  . ja  va 2 s . c  o m*/

    XSSFRichTextString rts = (XSSFRichTextString) richTextString;
    XSSFWorkbook wb = (XSSFWorkbook) workbook;

    XSSFCellStyle style = ((XSSFCell) excelCell).getCellStyle();
    font = style.getFont();

    String richText = rts.getString();
    if (rts.numFormattingRuns() > 1) {
        for (int i = 0; i < rts.numFormattingRuns(); i++) {
            index = rts.getIndexOfFormattingRun(i);
            length = rts.getLengthOfFormattingRun(i);

            try {
                font = rts.getFontOfFormattingRun(i);
            } catch (NullPointerException e) {
                font = wb.getFontAt(XSSFFont.DEFAULT_CHARSET);
                font.setTypeOffset(XSSFFont.SS_NONE);
            }

            String s = richText.substring(index, index + length);

            if (font.getTypeOffset() == XSSFFont.SS_SUPER)
                return true;
        }
    } else {
        if (font.getTypeOffset() == XSSFFont.SS_SUPER)
            return true;
    }
    return false;
}

From source file:ru.icc.cells.ssdc.DataLoader.java

License:Apache License

private String getNotSuperscriptText(Cell excelCell) {
    if (excelCell.getCellType() != Cell.CELL_TYPE_STRING)
        return null;
    RichTextString richTextString = excelCell.getRichStringCellValue();
    if (null == richTextString)
        return null;

    int index;//from  w w w. j ava2 s  .  c  om
    int length;
    String text;

    XSSFRichTextString rts = (XSSFRichTextString) richTextString;
    XSSFWorkbook wb = (XSSFWorkbook) workbook;

    XSSFCellStyle style = ((XSSFCell) excelCell).getCellStyle();
    XSSFFont font = style.getFont();

    String richText = rts.getString();
    StringBuilder notSuperscriptRuns = new StringBuilder();
    if (rts.numFormattingRuns() > 1) {
        boolean wasNotSuperscriptRun = false;
        for (int i = 0; i < rts.numFormattingRuns(); i++) {
            index = rts.getIndexOfFormattingRun(i);
            length = rts.getLengthOfFormattingRun(i);

            try {
                font = rts.getFontOfFormattingRun(i);
            } catch (NullPointerException e) {
                font = wb.getFontAt(XSSFFont.DEFAULT_CHARSET);
                font.setTypeOffset(XSSFFont.SS_NONE);
            }

            String s = richText.substring(index, index + length);

            if (font.getTypeOffset() == XSSFFont.SS_SUPER) {
                if (wasNotSuperscriptRun)
                    notSuperscriptRuns.append(" ");
                wasNotSuperscriptRun = false;
            } else {
                notSuperscriptRuns.append(s);
                wasNotSuperscriptRun = true;
            }
        }
        text = notSuperscriptRuns.toString();
    } else {
        if (font.getTypeOffset() == XSSFFont.SS_SUPER)
            text = null;
        else
            text = richText;
    }
    return text;
}