2013-10-01 18 views
2

我上傳的Excel文件服務器參考從文本細胞異常得到一個數值:How to upload files to server using JSP/Servlet?java.lang.IllegalStateException:不能同時上傳Excel表單服務器

這裏是我的代碼,這會導致錯誤

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表格中的條目嗎?任何解決方案

回答

0

Excel將日期存儲爲長(數字)。你想要的是一個SimpleDateFormat來解析你的STRING日期​​。

SimpleDateFormat sdf = new SimpleDateFormat("mm/dd/yy"); 
Date d = sdf.parse(cellValueAsString); 
3

根據DOC getDateCellValue()

獲取該小區作爲一個日期的值。 對於字符串,我們拋出異常。對於空白單元格,我們返回null。 返回: 單元格的值作爲日期

因此,您可以檢查使用cell.getCellType()使用哪種類型的單元格。 ,你可以得到的單元格的值的字符串,並將其解析爲使用的SimpleDateFormat這樣

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

Date對象如果單元格的格式爲日期,那麼你可以使用http://poi.apache.org/apidocs/org/apache/poi/hssf/usermodel/HSSFDateUtil.html 檢查,如果單元格中包含一個日期值然後獲取日期的形式,它

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

,如果電池未格式化爲日期(這這裏的情況),但你知道它的格式,它是一個有效的date.you可以使用第一種方法來獲得一個約會。

+0

using cell.celltype()如何區分內容的類型是日期還是數字? –

0

Excel中的單元格未設置爲日期格式,因此即使該值是作爲日期格式化的,Excel也將它視爲字符串。您需要將單元格設置爲日期或將其作爲字符串讀取並解析到日期。

相關問題