简体   繁体   English

java - 无法从 STRING 单元格异常中获取 NUMERIC 值

[英]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".我目前正在掌握使用 Apache POI 的窍门,我目前正在尝试导入 .xlsx 并将其存储到 ArrayList 中,但问题是我似乎收到了一个异常错误,提示“无法从一个 STRING 单元格”。 This is the message specifically.这是专门的消息。

Error错误

I'm pretty sure the value of the cell is has a NUMERIC value.我很确定单元格的值有一个 NUMERIC 值。

    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文件是这样的

Excel File Excel文件

Get it as string and convert it:将其作为字符串获取并进行转换:

Integer.valueOf(cell.getStringCellValue())

else in excel you should have your cell with numeric format否则在 excel 中,您应该使用数字格式的单元格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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