java - 将 MultiPartFile 转换为 XWPFDocument

标签 java spring apache ms-word apache-poi

我需要知道如何将 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/

相关文章:

java - 它有一个 "Spring way"来从带注释的接口(interface)中获取实现吗?

php - Seo Friendly URL 导致 CSS IMG 和 JS 不工作

android - Apache HTTPClient 不处理换行符

java - 单例配置类?具体例子

java - Lombok @Synchronized 与 Mockito 抛出 NPE

java - 无法在 Spring Data CRUD 方法上强制执行 Propagation.MANDATORY

php - 针对 Active Directory 的无缝 SSO 身份验证

java - 如何将存储为两个字符 ASCII 字符串的数字转换为二进制?

JavaFX:FXML 中的双向绑定(bind)

java - Spring Hibernate查询帮助