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

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

Introduction

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

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the cell This method returns a simple representation, anything more complex should be in user code, with knowledge of the semantics of the sheet being processed.

Usage

From source file:ambit.io.IteratingXLSReader.java

License:Open Source License

public Object next() {
    Molecule mol = null;/*from   w  w  w .j  a v a 2s .co m*/
    try {
        HSSFRow row = (HSSFRow) iterator.next();
        Iterator cols = row.cellIterator();
        Hashtable properties = new Hashtable();

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

            Object value = cell.toString();
            if (cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) {
                /*
                try {
                HSSFFormulaEvaluator.CellValue cellValue = evaluator.evaluate(cell);
                switch (cellValue.getCellType()) {
                case HSSFCell.CELL_TYPE_BOOLEAN:
                    value = cellValue.getBooleanValue();
                    break;
                case HSSFCell.CELL_TYPE_NUMERIC:
                    value = cellValue.getNumberValue();
                    break;
                case HSSFCell.CELL_TYPE_STRING:
                    value = cellValue.toString();
                    break;
                case HSSFCell.CELL_TYPE_BLANK:
                   value = "";
                    break;
                case HSSFCell.CELL_TYPE_ERROR:
                   value = "";
                    break;
                        
                // CELL_TYPE_FORMULA will never happen
                case HSSFCell.CELL_TYPE_FORMULA: 
                    break;
                }
                } catch (Exception x) {
                   x.printStackTrace();
                   value = "";
                }
                */
            }

            if (smilesIndex == cell.getCellNum()) {
                try {
                    mol = sp.parseSmiles(value.toString());
                    properties.put(AmbitCONSTANTS.SMILES, value.toString());
                } catch (InvalidSmilesException x) {
                    logger.warn("Invalid SMILES!\t" + value);
                    properties.put(AmbitCONSTANTS.SMILES, "Invalid SMILES");
                }
            } else
                properties.put(header.get(cell.getCellNum()).toString(), value);

        }
        if (mol == null)
            mol = new Molecule();
        mol.setProperties(properties);
    } catch (Exception x) {
        logger.error(x);
    }
    return mol;

}

From source file:ambit2.io.IteratingXLSReader.java

License:Open Source License

public Object next() {
    IMolecule mol = null;//from  w ww.  j a v  a 2  s  .  co m
    try {
        HSSFRow row = (HSSFRow) iterator.next();
        Iterator cols = row.cellIterator();
        Hashtable properties = new Hashtable();

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

            Object value = cell.toString();
            if (cell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) {
                /*
                try {
                HSSFFormulaEvaluator.CellValue cellValue = evaluator.evaluate(cell);
                switch (cellValue.getCellType()) {
                case HSSFCell.CELL_TYPE_BOOLEAN:
                    value = cellValue.getBooleanValue();
                    break;
                case HSSFCell.CELL_TYPE_NUMERIC:
                    value = cellValue.getNumberValue();
                    break;
                case HSSFCell.CELL_TYPE_STRING:
                    value = cellValue.toString();
                    break;
                case HSSFCell.CELL_TYPE_BLANK:
                   value = "";
                    break;
                case HSSFCell.CELL_TYPE_ERROR:
                   value = "";
                    break;
                        
                // CELL_TYPE_FORMULA will never happen
                case HSSFCell.CELL_TYPE_FORMULA: 
                    break;
                }
                } catch (Exception x) {
                   x.printStackTrace();
                   value = "";
                }
                */
            }

            if (smilesIndex == cell.getCellNum()) {
                try {
                    mol = sp.parseSmiles(value.toString());
                    properties.put(AmbitCONSTANTS.SMILES, value.toString());
                } catch (InvalidSmilesException x) {
                    logger.warn("Invalid SMILES!\t" + value);
                    properties.put(AmbitCONSTANTS.SMILES, "Invalid SMILES");
                }
            } else
                properties.put(header.get(cell.getCellNum()).toString(), value);

        }
        if (mol == null)
            mol = new Molecule();
        mol.setProperties(properties);
    } catch (Exception x) {
        logger.error(x);
    }
    return mol;

}

From source file:axiom.util.TextExtractor.java

License:Open Source License

public static String msExcelExtractor(InputStream is) throws Exception {
    POIFSFileSystem fs = new POIFSFileSystem(is);
    HSSFWorkbook wb = new HSSFWorkbook(fs);
    StringBuffer sb = new StringBuffer();

    final int numSheets = wb.getNumberOfSheets();
    for (int k = 0; k < numSheets; k++) {
        HSSFSheet sheet = wb.getSheetAt(k);
        Iterator rIt = sheet.rowIterator();
        while (rIt.hasNext()) {
            HSSFRow row = (HSSFRow) rIt.next();
            Iterator cIt = row.cellIterator();
            while (cIt.hasNext()) {
                HSSFCell cell = (HSSFCell) cIt.next();
                sb.append(cell.toString()).append(" ");
            }/*  ww w.  j av  a  2s. c om*/
        }
    }

    return sb.toString();
}

From source file:cdc.impl.datasource.office.ExcelDataSource.java

License:LGPL

private static String decodeValue(HSSFCell cell, HSSFFormulaEvaluator evaluator) throws RJException {
    if (cell == null) {
        return "";
    }//from  www.ja  v a  2s .co  m
    switch (evaluator.evaluateInCell(cell).getCellType()) {
    case HSSFCell.CELL_TYPE_BLANK:
        return "";
    case HSSFCell.CELL_TYPE_BOOLEAN:
        return String.valueOf(cell.getBooleanCellValue());
    case HSSFCell.CELL_TYPE_ERROR:
        return "";
    case HSSFCell.CELL_TYPE_FORMULA:
        break;
    case HSSFCell.CELL_TYPE_NUMERIC:
        if (HSSFDateUtil.isCellDateFormatted(cell)) {
            return cell.toString();
        } else {
            return formatter.format(cell.getNumericCellValue());
        }
    case HSSFCell.CELL_TYPE_STRING:
        return cell.getRichStringCellValue().getString();
    }
    throw new RJException("Error reading data from Excel input file");
}

From source file:ch.elexis.core.importer.div.importers.ExcelWrapper.java

License:Open Source License

/**
 * Return a row of data from the sheet.//from   w  w w  .  j  a  v a2  s . co m
 * 
 * @param rowNr
 *            zero based index of the desired row
 * @return a List of Strings with the row values or null if no such row exists.
 */
public List<String> getRow(final int rowNr) {
    HSSFRow row = sheet.getRow(rowNr);
    if (row == null) {
        return null;
    }
    ArrayList<String> ret = new ArrayList<String>();
    short first = 0;
    short last = 100;
    if (types != null) {
        last = (short) (types.length);
    } else {
        first = row.getFirstCellNum();
        last = row.getLastCellNum();
    }
    for (short i = first; i < last; i++) {
        HSSFCell cell = row.getCell(i);
        if (cell != null) {
            switch (cell.getCellType()) {
            case HSSFCell.CELL_TYPE_BLANK:
                ret.add(""); //$NON-NLS-1$
                break;
            case HSSFCell.CELL_TYPE_BOOLEAN:
                ret.add(Boolean.toString(cell.getBooleanCellValue()));
                break;
            case HSSFCell.CELL_TYPE_NUMERIC:
                if (types != null) {
                    if (types[i].equals(Integer.class)) {
                        ret.add(Long.toString(Math.round(cell.getNumericCellValue())));
                    } else if (types[i].equals(TimeTool.class)) {
                        Date date = cell.getDateCellValue();
                        if (date != null) {
                            TimeTool tt = new TimeTool(date.getTime());
                            ret.add(tt.toString(TimeTool.FULL_MYSQL));
                        } else {
                            ret.add(""); //$NON-NLS-1$
                        }
                    } else if (types[i].equals(Double.class)) {
                        ret.add(Double.toString(cell.getNumericCellValue()));
                        break;
                    } else /* if(types[i].equals(String.class)) */ {
                        double cv = cell.getNumericCellValue();
                        // String r=Double.toString(cv);
                        String r = NumberFormat.getNumberInstance().format(cv);
                        ret.add(r);
                    }
                    break;
                } // else fall thru
            case HSSFCell.CELL_TYPE_FORMULA:
                ret.add(Double.toString(cell.getNumericCellValue()));
                break;
            case HSSFCell.CELL_TYPE_STRING:
                ret.add(cell.toString());
                break;
            default:
                ret.add(Messages.ExcelWrapper_ErrorUnknownCellType);
            }

        } else {
            // empty cell
            ret.add(""); //$NON-NLS-1$
        }
    }
    return ret;
}

From source file:ch.javasoft.metabolic.parse.ExcelParser.java

License:BSD License

/**
 * Parses the excel file and returns the resulting metabolic network,
 * skipping <tt>headerRows</tt> rows, using the given columns to extract
 * information from, column indices being 0-based. The given pattern is
 * used to recognize external metabolites; a reversible exchange flux 
 * reaction is added to each external metabolite.
 * /*w w  w. j  a v  a 2s  .co m*/
 * @param reactionColumn      0-based column index for reaction formula
 * @param reactionNameColumn   0-based column index for reaction name
 * @param headerRows         number of header rows (being ignored)
 * @param externalPattern      pattern to recognize external metabolites
 * @throws IOException         if any unexpected exception occurs
 */
public MetabolicNetwork parse(int reactionColumn, int reactionNameColumn, int headerRows,
        Pattern externalPattern) throws IOException {
    Set<String> reacNames = new HashSet<String>();
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    PrintWriter pw = new PrintWriter(new OutputStreamWriter(buf));
    HSSFRow row;
    int rowIndex = headerRows;
    try {
        row = mSheet.getRow(rowIndex++);
        while (row != null) {
            HSSFCell nameCell = row.getCell((short) reactionNameColumn);
            HSSFCell formCell = row.getCell((short) reactionColumn);
            if (nameCell != null || formCell != null) {
                if (nameCell == null) {
                    throw new IOException("reaction name cell " + (reactionNameColumn + 1) + " is empty");
                }
                if (formCell == null) {
                    throw new IOException("reaction formula cell " + (reactionColumn + 1) + " is empty");
                }
                String name = nameCell.toString().trim();
                String form = formCell.toString().trim();
                if (name.length() > 0 && form.length() > 0) {
                    if (reacNames.contains(name))
                        throw new Exception("duplicate reaction name: " + name);
                    reacNames.add(name);
                    pw.println("\"" + name + "\"\t\"" + name + "\"\t\"" + form + "\"");
                    row = mSheet.getRow(rowIndex++);
                } else {
                    LOG.info("row " + (rowIndex + 1) + " found empty in excel file '" + mFile.getAbsolutePath()
                            + "', stopping here.");
                    row = null;
                }
            } else {
                LOG.info("row " + (rowIndex + 1) + " found empty in excel file '" + mFile.getAbsolutePath()
                        + "', stopping here.");
                row = null;
            }
        }
        pw.flush();
        ByteArrayInputStream in = new ByteArrayInputStream(buf.toByteArray());
        CompartmentReaction[] reacts = externalPattern == null
                ? new PalssonParser().parseReactions(new InputStreamReader(in))
                : new PalssonParser().parseReactions(new InputStreamReader(in), externalPattern);
        return new DefaultMetabolicNetwork(reacts);
    } catch (Exception ex) {
        IOException ioe = new IOException(ex.getMessage() + " [row " + (rowIndex + 1) + ", file="
                + mFile.getAbsolutePath() + ", sheet=" + mSheetName + "]");
        ioe.initCause(ex);
        throw ioe;
    }
}

From source file:com.bayareasoftware.chartengine.ds.util.ExcelDump.java

License:Apache License

private static String cell2string(HSSFCell c) {
    if (c == null) {
        return "<i>NULL CELL</i>";
    }//w  w  w .ja v  a 2s .c om
    int type = c.getCellType();
    String t = null, v = null;
    switch (type) {
    case HSSFCell.CELL_TYPE_BLANK:
        t = "BLANK";
        v = "";
        break;
    case HSSFCell.CELL_TYPE_STRING:
        t = "STRING";
        v = c.getStringCellValue();
        break;
    case HSSFCell.CELL_TYPE_NUMERIC:
        if (ExcelInference.isCellDateFormatted(c)) {
            t = "DATE";
            v = c.getDateCellValue() + "";
        } else {
            t = "NUMERIC";
            v = c.getNumericCellValue() + "";
        }
        break;
    case HSSFCell.CELL_TYPE_ERROR:
        t = "ERROR";
        v = "(errByte=" + c.getErrorCellValue() + "/toString=" + c + ")";
        break;
    case HSSFCell.CELL_TYPE_FORMULA:
        t = "FORMULA";
        v = c.getCellFormula();
        break;
    default:
        t = "(UNKNOWN TYPE: " + type + ")";
        v = c.toString();
        break;
    }
    short style = c.getCellStyle().getDataFormat();
    return v + "<br/>(" + t + ")<br/>dataformat=0x" + Integer.toHexString(style);
}

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

/**
* ?2003excel// w  w  w  .  j  a  va  2s .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.dv.util.DVExcelIO.java

License:Open Source License

public static Vector<Vector> readExcelReturnArrayList(String fileName, String sheetName, int rowCount) {

    Vector<Vector> hm = new Vector<Vector>();

    File file = new File(fileName);
    FileInputStream in = null;/*from   www. j  a v a2  s  . co  m*/

    try {

        in = new FileInputStream(file);
        HSSFWorkbook workbook = new HSSFWorkbook(in);
        HSSFSheet sheet = workbook.getSheet(sheetName);

        HSSFRow row = null;
        HSSFCell cell = null;

        for (int i = 0; i < rowCount; i = i + 3) {

            Vector cellList = new Vector();

            row = sheet.getRow((short) i);
            if (row != null) {
                cell = row.getCell(0);
                String cellString = cell.toString().replace(".0", " ").trim();
                cellString = cellString.replace("\n", " ").trim();
                cellList.add(0, cellString);
            }

            row = sheet.getRow((short) i + 1);
            if (row != null) {
                cell = row.getCell(0);
                String cellString = cell.toString().replace(".0", " ").trim();
                cellString = cellString.replace("\n", " ").trim();
                cellList.add(1, cellString);
            }

            row = sheet.getRow((short) i + 2);
            if (row != null) {
                cell = row.getCell(0);
                String cellString = cell.toString().replace(".0", " ").trim();
                cellString = cellString.replace("\n", " ").trim();
                cellList.add(2, cellString);
            }

            hm.addElement(cellList);

        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e1) {
            }
        }
    }

    return hm;

}

From source file:com.dv.util.DVExcelIO.java

License:Open Source License

public static Vector setExcelBHTIMFormat(String fullExcelFileName, String sheetName, int rowNumbers) {

    File file = new File(fullExcelFileName);
    FileInputStream in = null;//from   www.j a v  a 2  s . c  o  m
    Vector cols = new Vector();

    try {

        in = new FileInputStream(file);
        HSSFWorkbook workbook = new HSSFWorkbook(in);
        HSSFSheet sheet = workbook.getSheet(sheetName);

        HSSFRow row = null;
        HSSFCell cell = null;

        for (int i = 2; i < rowNumbers; i++) {

            row = sheet.getRow(i);

            cell = row.getCell(4);//9 for cty

            String ppp = cell.toString().trim();

            cell = row.getCell(9);

            String fff = cell.toString().trim();

            if (!ppp.equals("")) {

                String contents = "Verify from FMS side for " + ppp + "(" + fff + ")";

                //                    cols.addElement(contents);

                row.getCell(16).setCellValue(contents);

            } else {
                return null;
            }

        }

        FileOutputStream fOut = new FileOutputStream(file);
        workbook.write(fOut);
        fOut.flush();
        fOut.close();

    } catch (Exception eee) {

    }

    return cols;
}