java.lang.IllegalStateException : Cannot get a numeric value from a text cell Exception while uploading excel sheet to server 错误

标签 java jsp servlets apache-poi

我正在将 Excel 文件上传到服务器引用:How to upload files to server using JSP/Servlet?

这是导致错误的代码

public static ArrayList<MOPBDMMapping> readExcel(InputStream fileInputStream)
    {

        ArrayList<MOPBDMMapping> mappings = new ArrayList<MOPBDMMapping>();
        try{
        HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
        HSSFSheet worksheet = workbook.getSheetAt(0);
        HSSFRow row1 = worksheet.getRow(0);

        int noOfRows = worksheet.getLastRowNum();

        for(int i = 1 ; i <= noOfRows ; i++){
            MOPBDMMapping mapping = new MOPBDMMapping();

            HSSFCell cell1 = row1.getCell(0);
            mapping.setMappingID(cell1.getStringCellValue());   

            HSSFCell cell2 = row1.getCell(1);
            mapping.setMopID(cell2.getStringCellValue());

            HSSFCell cell3 = row1.getCell(2);
            mapping.setMopName(cell3.getStringCellValue());

            HSSFCell cell4 = row1.getCell(3);
            mapping.setBdmID(cell4.getStringCellValue());

            HSSFCell cell5 = row1.getCell(4);
            mapping.setBdmName(cell5.getStringCellValue());

            HSSFCell cell6 = row1.getCell(5);
            mapping.setBranch(cell6.getStringCellValue());

            HSSFCell cell7 = row1.getCell(6);
            mapping.setStartDate(cell7.getDateCellValue());   //Error Line

            HSSFCell cell8 = row1.getCell(7);
            mapping.setEndDate(cell8.getDateCellValue());

            HSSFCell cell9 = row1.getCell(8);
            mapping.setActive(cell9.getStringCellValue());

            mappings.add(mapping);
        }
        System.out.println(mappings.size());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
        return mappings;
    }
}

这是抛出的异常

java.lang.IllegalStateException: Cannot get a numeric value from a text cell
        org.apache.poi.hssf.usermodel.HSSFCell.typeMismatch(HSSFCell.java:643)
        org.apache.poi.hssf.usermodel.HSSFCell.getNumericCellValue(HSSFCell.java:668)
        org.apache.poi.hssf.usermodel.HSSFCell.getDateCellValue(HSSFCell.java:689)
        com.tcs.rspm.mops.business.ExcelReader.readExcel(ExcelReader.java:52)
        com.tcs.rspm.mop.upload.ExcelUploadController.doPost(ExcelUploadController.java:42)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

这是我的 Servlet 代码

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("In controller..");
        try {
            List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
            for (FileItem item : items) {
                if (item.isFormField()) {
                    // Process regular form field (input type="text|radio|checkbox|etc", select, etc).
                    String fieldname = item.getFieldName();
                    String fieldvalue = item.getString();
                    System.out.println( "Normal Field : " + fieldname + fieldvalue);
                    // ... (do your job here)
                } else {
                    // Process form file field (input type="file").
                    String fieldname = item.getFieldName();
                    String filename = FilenameUtils.getName(item.getName());
                    System.out.println("File field : " + fieldname + filename );
                    InputStream filecontent = item.getInputStream();
                    ExcelReader.readExcel(filecontent);

                }
            }
        } catch (FileUploadException e) {
            throw new ServletException("Cannot parse multipart request.", e);
        }
        System.out.println("Out controller..");
        // ...
    }

可能是什么原因? '08/12/13' 是 Excel 表中的条目吗?有什么解决办法吗?

最佳答案

根据文档 getDateCellValue()

获取单元格的值作为日期。 对于字符串,我们抛出异常。对于空白单元格,我们返回一个空值。 返回: 单元格的值作为日期

因此您可以使用 cell.getCellType() 检查它使用的是哪种类型的单元格。 您可以像这样使用 SimpleDateFormat 将单元格值作为 String 并解析为 Date 对象

SimpleDateFormat dateFormat = new SimpleDateFormat("dd/mm/yy");
        Date date = dateFormat.parse(yourStringDateHere);

如果您的单元格格式为日期,那么您可以使用 http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFDateUtil.html 检查单元格是否包含日期值然后从中获取日期

if(HSSFDateUtil.isCellDateFormatted(cell)){
            Date date = HSSFDateUtil.getJavaDate(cell.getNumericCellValue());
        } 

如果单元格未格式化为日期(这里是这种情况)但您知道它的格式并且它是有效日期。您可以使用第一种方法来获取日期。

关于java.lang.IllegalStateException : Cannot get a numeric value from a text cell Exception while uploading excel sheet to server 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19115421/

相关文章:

java - Facebook access_token 检索

java - 为什么java Iterator接口(interface)应该实现为内部类?

jsp - 使用 c :url in c:set

java - 如果只有表单元素是提交按钮,JSP/servlet 组合不会在 Enter 时提交表单

java - servlet 过滤器如何在请求页面上发送错误消息?

java - 使用 Object.wait 代替 Thread.sleep 进行时间同步

java - 由于 Hibernate Mapping 需要将某些字段作为 @Transient 但 JSP 无法访问它们

java - 当 servlet 的 doGet() 仍在运行时检测 J2EE 容器停止事件

java - 如何在 java servlet 中以分块响应发送 Http 预告片/页脚?

JavaFX:如何临时阻止 GUI