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

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

Introduction

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

Prototype

public 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.pureinfo.studio.db.txt2SRM.impl.SchoolSCITxtImportRunner.java

License:Open Source License

/**
 * @param _sString//from  www.  j  a  va  2  s  .  c  om
 * @throws PureException
 * @throws IOException
 * @throws FileNotFoundException
 */
public List check(String _sFileName) throws PureException, FileNotFoundException, IOException {

    POIFSFileSystem fs;
    HSSFSheet m_sheet = null;
    FileInputStream fileInputStream = new FileInputStream(_sFileName);
    String[] m_heads;

    fs = new POIFSFileSystem(fileInputStream);
    HSSFWorkbook wb = new HSSFWorkbook(fs);
    m_sheet = wb.getSheetAt(0);
    HSSFRow row = m_sheet.getRow(0);
    HSSFCell cell;

    // TITLE
    // BY lily
    List list = new ArrayList(row.getLastCellNum());
    for (int i = 0; i < row.getLastCellNum(); i++) {
        cell = row.getCell((short) i);
        if (cell == null) {
            break;
        }
        list.add(cell.getStringCellValue().trim());
    }
    m_heads = new String[list.size()];
    list.toArray(m_heads);
    list.clear();
    fileInputStream.close();
    return checkExcelHead(m_heads);

}

From source file:com.pureinfo.studio.db.xls2srm.impl.ProjectBatchImportRunner.java

License:Open Source License

/**
 * @param _sString/*from w  ww . j  av a2 s. co m*/
 * @throws PureException
 * @throws IOException
 * @throws FileNotFoundException
 */
public List check(String _sFileName) throws PureException, FileNotFoundException, IOException {
    POIFSFileSystem fs;
    HSSFSheet m_sheet = null;
    FileInputStream fileInputStream = new FileInputStream(_sFileName);
    String[] m_heads;

    fs = new POIFSFileSystem(fileInputStream);
    HSSFWorkbook wb = new HSSFWorkbook(fs);
    m_sheet = wb.getSheetAt(0);
    HSSFRow row = m_sheet.getRow(0);
    HSSFCell cell;

    // TITLE
    // BY lily
    List list = new ArrayList(row.getLastCellNum());
    for (int i = 0; i < row.getLastCellNum(); i++) {
        cell = row.getCell((short) i);
        if (cell == null) {
            break;
        }
        list.add(cell.getStringCellValue().trim());
    }
    m_heads = new String[list.size()];
    list.toArray(m_heads);
    list.clear();
    fileInputStream.close();
    return checkExcelHead(m_heads);

}

From source file:com.pureinfo.studio.db.xls2srm.impl.XlsObjectsImpl.java

License:Open Source License

public XlsObjectsImpl(String _sFileName) throws PureException {
    POIFSFileSystem fs;/*from  w  w  w .  ja v  a 2  s  . c  o  m*/
    try {
        fs = new POIFSFileSystem(new FileInputStream(_sFileName));
        HSSFWorkbook wb = new HSSFWorkbook(fs);
        m_sheet = wb.getSheetAt(0);
        HSSFRow row = m_sheet.getRow(0);
        List list = new ArrayList(row.getLastCellNum());
        HSSFCell cell;

        for (int i = 0; i < row.getLastCellNum(); i++) {
            cell = row.getCell((short) i);
            if (cell == null) {
                break;
            }
            list.add(cell.getStringCellValue().trim().toUpperCase());
        }
        m_heads = new String[list.size()];
        list.toArray(m_heads);
        list.clear();
    } catch (Exception ex) {
        throw new PureException(PureException.UNKNOWN, "", ex);
    }

}

From source file:com.pureinfo.studio.db.xls2srm.impl.XlsObjectsImpl.java

License:Open Source License

/**
 * @see com.pureinfo.dolphin.model.IObjects#next()
 *///from www. j  a v  a  2s  . c om
public DolphinObject next() throws PureException {
    HSSFRow row = m_sheet.getRow(m_nCurrent++);
    if (row == null)
        return null;

    //else
    DolphinObject obj = new DolphinObject();
    Object oValue;
    HSSFCell cell;

    int nCellNum = row.getLastCellNum();
    if (nCellNum > m_heads.length) {
        nCellNum = m_heads.length;
    }
    for (int i = 0; i < nCellNum; i++) {
        cell = row.getCell((short) i);
        if (cell == null) {
            oValue = null;
        } else {
            switch (cell.getCellType()) {
            case HSSFCell.CELL_TYPE_NUMERIC:
                oValue = new Double(cell.getNumericCellValue());
                break;

            case HSSFCell.CELL_TYPE_STRING:
                oValue = cell.getStringCellValue();
                if (oValue != null)
                    oValue = ((String) oValue).trim();
                break;

            case HSSFCell.CELL_TYPE_FORMULA:
                oValue = new Double(cell.getNumericCellValue());
                break;

            case HSSFCell.CELL_TYPE_BOOLEAN:
                oValue = new Boolean(cell.getBooleanCellValue());
                break;

            case HSSFCell.CELL_TYPE_ERROR:
                throw new PureException(PureException.INVALID_VALUE, "error value in cell[" + i + "]-"
                        + m_heads[i] + ": " + String.valueOf(cell.getErrorCellValue()));
                //case HSSFCell.CELL_TYPE_BLANK:
            default:
                oValue = null;
            }//endcase

            if (oValue instanceof Number) {
                int nFormat = cell.getCellStyle().getDataFormat();
                if (nFormat >= 0xe && nFormat <= 0x16) {
                    oValue = cell.getDateCellValue();
                } else if (nFormat == 1) {
                    oValue = new Long(((Number) oValue).intValue());
                }
            }
        }

        obj.setProperty(m_heads[i], oValue);
    }
    return obj;
}

From source file:com.qms.operations.rates.ejb.sls.BuyRatesSessionBean.java

License:Open Source License

private String getCellData(HSSFCell cell, int j) throws Exception {
    //   String format =  loginBean.getUserPreferences().getDateFormat();
    String s = "";
    ESupplyDateUtility fomater = null;/*from   w  w w  . j  a va  2s . c o  m*/
    try {
        fomater = new ESupplyDateUtility();

        if (cell != null) {
            if (cell != null) {

                if (cell.CELL_TYPE_STRING == cell.getCellType()) {
                    s = cell.getStringCellValue();
                } else if (cell.CELL_TYPE_NUMERIC == cell.getCellType()) {
                    // if( HSSFDateUtil.isCellDateFormatted(cell))
                    if (j == 6 || j == 7) {
                        SimpleDateFormat format = new SimpleDateFormat();
                        format.applyPattern("dd/MMM/yy");

                        s = format.format(cell.getDateCellValue());

                        // s =  cell.getDateCellValue();
                    } else {
                        s = String.valueOf(cell.getNumericCellValue()).trim();
                    }

                }
                // 
                else if (cell.CELL_TYPE_BLANK == cell.getCellType())
                    s = "";
                else if (cell.CELL_TYPE_ERROR == cell.getCellType())
                    s = "";
                else
                    s = "";
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
    return s;
}

From source file:com.raisepartner.chartfusion.generator.XLSParser.java

License:Open Source License

/**
 * Extractes a String value from a cell/*from  ww w .ja  va 2s.c o  m*/
 * @param cell is an XLS cell
 * @param defaultValue is the default value returned if cell does not 
 * contains data (null or empty String).
 * @return a String instance or the default value
 */
public String getStringValue(HSSFCell cell, String defaultValue) {
    if (cell == null) {
        return defaultValue;
    } else {
        String s;
        try {
            s = cell.getStringCellValue();
        } catch (RuntimeException e) {
            return "" + cell.getNumericCellValue();
        }
        if (s == null || s.length() == 0) {
            return defaultValue;
        } else {
            return s;
        }
    }
}

From source file:com.scoretracker.service.ScoreTrackerServiceImpl.java

public ArrayList<String> getExcel() {
    ArrayList<String> lijst = new ArrayList<String>();
    try {/*from   www.j a  v a 2  s . c  om*/

        FileInputStream fileInputStream = new FileInputStream("C:\\poi-test.xls");
        HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
        HSSFSheet worksheet = workbook.getSheet("POI Worksheet");
        HSSFRow row1 = worksheet.getRow(0);
        HSSFCell cellA1 = row1.getCell((short) 0);
        String a1Val = cellA1.getStringCellValue();
        HSSFCell cellB1 = row1.getCell((short) 1);
        String b1Val = cellB1.getStringCellValue();
        HSSFCell cellC1 = row1.getCell((short) 2);
        String c1Val = cellC1.getStringCellValue();
        HSSFCell cellD1 = row1.getCell((short) 3);
        String d1Val = cellD1.getStringCellValue();

        lijst.add(a1Val);
        lijst.add(b1Val);
        lijst.add(c1Val);
        lijst.add(d1Val);

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return lijst;
}

From source file:com.servoy.extensions.plugins.excelxport.ImportSelectSheetPanel.java

License:Open Source License

public Object getValueAt(int r, int c) {
    if (r == -1) {
        r = 0;//from   w ww .j a  va2  s.co m
    } else if (useHeaderRow) {
        r++;
    }
    HSSFRow row = sheet.getRow(r);
    if (row != null) {
        HSSFCell cell = row.getCell((short) c);
        if (cell != null) {
            switch (cell.getCellType()) {
            case Cell.CELL_TYPE_NUMERIC:
                Number d = new Double(cell.getNumericCellValue());
                if (((int) d.doubleValue()) == Math.ceil(d.doubleValue())) {
                    d = new Integer(d.intValue());
                }
                return d;
            //               case HSSFCell.CELL_TYPE_NUMERIC:
            //               return cell.getStringCellValue();   

            default:
                return cell.getStringCellValue();
            }
        }
    }
    return null;
}

From source file:com.smanempat.controller.ControllerClassification.java

private void showXLS(JTextField txtFileDirectory, JTable tablePreview)
        throws FileNotFoundException, IOException {
    DefaultTableModel tableModel = new DefaultTableModel();
    File fileName = new File(txtFileDirectory.getText());
    FileInputStream inputStream = new FileInputStream(fileName);
    HSSFWorkbook workBook = new HSSFWorkbook(inputStream);
    HSSFSheet sheet = workBook.getSheetAt(0);

    int rowValue = sheet.getLastRowNum() + 1;
    int colValue = sheet.getRow(0).getLastCellNum();
    String[][] data = new String[rowValue][colValue];
    String[] colName = new String[colValue];
    for (int i = 0; i < rowValue; i++) {
        HSSFRow row = sheet.getRow(i);//from w ww .  j  ava2s. c om
        for (int j = 0; j < colValue; j++) {
            HSSFCell cell = row.getCell(j);
            int type = cell.getCellType();
            Object returnCellValue = null;
            if (type == 0) {
                returnCellValue = cell.getNumericCellValue();
            } else if (type == 1) {
                returnCellValue = cell.getStringCellValue();
            }

            data[i][j] = returnCellValue.toString();
        }
    }

    for (int i = 0; i < colValue; i++) {
        colName[i] = data[0][i];
    }

    tableModel = new DefaultTableModel(data, colName);
    tablePreview.setModel(tableModel);
    tableModel.removeRow(0);
}

From source file:com.smanempat.view.ReadWorkbook.java

String nilaiCell(HSSFCell pCell) {
    int tipe = pCell.getCellType();
    Object nilaiBalik = null;//from w ww  . j a  v  a  2s  . com

    if (tipe == 0) {
        nilaiBalik = pCell.getNumericCellValue();

    } else if (tipe == 1) {
        nilaiBalik = pCell.getStringCellValue();
    }

    return nilaiBalik.toString();
}