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:misuExcel.excelWrite.java

License:Open Source License

private void outType02() {
    if (wbSheet != null && names != null && list != null) {
        Log.info("list size:" + list.size());
        Workbook splitWb = null;//from  w w  w .  ja  v  a 2s .co m
        if (indexType == 1)
            splitWb = new XSSFWorkbook();
        else if (indexType == 2)
            splitWb = new HSSFWorkbook();
        for (int i = 0; i < list.size(); i++) {
            ArrayList<Integer> integers = list.get(i);
            Sheet sheet = splitWb.createSheet(names.get(i));
            for (int j = 0; j < integers.size() + splitJpanel.ignore_Row; j++) {
                Row row = null;
                Row copy = null;
                if (j >= splitJpanel.ignore_Row) {
                    row = sheet.createRow(j);
                    copy = wbSheet.getRow(integers.get(j - splitJpanel.ignore_Row));
                } else {
                    row = sheet.createRow(j);
                    copy = wbSheet.getRow(j);
                }
                for (int k = 0; k < copy.getLastCellNum(); k++) {
                    Cell cell = row.createCell(k);
                    Cell copyCell = copy.getCell(k);
                    if (copyCell != null) {
                        switch (copyCell.getCellType()) {
                        case Cell.CELL_TYPE_STRING:
                            cell.setCellValue(copyCell.getRichStringCellValue().getString().trim());
                            break;
                        case Cell.CELL_TYPE_NUMERIC:
                            if (DateUtil.isCellDateFormatted(copyCell)) {
                                cell.setCellValue(copyCell.getDateCellValue());
                            } else {
                                cell.setCellValue(copyCell.getNumericCellValue());
                            }
                            break;
                        case Cell.CELL_TYPE_BOOLEAN:
                            cell.setCellValue(copyCell.getBooleanCellValue());
                            break;
                        case Cell.CELL_TYPE_FORMULA:
                            cell.setCellValue(copyCell.getCellFormula());
                            break;
                        default:
                            cell.setCellValue(copyCell.getStringCellValue().trim());
                        }
                    }
                }
            }
        } //end for
        createWB(splitWb, fileReal + "(cut)");
        JOptionPane.showMessageDialog(null, fileReal + "(cut)." + _index + "?");
    }
}

From source file:misuExcel.excelWrite.java

License:Open Source License

private void outType03() {
    if (wbSheet != null && addWb != null && names != null && list != null) {
        Sheet sheet = addWb.getSheetAt(sheetNum_target);
        for (int i = 0; i < list.size(); i++) {
            ArrayList<Integer> integers = list.get(i);
            Row copy = wbSheet.getRow(i + addJpanel.ignore_Rowtar);
            for (int j = 0; j < integers.size(); j++) {
                Row row = sheet.getRow(integers.get(j));
                int numRow = row.getLastCellNum();
                for (int k = addJpanel.ignore_Celltar; k < copy.getLastCellNum(); k++) {
                    Cell cell = null;//w  ww.ja v  a  2 s  .  c  om
                    Cell copyCell = null;
                    if (k != cellNum_target) {
                        copyCell = copy.getCell(k);
                        if (addJpanel.ignore_Celltar > cellNum_target) {
                            cell = row.createCell(k + numRow - addJpanel.ignore_Celltar);
                        } else {
                            cell = row.createCell(k < cellNum_target ? (k + numRow - addJpanel.ignore_Celltar)
                                    : (k - 1 + numRow - addJpanel.ignore_Celltar));
                        }
                    }
                    if (copyCell != null) {
                        switch (copyCell.getCellType()) {
                        case Cell.CELL_TYPE_STRING:
                            cell.setCellValue(copyCell.getRichStringCellValue().getString().trim());
                            break;
                        case Cell.CELL_TYPE_NUMERIC:
                            if (DateUtil.isCellDateFormatted(copyCell)) {
                                cell.setCellValue(copyCell.getDateCellValue());
                            } else {
                                cell.setCellValue(copyCell.getNumericCellValue());
                            }
                            break;
                        case Cell.CELL_TYPE_BOOLEAN:
                            cell.setCellValue(copyCell.getBooleanCellValue());
                            break;
                        case Cell.CELL_TYPE_FORMULA:
                            cell.setCellValue(copyCell.getCellFormula());
                            break;
                        default:
                            cell.setCellValue(copyCell.getStringCellValue().trim());
                        }
                    }
                }
            }
        } //end for
        createWB(addWb, fileReal + "(add)");
        JOptionPane.showMessageDialog(null, fileReal + "(add)." + _index + "?");
    }
}

From source file:misuExcel.excelWrite.java

License:Open Source License

private void outType04() {
    if (wbSheet != null && addWb != null && names != null && list != null) {
        Sheet sheet = addWb.getSheetAt(sheetNum_target);
        int numRow = sheet.getLastRowNum() + 1;
        ArrayList<Integer> integers = list.get(0);
        for (int j = addJpanel.ignore_Rowtar; j <= wbSheet.getLastRowNum(); j++) {
            Row row = null;/* w w  w . jav a2 s  .c  om*/
            Row copy = null;
            if (j != cellNum_target) {
                if ((cellNum_target + 1) > addJpanel.ignore_Rowtar)
                    row = sheet.createRow(j < cellNum_target ? (j + numRow - addJpanel.ignore_Rowtar)
                            : (j + numRow - 1 - addJpanel.ignore_Rowtar));
                else
                    row = sheet.createRow(j + numRow - addJpanel.ignore_Rowtar);
                copy = wbSheet.getRow(j);
            }
            if (copy != null) {
                for (int k = 0; k < copy.getLastCellNum(); k++) {
                    Cell cell = null;
                    if (k >= addJpanel.ignore_Celltar)
                        cell = row.createCell(integers.get((k - addJpanel.ignore_Celltar)));
                    else
                        cell = row.createCell(k);
                    Cell copyCell = copy.getCell(k);
                    if (copyCell != null) {
                        switch (copyCell.getCellType()) {
                        case Cell.CELL_TYPE_STRING:
                            cell.setCellValue(copyCell.getRichStringCellValue().getString().trim());
                            break;
                        case Cell.CELL_TYPE_NUMERIC:
                            if (DateUtil.isCellDateFormatted(copyCell)) {
                                cell.setCellValue(copyCell.getDateCellValue());
                            } else {
                                cell.setCellValue(copyCell.getNumericCellValue());
                            }
                            break;
                        case Cell.CELL_TYPE_BOOLEAN:
                            cell.setCellValue(copyCell.getBooleanCellValue());
                            break;
                        case Cell.CELL_TYPE_FORMULA:
                            cell.setCellValue(copyCell.getCellFormula());
                            break;
                        default:
                            cell.setCellValue(copyCell.getStringCellValue().trim());
                        }
                    }
                }
            }
        } //end for
        createWB(addWb, fileReal + "(add)");
        JOptionPane.showMessageDialog(null, fileReal + "(add)." + _index + "?");
    }
}

From source file:mongodbutils.Filehandler.java

private Object getCellValue(Cell cell) {

    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        return cell.getRichStringCellValue().getString();
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            Date dt = cell.getDateCellValue();

            JSONObject obj = new JSONObject();
            obj.put("$date", dt.getTime());
            return obj.toString();

            //return "" + cell.getDateCellValue();
        } else {//  www  . j a  v  a 2s  . c  om
            return cell.getNumericCellValue();
        }
    case Cell.CELL_TYPE_BOOLEAN:
        return "" + cell.getBooleanCellValue();
    case Cell.CELL_TYPE_FORMULA:
        return cell.getCellFormula();
    }
    return "";
}

From source file:monthly.organizer.MonthlyOrganizer.java

@Override
public void start(Stage primaryStage) {
    wbo = new XSSFWorkbook();
    try {//from   w ww  .j  a v  a 2 s  .  c  om
        Button merge = new Button("merge");
        Button folder = new Button("Choose Folder");
        Button file = new Button("Choose Save File");
        Label fileLoc = new Label("Not Selected");
        Label folderLoc = new Label("Not Selected");
        folder.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent arg0) {
                DirectoryChooser folderChooser = new DirectoryChooser();
                folderChooser.setTitle("Choose Folder Containing Files");
                selectedFolder = folderChooser.showDialog(primaryStage);
                //folderLoc.setText(selectedFolder.getName());
                if (null == selectedFolder)
                    folderLoc.setText("No Folder Selected");
                else
                    folderLoc.setText(selectedFolder.getName());
            }

        });

        file.setOnAction(new EventHandler<ActionEvent>() {

            @SuppressWarnings("deprecation")
            @Override
            public void handle(ActionEvent arg0) {

                FileChooser fileSaver = new FileChooser();
                fileSaver.setTitle("Create Save File");
                fileSaver.getExtensionFilters().addAll(new ExtensionFilter(
                        "ExceBeaconController bc = new BeaconController();ll Files", "*.xlsx"));

                outputFile = fileSaver.showSaveDialog(primaryStage);
                //outputFile = new File("/home/camen/Desktop" + "/Final_Books" + LocalDate.now() + ".xlsx");
                if (null == outputFile)
                    fileLoc.setText("No File Selected");
                else
                    fileLoc.setText(outputFile.getName());
            }
        });

        merge.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent arg0) {
                try {
                    outputStream = new FileOutputStream(outputFile);
                } catch (FileNotFoundException e2) {
                    // TODO Auto-generated catch block
                    e2.printStackTrace();
                }
                try (

                        Stream<Path> paths = Files.walk(Paths.get(selectedFolder.getPath()))) {
                    paths.forEach(filePath -> {
                        if (Files.isRegularFile(filePath)
                                && !filePath.toString().equals(selectedFolder.toPath().toString())) {
                            //System.out.println(filePath);
                            fileNum++;
                            FileInputStream inputStream;

                            try {
                                inputStream = new FileInputStream(filePath.toString());

                                wb = new XSSFWorkbook(inputStream);

                                CreationHelper createHelper = wbo.getCreationHelper();
                                Sheet sheetOut = wbo.createSheet(filePath.getFileName().toString());
                                //add client name to beacon controller
                                clients.add(new Client(filePath.getFileName().toString()));
                                clientNum++;
                                //System.out.println(clientNum + "");
                                Row rowOut = sheetOut.createRow(0);
                                Cell cellOut;
                                cellOut = rowOut.createCell(0);
                                cellOut.setCellValue(createHelper.createRichTextString("Week"));
                                cellOut = rowOut.createCell(1);
                                cellOut.setCellValue(createHelper.createRichTextString("Behavior/Decel"));
                                cellOut = rowOut.createCell(2);
                                cellOut.setCellValue(createHelper.createRichTextString("Data Input Total"));
                                cellOut = rowOut.createCell(3);
                                cellOut.setCellValue(createHelper.createRichTextString("Measurment Type"));
                                cellOut = rowOut.createCell(4);
                                cellOut.setCellValue(createHelper.createRichTextString("Measurment Unit"));
                                cellOut = rowOut.createCell(5);
                                cellOut.setCellValue(
                                        createHelper.createRichTextString("Total Time for Week (Mins)"));
                                int rowCount = 1;
                                for (int k = 0; k < wb.getNumberOfSheets(); k++) {
                                    Sheet sheet = wb.getSheetAt(k);
                                    double weekTotal = 0;
                                    double timeTotal = 0;
                                    boolean replace = false;
                                    String decelName = null;
                                    String replaceName = null;
                                    Random rand = null;
                                    int n = 0;
                                    String measType = null;
                                    String measUnit = null;
                                    String replacedName = null;
                                    String replacedNameOld = null;
                                    LocalDateTime date = null;
                                    int weekNum = -1;
                                    int lastWeek = -1;
                                    int year = 0;
                                    int rows = sheet.getPhysicalNumberOfRows();
                                    //System.out.println("Sheet " + k + " \"" + wb.getSheetName(k) + "\" has " + rows
                                    //+ " row(s).");
                                    for (int r = 0; r < rows; r++) {

                                        Row row = sheet.getRow(r);
                                        if (row == null) {
                                            continue;
                                        }

                                        int cells = row.getPhysicalNumberOfCells();
                                        //System.out.println("\nROW " + row.getRowNum() + " has " + cells
                                        //+ " cell(s).");
                                        for (int c = 0; c < cells; c++) {
                                            Cell cell = row.getCell(c);
                                            if (cell == null) {
                                                //c++;
                                                cells++;
                                            } else {
                                                switch (cell.getCellTypeEnum()) {
                                                case STRING:
                                                    if (r == 0 && cell.getRichStringCellValue().getString()
                                                            .contains("Replacement"))
                                                        replace = true;
                                                    if (r == 0 && replace == true && c == 1) {
                                                        replaceName = cell.getRichStringCellValue().getString();
                                                        rand = new Random();
                                                        n = rand.nextInt(20) + 1;
                                                    } else if (r == 0 && replace == false && c == 1)
                                                        decelName = cell.getRichStringCellValue().getString();
                                                    else if (r == 0 && replace == true && c == 2)
                                                        replacedName = cell.getRichStringCellValue()
                                                                .getString();
                                                    if (r == 1 && c == 0)
                                                        measType = cell.getRichStringCellValue().getString();
                                                    if (r == 1 && c == 1)
                                                        measUnit = cell.getRichStringCellValue().getString();
                                                    //System.out.println(cell.getRichStringCellValue().getString());
                                                    break;
                                                case NUMERIC:
                                                    if (DateUtil.isCellDateFormatted(cell)) {
                                                        date = cell.getDateCellValue().toInstant()
                                                                .atZone(ZoneId.systemDefault()).toLocalDate()
                                                                .atTime(LocalTime.NOON);
                                                        year = date.getYear();
                                                        weekNum = date.get(IsoFields.WEEK_OF_WEEK_BASED_YEAR);
                                                        if (lastWeek == -1)
                                                            lastWeek = weekNum;
                                                        c++;
                                                        cell = row.getCell(c);
                                                        weekTotal += cell.getNumericCellValue();
                                                        c++;
                                                        //cell = row.getCell(c);
                                                        //timeTotal += cell.getNumericCellValue();
                                                    } else {
                                                        System.out.println(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();
                                                }

                                                //System.out.println("CELL col=" + cell.getColumnIndex() + " VALUE="
                                                //+ value);
                                            }
                                        } //System.out.println(lastWeek + " " + weekNum + " " + weekTotal);
                                        if (lastWeek != weekNum && lastWeek != -1) {

                                            //weekCount ++;
                                            rowOut = sheetOut.createRow(rowCount);
                                            Cell cell = rowOut.createCell(0);
                                            cell.setCellValue(createHelper.createRichTextString(date.getMonth()
                                                    + ", Week " + weekNum + " of Year: " + date.getYear()));

                                            //create random number to choose behavior

                                            cell = rowOut.createCell(1);
                                            if (replace == true) {
                                                cell.setCellValue(
                                                        createHelper.createRichTextString(replaceName));
                                                //client.addValue((((date.getYear() - 2014)*53) + weekNum), weekTotal, Arrays.binarySearch(Client.behaviors, replaceName));

                                                //addvalue to random behavior chosen for the week
                                                clients.get(clientNum).addValue(
                                                        (((date.getYear() - 2014) * 53) + weekNum), weekTotal,
                                                        n);
                                                //System.out.println((((date.getYear() - 2014)*53) + weekNum) + " " + weekTotal + " " + Client.behaviors[n] + " " + clients.get(clientNum).getName());
                                            }

                                            else
                                                cell.setCellValue(createHelper.createRichTextString(decelName));
                                            cell = rowOut.createCell(2);
                                            cell.setCellValue(weekTotal);
                                            cell = rowOut.createCell(3);
                                            cell.setCellValue(createHelper.createRichTextString(measType));
                                            cell = rowOut.createCell(4);
                                            cell.setCellValue(createHelper.createRichTextString(measUnit));
                                            //System.out.println(months[wR[weekNum].month-1] + " week " + (wR[weekNum].monthWeek) + ", " + (year+1900) + ": " + weekTotal + " Measurment Type: " + measType + " Measument Unit: " + measUnit);
                                            weekTotal = 0;
                                            lastWeek = weekNum;
                                            //newWeek = false;
                                            rowCount++;
                                        }

                                    }
                                    //if(replace == true)
                                    //System.out.println(replaceName + " for " + replacedName);
                                    //else
                                    //System.out.println(decelName);

                                }
                                //controller.addClient(clients.get(clientNum));
                                //controller.getClients().forEach((names) -> {
                                //System.out.println(names.getName());
                                //});

                                wb.close();

                                inputStream.close();
                            } catch (FileNotFoundException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }

                    });
                    wbo.write(outputStream);
                    wbo.close();
                    outputStream.close();
                    final FXMLLoader loader = new FXMLLoader(getClass().getResource("beacon.fxml"));
                    final Stage stage = new Stage(StageStyle.DECORATED);
                    stage.setScene(new Scene((Pane) loader.load()));
                    final BeaconController controller = loader.<BeaconController>getController();
                    controller.addClients(clients);
                    controller.setFields(true);
                    stage.showAndWait();
                    //getHostServices().showDocument(outputFile.toURI().toURL().toExternalForm());
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }

        });

        VBox root = new VBox();
        root.setId("pane");
        folder.setId("button");
        file.setId("button");
        merge.setId("button");
        root.getStylesheets().addAll(this.getClass().getResource("buttonStyle.css").toExternalForm());
        root.setPadding(new Insets(180, 200, 30, 200));
        root.setAlignment(Pos.BASELINE_CENTER);
        root.getChildren().addAll(folder, folderLoc, file, fileLoc, merge);
        root.setSpacing(10);
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:net.ceos.project.poi.annotated.core.CellHandler.java

License:Apache License

/**
 * Manage the cached formula case./*from w ww  .j a v a  2s .co m*/
 * 
 * @param cellthe
 *            {@link Cell}
 * @return the content of the cell
 */
private static String switchCachedFormulaManager(final Cell cell) {
    switch (cell.getCachedFormulaResultType()) {
    case Cell.CELL_TYPE_NUMERIC:
        Double value = cell.getNumericCellValue();
        return value.toString();

    case Cell.CELL_TYPE_STRING:
        return cell.getRichStringCellValue().toString();

    default:
        return null;
    }
}

From source file:net.duckling.ddl.util.ExcelReader.java

License:Apache License

/**
 * //from   w w w  .java  2s .c o m
 * ?Cell?
 * 
 * @param cell
 * @return
 */
private Object getCellFormatValue(Cell cell) {
    Object cellvalue = "";
    if (cell != null) {
        // ?CellType
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_NUMERIC:// ?CellTypeNUMERIC
        case Cell.CELL_TYPE_FORMULA: {
            // ?cell?Date
            if (DateUtil.isCellDateFormatted(cell)) {
                Date date = cell.getDateCellValue();
                cellvalue = date;
            } else {// 
                // ??Cell
                cellvalue = String.valueOf(cell.getNumericCellValue());
            }
            break;
        }
        case Cell.CELL_TYPE_STRING:// ?CellTypeSTRING
            // ??Cell
            cellvalue = cell.getRichStringCellValue().getString();
            break;
        default:// Cell
            cellvalue = "";
        }
    } else {
        cellvalue = "";
    }
    return cellvalue;
}

From source file:net.illustrato.ctrl.CtrlCore.java

private Row copyRow(Workbook workbook, Sheet worksheet, int sourceRowNum, int destinationRowNum) {
    // Get the source / new row
    Row newRow = worksheet.getRow(destinationRowNum);
    Row sourceRow = worksheet.getRow(sourceRowNum);

    // If the row exist in destination, push down all rows by 1 else create a new row
    if (newRow != null) {
        worksheet.shiftRows(destinationRowNum, worksheet.getLastRowNum(), 1);
    } else {//from   w  w  w . j  ava 2 s.  c  om
        newRow = worksheet.createRow(destinationRowNum);
    }

    // Loop through source columns to add to new row
    for (int i = 0; i < sourceRow.getLastCellNum(); i++) {
        // Grab a copy of the old/new cell
        Cell oldCell = sourceRow.getCell(i);
        Cell newCell = newRow.createCell(i);

        // If the old cell is null jump to next cell
        if (oldCell == null) {
            newCell = null;
            continue;
        }

        // Copy style from old cell and apply to new cell
        CellStyle newCellStyle = workbook.createCellStyle();
        newCellStyle.cloneStyleFrom(oldCell.getCellStyle());
        newCell.setCellStyle(newCellStyle);

        // Set the cell data type
        newCell.setCellType(oldCell.getCellType());

        // Set the cell data value
        switch (oldCell.getCellType()) {
        case HSSFCell.CELL_TYPE_BLANK:
            newCell.setCellValue(oldCell.getStringCellValue());
            break;
        case HSSFCell.CELL_TYPE_FORMULA:
            newCell.setCellFormula(oldCell.getCellFormula());
            //Si tenemos que modificar la formulario lo podemos hacer como string
            //oldCell.getCellFormula().replace("A"+sourceRowNum, "A"+destinationRowNum)
            break;
        case HSSFCell.CELL_TYPE_NUMERIC:
            newCell.setCellValue(oldCell.getNumericCellValue());
            break;
        case HSSFCell.CELL_TYPE_STRING:
            newCell.setCellValue(oldCell.getRichStringCellValue());
            break;
        }
    }
    return newRow;
}

From source file:net.mcnewfamily.rmcnew.model.Util.java

License:Open Source License

public static String getCellValueAsStringOrEmptyString(Cell cell) {
    if (cell == null) {
        return "";
    }//w  ww.j av a  2s.  com
    String value;
    switch (cell.getCellType()) {
    case Cell.CELL_TYPE_STRING:
        value = cell.getRichStringCellValue().getString();
        break;
    case Cell.CELL_TYPE_NUMERIC:
        if (DateUtil.isCellDateFormatted(cell)) {
            value = cell.getDateCellValue().toString();
        } else {
            value = Integer.toString((int) cell.getNumericCellValue());
        }
        break;
    case Cell.CELL_TYPE_BOOLEAN:
        value = Boolean.toString(cell.getBooleanCellValue());
        break;
    case Cell.CELL_TYPE_FORMULA:
        value = cell.getCellFormula();
        break;
    default:
        value = "";
    }
    return value;
}

From source file:nl.detoren.ijsco.io.ExcelImport.java

License:Open Source License

private String cellStringValue(Cell cell) {
    String value = null;/*  w  w  w .  j  a  v a2 s . co m*/
    if (cell != null) {
        switch (cell.getCellType()) {
        case Cell.CELL_TYPE_STRING:
            value = cell.getStringCellValue();
            // logger.log(Level.INFO, "Waarde is " + value + " !");
            break;
        case Cell.CELL_TYPE_FORMULA:
            switch (cell.getCachedFormulaResultType()) {
            case Cell.CELL_TYPE_STRING:
                //System.out.println("Last evaluated as \"" + cell.getRichStringCellValue() + "\"");
                value = cell.getRichStringCellValue().getString();
                break;
            }
        default:
            //logger.log(Level.WARNING, "Waarde is geen String!");
        }
    }
    return value;
}