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

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

Introduction

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

Prototype

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.ostrichemulators.semtool.poi.main.POIReader.java

License:Open Source License

/**
 * Always return a non-null string (will be "" for null cells).
 *
 * @param cell/*from   w  ww . ja  va2 s.  c  o  m*/
 * @return
 */
private static String getString(Cell cell) {
    if (null == cell) {
        return "";
    }

    switch (cell.getCellType()) {
    case NUMERIC:
        return Double.toString(cell.getNumericCellValue());
    case BOOLEAN:
        return Boolean.toString(cell.getBooleanCellValue());
    case FORMULA:
        return cell.getCellFormula();
    default:
        return cell.getStringCellValue();
    }
}

From source file:com.pdf.GetPdf.java

public static void addXls(Document document, String url, String type) throws IOException, DocumentException {
    Iterator<Row> rowIterator;
    int colNo;//from  ww w.  ja v a 2s. co m
    if (type.equals("xls")) {
        HSSFWorkbook excelWorkbook = new HSSFWorkbook(new URL(url).openStream());
        HSSFSheet my_worksheet = excelWorkbook.getSheetAt(0);
        rowIterator = my_worksheet.iterator();
        colNo = my_worksheet.getRow(0).getLastCellNum();
    } else {
        XSSFWorkbook excelWorkbook1 = new XSSFWorkbook(new URL(url).openStream());
        XSSFSheet my_worksheet = excelWorkbook1.getSheetAt(0);
        rowIterator = my_worksheet.iterator();
        colNo = my_worksheet.getRow(0).getLastCellNum();
    }
    PdfPTable my_table = new PdfPTable(colNo);
    PdfPCell table_cell = null;
    while (rowIterator.hasNext()) {
        Row row = rowIterator.next(); //Read Rows from Excel document       
        Iterator<Cell> cellIterator = row.cellIterator();//Read every column for every row that is READ
        while (cellIterator.hasNext()) {
            Cell cell = cellIterator.next(); //Fetch CELL
            if (cell.getCellType() == (Cell.CELL_TYPE_NUMERIC)) {
                table_cell = new PdfPCell(new Phrase(new Double(cell.getNumericCellValue()).toString()));
                System.out.println(cell.getNumericCellValue());
                my_table.addCell(table_cell);
            } else if (cell.getCellType() == (Cell.CELL_TYPE_STRING)) {
                table_cell = new PdfPCell(new Phrase(cell.getStringCellValue()));
                System.out.println(cell.getStringCellValue());
                my_table.addCell(table_cell);
            } else if (cell.getCellType() == (Cell.CELL_TYPE_FORMULA)) {
                table_cell = new PdfPCell(new Phrase(cell.getCellFormula()));
                my_table.addCell(table_cell);
            } else if (cell.getCellType() == (Cell.CELL_TYPE_BLANK)) {
                table_cell = new PdfPCell(new Phrase(""));
                my_table.addCell(table_cell);
            } else {
                table_cell = new PdfPCell(new Phrase(""));
                my_table.addCell(table_cell);
            }
        }
    }
    document.add(my_table);
}

From source file:com.photon.phresco.eshop.utils.ServiceUtil.java

License:Apache License

public static String getValue(Cell cell) {
    if (cell == null) {
        return null;
    }/*from w  w w  .  j a  va  2  s  .  c  o  m*/

    if (Cell.CELL_TYPE_STRING == cell.getCellType()) {
        return cell.getStringCellValue();
    }

    if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) {
        return String.valueOf(cell.getNumericCellValue());
    }

    if (Cell.CELL_TYPE_BOOLEAN == cell.getCellType()) {
        return String.valueOf(cell.getBooleanCellValue());
    }

    if (Cell.CELL_TYPE_BLANK == cell.getCellType()) {
        return null;
    }

    return null;
}

From source file:com.photon.phresco.framework.commons.FrameworkUtil.java

License:Apache License

private static String getValue(Cell cell) {
    if (cell != null) {
        if (Cell.CELL_TYPE_STRING == cell.getCellType()) {
            return cell.getStringCellValue();
        }//from w w w.  ja v  a  2  s . c o  m

        if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) {
            return String.valueOf(cell.getNumericCellValue());
        }
    }

    return null;
}

From source file:com.photon.phresco.framework.commons.FrameworkUtil.java

License:Apache License

public void addNew(String filePath, String testName, String cellValue[]) throws PhrescoException {
    try {/*from   w  w w  .  j  ava 2  s.  c o  m*/
        //FileInputStream myInput = new FileInputStream(filePath);

        int numCol;
        int cellno = 0;
        CellStyle tryStyle[] = new CellStyle[20];
        String sheetName = testName;
        //String cellValue[] = {"","",testName,success, fail,"","","",total,testCoverage,"","",""};
        Iterator<Row> rowIterator;
        File testDir = new File(filePath);
        StringBuilder sb = new StringBuilder(filePath);
        if (testDir.isDirectory()) {
            FilenameFilter filter = new PhrescoFileFilter("", "xlsx");
            File[] listFiles = testDir.listFiles(filter);
            if (listFiles.length != 0) {
                for (File file1 : listFiles) {
                    if (file1.isFile()) {
                        sb.append(File.separator);
                        sb.append(file1.getName());
                        break;
                    }
                }
                FileInputStream myInput = new FileInputStream(sb.toString());
                OPCPackage opc = OPCPackage.open(myInput);

                XSSFWorkbook myWorkBook = new XSSFWorkbook(opc);
                XSSFSheet mySheet = myWorkBook.getSheetAt(0);
                rowIterator = mySheet.rowIterator();
                numCol = 13;
                Row next;
                for (Cell cell : mySheet.getRow((mySheet.getLastRowNum()) - 2)) {
                    tryStyle[cellno] = cell.getCellStyle();
                    cellno = cellno + 1;
                }
                do {

                    int flag = 0;
                    next = rowIterator.next();
                    if ((mySheet.getSheetName().trim()).equalsIgnoreCase("Index")
                            && ((mySheet.getLastRowNum() - next.getRowNum()) < 3)) {
                        for (Cell cell : next) {
                            cell.setCellType(1);
                            if (cell.getStringCellValue().equalsIgnoreCase("total")) {
                                mySheet.shiftRows((mySheet.getLastRowNum() - 1),
                                        (mySheet.getPhysicalNumberOfRows() - 1), 1);
                                flag = 1;
                            }
                            if (flag == 1)
                                break;
                        }
                        if (flag == 1)
                            break;
                    }
                } while (rowIterator.hasNext());

                Row r = null;
                if ((mySheet.getSheetName().trim()).equalsIgnoreCase("Index")) {
                    r = mySheet.createRow(next.getRowNum() - 1);

                } else {
                    r = mySheet.createRow(next.getRowNum() + 1);
                }
                for (int i = 0; i < numCol; i++) {
                    Cell cell = r.createCell(i);
                    cell.setCellValue(cellValue[i]);
                    // used only when sheet is 'index'
                    if (i == 2)
                        sheetName = cellValue[i];

                    cell.setCellStyle(tryStyle[i]);
                }
                if ((mySheet.getSheetName().trim()).equalsIgnoreCase("Index")) {
                    Sheet fromSheet = myWorkBook.getSheetAt((myWorkBook.getNumberOfSheets() - 1));
                    Sheet toSheet = myWorkBook.createSheet(sheetName);
                    int i = 0;
                    Iterator<Row> copyFrom = fromSheet.rowIterator();
                    Row fromRow, toRow;
                    CellStyle newSheetStyle[] = new CellStyle[20];
                    Integer newSheetType[] = new Integer[100];
                    String newSheetValue[] = new String[100];
                    do {
                        fromRow = copyFrom.next();
                        if (fromRow.getRowNum() == 24) {
                            break;
                        }
                        toRow = toSheet.createRow(i);
                        int numCell = 0;
                        for (Cell cell : fromRow) {
                            Cell newCell = toRow.createCell(numCell);

                            cell.setCellType(1);

                            newSheetStyle[numCell] = cell.getCellStyle();
                            newCell.setCellStyle(newSheetStyle[numCell]);

                            newSheetType[numCell] = cell.getCellType();
                            newCell.setCellType(newSheetType[numCell]);
                            if (fromRow.getCell(0).getStringCellValue().length() != 1
                                    && fromRow.getCell(0).getStringCellValue().length() != 2
                                    && fromRow.getCell(0).getStringCellValue().length() != 3) {
                                newSheetValue[numCell] = cell.getStringCellValue();
                                newCell.setCellValue(newSheetValue[numCell]);
                            }

                            numCell = numCell + 1;
                        }
                        i = i + 1;
                    } while (copyFrom.hasNext());
                }
                // write to file
                FileOutputStream fileOut = new FileOutputStream(sb.toString());
                myWorkBook.write(fileOut);
                myInput.close();
                fileOut.close();
            } else {
                FilenameFilter xlsFilter = new PhrescoFileFilter("", "xls");
                File[] xlsListFiles = testDir.listFiles(xlsFilter);
                if (xlsListFiles.length != 0) {
                    for (File file2 : xlsListFiles) {
                        if (file2.isFile()) {
                            sb.append(File.separator);
                            sb.append(file2.getName());
                            break;
                        }
                    }
                    FileInputStream myInput = new FileInputStream(sb.toString());
                    HSSFWorkbook myWorkBook = new HSSFWorkbook(myInput);

                    HSSFSheet mySheet = myWorkBook.getSheetAt(0);
                    rowIterator = mySheet.rowIterator();
                    numCol = 13;
                    Row next;
                    for (Cell cell : mySheet.getRow((mySheet.getLastRowNum()) - 2)) {
                        tryStyle[cellno] = cell.getCellStyle();
                        cellno = cellno + 1;
                    }
                    do {

                        int flag = 0;
                        next = rowIterator.next();
                        if ((mySheet.getSheetName().trim()).equalsIgnoreCase("Index")
                                && ((mySheet.getLastRowNum() - next.getRowNum()) < 3)) {
                            for (Cell cell : next) {
                                cell.setCellType(1);
                                if (cell.getStringCellValue().equalsIgnoreCase("total")) {
                                    mySheet.shiftRows((mySheet.getLastRowNum() - 1),
                                            (mySheet.getPhysicalNumberOfRows() - 1), 1);
                                    flag = 1;
                                }
                                if (flag == 1)
                                    break;
                            }
                            if (flag == 1)
                                break;
                        }
                    } while (rowIterator.hasNext());

                    Row r = null;
                    if ((mySheet.getSheetName().trim()).equalsIgnoreCase("Index")) {
                        r = mySheet.createRow(mySheet.getLastRowNum() - 2);
                    } else {
                        r = mySheet.createRow(next.getRowNum() + 1);
                    }
                    for (int i = 0; i < numCol; i++) {
                        Cell cell = r.createCell(i);
                        cell.setCellValue(cellValue[i]);
                        // used only when sheet is 'index'
                        if (i == 2)
                            sheetName = cellValue[i];

                        cell.setCellStyle(tryStyle[i]);
                    }
                    if ((mySheet.getSheetName().trim()).equalsIgnoreCase("Index")) {
                        Sheet fromSheet = myWorkBook.getSheetAt((myWorkBook.getNumberOfSheets() - 1));
                        Sheet toSheet = myWorkBook.createSheet(sheetName);
                        int i = 0;
                        Iterator<Row> copyFrom = fromSheet.rowIterator();
                        Row fromRow, toRow;
                        CellStyle newSheetStyle[] = new CellStyle[20];
                        Integer newSheetType[] = new Integer[100];
                        String newSheetValue[] = new String[100];
                        do {
                            fromRow = copyFrom.next();
                            if (fromRow.getRowNum() == 24) {
                                break;
                            }
                            toRow = toSheet.createRow(i);
                            int numCell = 0;
                            for (Cell cell : fromRow) {
                                Cell newCell = toRow.createCell(numCell);

                                cell.setCellType(1);

                                newSheetStyle[numCell] = cell.getCellStyle();
                                newCell.setCellStyle(newSheetStyle[numCell]);

                                newSheetType[numCell] = cell.getCellType();
                                newCell.setCellType(newSheetType[numCell]);
                                if (fromRow.getCell(0).getStringCellValue().length() != 1
                                        && fromRow.getCell(0).getStringCellValue().length() != 2
                                        && fromRow.getCell(0).getStringCellValue().length() != 3) {
                                    newSheetValue[numCell] = cell.getStringCellValue();
                                    newCell.setCellValue(newSheetValue[numCell]);
                                }

                                numCell = numCell + 1;
                                if (numCell == 15) {
                                    break;
                                }
                            }
                            i = i + 1;
                        } while (copyFrom.hasNext());
                    }
                    // write to file
                    FileOutputStream fileOut = new FileOutputStream(sb.toString());
                    myWorkBook.write(fileOut);
                    myInput.close();
                    fileOut.close();
                } else {
                    FilenameFilter odsFilter = new PhrescoFileFilter("", "ods");
                    File[] odsListFiles = testDir.listFiles(odsFilter);
                    for (File file1 : odsListFiles) {
                        if (file1.isFile()) {
                            sb.append(File.separator);
                            sb.append(file1.getName());
                            break;
                        }
                    }
                    File file = new File(sb.toString());
                    addTestSuiteToOds(file, cellValue);
                }
            }
        }
    } catch (Exception e) {
        //         throw new PhrescoException(e);
    }
}

From source file:com.photon.phresco.service.tools.AdminConfigGenerator.java

License:Apache License

private String getValue(Cell cell) {
    if (Cell.CELL_TYPE_STRING == cell.getCellType()) {
        System.out.println("hello-->" + cell.getStringCellValue());
        return cell.getStringCellValue();
    }/*from w ww . ja v a2 s. co  m*/

    if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) {
        System.out.println("hello2");
        return String.valueOf(cell.getNumericCellValue());
    }
    return null;
}

From source file:com.photon.phresco.service.tools.ConfigDataGenerator.java

License:Apache License

private static String getValue(Cell cell) {
    if (Cell.CELL_TYPE_STRING == cell.getCellType()) {
        return cell.getStringCellValue();
    }//from w  w w. j  av a2  s .  c  o m

    if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) {
        return String.valueOf(cell.getNumericCellValue());
    }

    return null;
}

From source file:com.photon.phresco.service.tools.JsLibraryGenerator.java

License:Apache License

private String getValue(Cell cell) {
    if (Cell.CELL_TYPE_STRING == cell.getCellType()) {
        return cell.getStringCellValue();
    }//from  w  w  w  . java2s.  co  m

    if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) {
        return String.valueOf(cell.getNumericCellValue());
    }

    return null;
}

From source file:com.photon.phresco.service.tools.JsLibraryGenerator.java

License:Apache License

private ModuleGroup createJSLibrary(Row row, String techId) throws PhrescoException {
    ModuleGroup moduleGroup = new ModuleGroup();
    Cell serialNo = row.getCell(0);//from   w  ww  .ja v a2s.  c  o  m
    if (serialNo == null || Cell.CELL_TYPE_BLANK == serialNo.getCellType()) {
        return null;
    }

    String name = row.getCell(1).getStringCellValue();
    String[] versions = new String[] { "1.0" };
    Cell versionCell = row.getCell(2);
    if (versionCell != null && Cell.CELL_TYPE_BLANK != versionCell.getCellType()) {
        String version = getValue(versionCell);
        versions = StringUtils.split(version, DELIMITER);
    }

    Boolean req = false;
    Cell required = row.getCell(9);
    if (required != null && Cell.CELL_TYPE_BLANK != required.getCellType()) {
        req = convertBoolean(getValue(required));
    }

    String identifier = id1 + row.getCell(1).getStringCellValue().toLowerCase();
    String no = String.valueOf(identifier);

    List<Documentation> documentations = new ArrayList<Documentation>();
    Documentation documents;
    Cell helptext = row.getCell(7);
    if (helptext != null && Cell.CELL_TYPE_BLANK != helptext.getCellType()) {
        documents = new Documentation();
        documents.setContent(helptext.getStringCellValue());
        documents.setType(DocumentationType.HELP_TEXT);
        documentations.add(documents);
    }

    Cell description = row.getCell(4);
    if (description != null && Cell.CELL_TYPE_BLANK != description.getCellType()) {
        documents = new Documentation();
        documents.setContent(description.getStringCellValue());
        documents.setType(DocumentationType.DESCRIPTION);
        documentations.add(documents);
    }
    String fileExt = "zip";
    Cell filenameCell = row.getCell(8);
    if (filenameCell != null && Cell.CELL_TYPE_BLANK != filenameCell.getCellType()) {

        String filePath = filenameCell.getStringCellValue().trim();

        if (filePath.endsWith(".tar.gz")) {
            fileExt = "tar.gz";
        } else if (filePath.endsWith(".tar")) {
            fileExt = "tar";
        } else if (filePath.endsWith(".zip")) {
            fileExt = "zip";
        } else if (filePath.endsWith(".jar")) {
            fileExt = "jar";
        }

        System.out.println("Uploading jslibrary : " + filePath);
        //      publishJSLibrary(library, filePath, fileExt ,filePath);
    }
    List<Module> moduleVersions = createModules(name, versions, fileExt, no);
    moduleGroup.setModuleId(no);
    moduleGroup.setName(name);
    moduleGroup.setRequired(req);
    moduleGroup.setTechId(techId);
    moduleGroup.setType("js");
    moduleGroup.setDocs(documentations);
    return moduleGroup;
}

From source file:com.photon.phresco.service.tools.PilotProjectGenerator.java

License:Apache License

private String getValue(Cell cell) {
    if (Cell.CELL_TYPE_STRING == cell.getCellType()) {
        return cell.getStringCellValue();
    }//  w  w w. j a v a  2  s. c o  m
    if (Cell.CELL_TYPE_NUMERIC == cell.getCellType()) {
        return String.valueOf(cell.getNumericCellValue());
    }
    return null;
}