上傳excel解析存到數據庫時報:
org.apache.poi.poifs.filesystem.OfficeXmlFileException:
The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents.
You need to call a different part of POI to process this data (eg XSSF instead of HSSF)
該錯誤意思是說,文件中的數據是用Office2007+XML保存的,而現在卻調用OLE2 Office文檔處理,應該使用POI不同的部分來處理這些數據,
比如使用XSSF來代替HSSF。
斷點跟蹤到:new HSSFWorkbook(file.getInputStream)出錯,在網上找了很多方法,都不行,最後在一個犄角旮旯裡找到如下方法
Workbook wb=WorkbookFactory.create(file.getInputStream);
用的maven依賴(版本號最好一致,否則容易出錯)
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.14-beta1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.14-beta1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>3.14-beta1</version> </dependency>
原文網址::http://www.itnose.net/detail/6144353.html
springmvc上傳excel解析存到數據庫原文博客:http://blog.csdn.net/jinwufeiyang/article/details/52216218
將原博客以下方法稍作了修改。
public List<Customer> getExcelInfo(String fileName, MultipartFile Mfile) { List<Customer> customerList = new ArrayList<Customer>(); try { // 驗證文件名是否合格 if (!validateExcel(fileName)) { return null; } } catch (Exception e) { e.printStackTrace(); } // 根據文件名判斷文件是2003版本還是2007版本 boolean isExcel2003 = true; if (WDWUtil.isExcel2007(fileName)) { isExcel2003 = false; } try { customerList = getExcelInfo(Mfile.getInputStream(), isExcel2003); } catch (IOException e) { e.printStackTrace(); } return customerList; }