【问题标题】:Excel/Word to PDF with Apache POI使用 Apache POI 将 Excel/Word 转换为 PDF
【发布时间】:2018-09-25 14:24:20
【问题描述】:

我正在测试 Apache POI,但我有问题。
我正在使用 maven 和 java。

  1. 可以在不阅读的情况下将excel文件转换为PDF(类似于PdfConverter.getInstance().convert
  2. 如果没有步骤1中的功能,我该如何保留excel的样式、格式和其他东西?
  3. 为什么这个代码PdfConverter.getInstance().convert(doc,outFile,options);有这个错误java.lang.ClassNotFoundException: org.apache.poi.POIXMLDocumentPart(见下面的代码)?
  4. 是否有其他免费的库可供我使用?

我附上了pom.xml和单词转换的代码:

<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.0.0</version>
</dependency>

<!-- org.apache.poi/poi-ooxml -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.0.0</version>
</dependency>   

<!-- org.apache.poi.xwpf.converter.pdf -->
<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
    <version>1.0.6</version>
</dependency>    

<!-- com.itextpdf/itextpdf -->
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13</version>
</dependency>

单词转换代码:

File file1 = new File("myWord.docx");
FileInputStream fileInpStr1 = new FileInputStream(file1.getAbsolutePath());

XWPFDocument doc = new XWPFDocument(fileInpStr1);
OutputStream outFile = new FileOutputStream("myPDFout.pdf"));

PdfOptions options = null;
PdfConverter.getInstance().convert(doc,outFile,options); <-- here the error jumps

outFile.close();
doc.close();

【问题讨论】:

标签: java excel maven ms-word apache-poi


【解决方案1】:

我在尝试通过 POI 将 word(.docx) 转换为 pdf 时遇到了同样的问题。错误是 java.lang.NoSuchMethodError:org.apache.poi.POIXMLDocumentPart.getPackageRelationship()Lorg/apache/poi/openxml4j/opc/PackageRelationshipe 我通过将 POI 的版本从 3.16 更改为 3.15 来解决这个问题(请参阅下面的依赖项)。但我认为这不是最佳解决方案。

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.15</version>  <-- right version number
        </dependency>

因此,如果您通过不更改版本来解决此问题,请向我展示您的代码。

【讨论】:

    猜你喜欢
    • 2023-03-14
    • 2011-09-06
    • 2013-07-23
    • 1970-01-01
    • 2014-10-23
    • 2019-02-17
    • 2015-03-21
    • 2020-12-13
    • 1970-01-01
    相关资源
    最近更新 更多