我需要知道如何将 multipartFile 转换为 XWPFDocument 以便将其作为 Word 文件读取(我上传的文件实际上是 .docx ),但是我收到错误:
org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoClassDefFoundError: org/apache/poi/UnsupportedFileFormatException
这是我的代码:
@Override
public void addReport(MultipartFile report) throws ApcException {
try{
File convFile = new File(report.getOriginalFilename());
convFile.createNewFile();
FileOutputStream fos = new FileOutputStream(convFile);
fos.write(report.getBytes());
fos.close();
FileInputStream fis = new FileInputStream(convFile.getAbsolutePath());
XWPFDocument docx = new XWPFDocument(fis);
List<XWPFTable> tables =docx.getTables();
invokeUpdate(ADD_REPORT,new Object[]{
tables.get(0).getRow(1).getCell(0),
tables.get(1).getRow(0).getCell(0),
tables.get(2).getRow(0).getCell(0),
tables.get(3).getRow(2).getCell(1),
tables.get(4).getRow(1).getCell(1)});
}
catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
这是我的 pom.xml 中的 Apache 依赖项:
对于 Apache POI,我有这些:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.7</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
感谢您的帮助!
最佳答案
您需要将 apache poi 版本从 3.7 替换为 3.17。
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
最新的稳定版本是 Apache POI 3.17
关于java - 将 MultiPartFile 转换为 XWPFDocument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51139249/