简体   繁体   中英

java - Cannot get a NUMERIC value from a STRING cell Exception

I'm currently getting the hang of using the Apache POI and I'm currently trying to import a .xlsx and store it into an ArrayList but the problem is I seem to be getting an exception error that says "Cannot get a NUMERIC value from a STRING cell". This is the message specifically.

Error

I'm pretty sure the value of the cell is has a NUMERIC value.

    private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
    FileNameExtensionFilter filter = new FileNameExtensionFilter("Excel file","xlsx", "xls"); 
    final JFileChooser fc = new JFileChooser();
    fc.setFileFilter(filter);
    fc.setDialogTitle("Excel file selector");
    int returnVal = fc.showOpenDialog(mainMenuPanel);
    if (returnVal == JFileChooser.APPROVE_OPTION){
        jDialogaddItem.setVisible(true);
        try{
            ArrayList<Item> itemList2 = new ArrayList<>();
            Item item2;
            String filepath = fc.getSelectedFile().getAbsolutePath();
            XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(filepath));
            XSSFSheet sheet = wb.getSheetAt(0);
            Iterator<Row> rowIterator = sheet.iterator();
            while(rowIterator.hasNext()){
                Row row = rowIterator.next();
                Iterator<Cell> cellIterator = row.cellIterator(); 
                while(cellIterator.hasNext()){
                    Cell cell = cellIterator.next();                
                    item2 = new Item(cell.getStringCellValue(),cell.getStringCellValue(),cell.getStringCellValue(),cell.getStringCellValue(), (int)cell.getNumericCellValue());
                    itemList2.add(item2);    
                }
            }
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }
    }
}         

My excel file looks like this

Excel File

Get it as string and convert it:

Integer.valueOf(cell.getStringCellValue())

else in excel you should have your cell with numeric format

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM