将Excel文件上传到servlet并从中获取数据

时间:2014-10-27 14:55:32

标签: java servlets apache-poi

我试图将excel文件上传到servlet,从中获取数据然后将这些数据发送到数据库。

我一开始就陷入困境:上传文件。

要从文件中获取数据,我想使用Apache POI,这是我的代码:

System.out.println("entered Import.java");
Part filePart = request.getPart("import"); 
System.out.println("filePart: "+filePart);

FileInputStream inputStream = (FileInputStream) filePart.getInputStream();
System.out.println("inputStream: "+inputStream);

Workbook book = WorkbookFactory.create(inputStream);

Sheet sheet = book.getSheetAt(0);

for (Row row : sheet) {
    for (Cell cell : row) {
        System.out.println("row: "+row+", cell value: "+cell.getRichStringCellValue().getString());
    }
}
inputStream.close();

此代码的输出为:

entered Import.java
filePart: org.apache.catalina.core.ApplicationPart@bc6f13
inputStream: java.io.FileInputStream@532048c5
Servlet.service() for servlet [Import] in context with path [/Management] threw exception [Servlet execution threw an exception] with root causejava.lang.ClassNotFoundException: org.apache.xmlbeans.XmlOptions

UI部分是SAPUI5框架和here it is

我查看了thisthis个帖子,但它对我没有帮助。

如何让这个servlet工作?

1 个答案:

答案 0 :(得分:2)

您缺少Web应用程序中包含XmlOptions类的jar。

请参阅(java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlOptions)中的以下回复。

相关问题