【问题标题】:Converting MultiPartFile to XWPFDocument将 MultiPartFile 转换为 XWPFDocument
【发布时间】:2018-12-10 20:42:58
【问题描述】:

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

感谢您的帮助!

【问题讨论】:

  • 你能告诉我们你的依赖吗?

标签: java spring apache ms-word apache-poi


【解决方案1】:

您需要将 apache poi 版本从 3.7 替换为 3.17。

<dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>3.17</version>
</dependency>

最新的稳定版本是 Apache POI 3.17

【讨论】:

  • 谢谢你解决了我的问题!
猜你喜欢
  • 2020-05-07
  • 2013-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-31
  • 1970-01-01
  • 1970-01-01
  • 2020-04-11
相关资源
最近更新 更多