Haricharan Shetty
Haricharan Shetty

Reputation: 113

how to convert hssfcell to string in java

I have a piece of code which throws me (I have made the line bold)

Exception in thread "main" java.lang.ClassCastException: org.apache.poi.hssf.usermodel.HSSFCell cannot be cast to java.lang.String
    at com.codi.excel.ExcelRead.main(ExcelRead.java:36)

My code is as follows -

HSSFWorkbook wb = new HSSFWorkbook(input);
            HSSFSheet sheet = wb.getSheetAt(0);
            List MobileSeries=new ArrayList();
            MobileSeries = findRow(sheet, cellContent);

            if(MobileSeries !=null){
                for(Iterator iter=MobileSeries.iterator();iter.hasNext();){
                    **String mobileSeries=(String)iter.next();**
                    String LidPattern=extractNumber(mobileSeries);
                    if (lid.startsWith(LidPattern)) {
                    System.out.println("This is a mobile number");

Could you please help me out.

Upvotes: 1

Views: 5675

Answers (3)

McNultyyy
McNultyyy

Reputation: 1032

Try to access cell and extract value out of it. The below snippet should help you:

Cell cell = iter.next();
cell.getStringCellValue()

Upvotes: -1

Gagravarr
Gagravarr

Reputation: 48336

Apache POI provides a handy class for you to do just that - DataFormatter

Using DataFormatter, for string cells you'll get the current contents, and for numeric or date cells the value will be formatted based on the formatting / styling rules applied to the cell, then returned as a string with that applied.

To loop over all the rows and cells in a workbook, getting their values, following the pattern in the docs, just do something like:

Workbook wb = WorkbookFactory.create(input);
Sheet sheet = wb.getSheetAt(0);
DataFormatter formatter = new DataFormatter();
for (Row r : sheet) {
   for (Cell c : r) {
       String value = formatter.formatCellValue(c);
   }
}

Easy!

Upvotes: 2

Sandeep Chatterjee
Sandeep Chatterjee

Reputation: 3249

While you are iterating the rows of a worksheet, check whether the HSSFCell cell is of type String or not.

InputStream fileInputStream = null;
HSSFWorkbook hssfWorkbook;
HSSFSheet sheet;
HSSFRow row;
HSSFCell cell;
Iterator rowIterator, cellIterator;

// Put these loc in a try-catch block
fileInputStream = new FileInputStream("/path/to/TestExcel.xls");
hssfWorkbook = new HSSFWorkbook(fileInputStream);
sheet = hssfWorkbook.getSheetAt(0);
rowIterator = sheet.rowIterator();

while (rowIterator.hasNext()) {
    row = (HSSFRow) rowIterator.next();
    cellIterator = row.cellIterator();
    while (cellIterator.hasNext()) {
        cell = (HSSFCell) cellIterator.next();

        if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
            String someVariable = cell.getStringCellValue();
        } else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
            // Handle numeric type
        } else {
          // Handle other types
        }
    }
    // Other code
}

Upvotes: 0

Related Questions