0

I have created a method to pull the first row and another method to pull the test data. I am unable to add the cell directly to List using list.add(), how to convert them to string and add to list

public class read {

    HSSFCell cell;
    HSSFRow row;
    HSSFWorkbook wbk;
    HSSFSheet sheet;
    List<HSSFCell> cellTempList;

    Hashtable<String, String>[] data = null;    
    Iterator CIterator;
    List<List<HSSFCell>> list;
    HSSFFormulaEvaluator HFE = new HSSFFormulaEvaluator(wbk);

    @SuppressWarnings("unchecked")
    public String[] readFirstRow() throws IOException{
    InputStream ipstream = new FileInputStream(new File("C:\readfile.xls"));
    wbk = new HSSFWorkbook(ipstream);

        sheet = wbk.getSheetAt(0);
        row = sheet.getRow(0);
        int numberofCells = row.getPhysicalNumberOfCells();
        Object result;
        String[] firstRow = new String[numberofCells];

        for(int i = 0; i<= numberofCells; i++){
            CIterator = row.cellIterator();
            while(CIterator.hasNext()){
                CIterator.next();
        HSSFCell cellFirstRow = row.getCell(i);

      switch(cellFirstRow.getCellType()){
      case Cell.CELL_TYPE_BOOLEAN:
          result = cell.getBooleanCellValue();
          break;
      case Cell.CELL_TYPE_NUMERIC:
          result = cell.getNumericCellValue();
          break;
      case Cell.CELL_TYPE_STRING:
          result = cell.getStringCellValue();
          break;
      }

        for(int j=0; j<=numberofCells; j++){
            firstRow[i] = result.toString();
        }
        }


          return firstRow;

      }

        public void getNextRow(){

            Object obj;

            list = new ArrayList<List<HSSFCell>>();
             cellTempList = new ArrayList<HSSFCell>();
            HSSFRow rowNext = sheet.getRow(1);
            Iterator rowIterator = sheet.rowIterator();

            while (rowIterator.hasNext()) {
                rowIterator.next();

            CIterator = rowNext.cellIterator();

            for(int j = 0; j<= rowNext.getPhysicalNumberOfCells();j++){


            while (CIterator.hasNext()) {
                CIterator.next();


                //HSSFCell cell = rowNext.getCell(numberofCells);


              cellTempList.add(cell);
            }
            list.add(cellTempList);
            }           
        }

}
Alexander Vogt
  • 17,879
  • 13
  • 52
  • 68
user2207839
  • 77
  • 1
  • 9

1 Answers1

0

If your cell contains String value, you can get it with ..

String value = cell.getRichStringCellValue().getString();

.. and then add it to a List<String> object

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147