使用poi-tl动态写入目录更新问题解决

发布于:2024-10-15 ⋅ 阅读:(124) ⋅ 点赞:(0)

在使用poi-tl动态写完word后,是无法更新目录的,使用poi-tl提供的插件也是不行的,而且很多使用poi手动写入的也是不行,最多就是让你在打开文件时提示你更新目录/更新域,用户体验很差,要点击好几次而且wps还不一定会弹的出来,只有office可以弹框。所以全网搜索最终找到两种解决方案,亲测可用,包含附件破解jar包,当然有商业版的更好

引入破解jar包,并导入项目,在资源里下载
spire.doc.free-5.2.0.jar
aspose-words-20.12-jdk17-crack.jar
在项目里的resources目录下新增lib文件夹将jar放入,打开pom文件夹配置

1,使用spire.doc(耗时长)

        <dependency>
            <groupId>e-iceblue</groupId>
            <artifactId>spire.doc.free</artifactId>
            <version>5.2.0</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/resources/lib/ spire.doc.free-5.2.0.jar</systemPath>
        </dependency>
  public static void main(String[] args) throws Exception {
	   String wordPath = "D:\word\template\test.docx"
       Document dd = new Document(wordPath);
       // 更新目录
       dd.updateTableOfContents();
       // 再保存到原文件或者保存到新文件地址都可以
       dd.saveToFile(wordPath);
}

2,使用aspose.words

        <dependency>
            <groupId>com.aspose</groupId>
            <artifactId>aspose-words</artifactId>
            <version>20.12</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/resources/lib/aspose-words-20.12-jdk17-crack.jar</systemPath>
        </dependency>

  public static void main(String[] args) throws Exception {
	   String wordPath = "D:\word\template\test.docx"
       Document doc = new Document(wordPath );
       // 更新目录
       doc.updateFields();
       // 再保存到原文件或者保存到新文件地址都可以
       doc.save(wordPath);
}
  // aspose 还提供很多转换功能比如转pdf
  public static void main(String[] args) throws Exception {
	   String wordPath = "D:\word\template\test.docx"
	   String pdfPath = "D:\word\template\testPdf.pdf"
	   File file = new File(pdfPath);
       try (FileOutputStream os = new FileOutputStream(file)) {
            Document doc = new Document(pdfPath);
            doc.updateFields();
            doc.save(os, SaveFormat.PDF);
        }   
}

打包会遇到的一点小坑

当使用system引入依赖时,打包的时候要多注意,要加上如下配置,不然springboot是不会这种方式引入的jar进行打包的。

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <!-- 打包时会将本地jar一起打包 -->
            <configuration>
                <includeSystemScope>true</includeSystemScope>
            </configuration>     
        </plugin>
    </plugins>
</build>

参考连接