• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Java的POI-word模板生成目录自动更新--完美解决

武飞扬头像
飞翔的史莱克
帮助4

目录问题:

解决word模板目录在第一次打开不更新就不显示目录问题的原因:之前是通过动态替换域代码toc的形式,生成了一段域代码放置在Word的目录行,打开的时候无法直接触发渲染和更新。

方案:通过插入-文档组件-域组件-目录和索引,将当前的模板的目录直接生成到文档的目录中,在数据替换的时候,由于目录用的也是正文的内容,所以直接就替换掉了。

上述方案解决了需要手动更新才能显示,否则空白的问题。但是也存在缺点:只能更新目录的内容,目录的页码无法正确更新显示,是当时模板的页码。

------------------------------------------分界线-------------------------------------------------------------------------

项目时间允许之际,又做了方案调研,用以下方案,完美解决:

1、spire.doc

有收费版本和免费版本,免费的版本只能读取500行的内容,并生成目录,所以不全,不用;

收费版本的生成word后,会在文档第一行显示试用提示,将该行用poi删除即可;

先用poi-tl的模板生成word,然后用spire.doc打开,并更新域,再保存到原文件路径即可,最后将第一行删掉:

  1.  
     
  2.  
    public class Demo4 {
  3.  
    public static void main (String[] args) throws IOException {
  4.  
    //加载已设置大纲级别的测试文档
  5.  
    long start = System.currentTimeMillis();
  6.  
    Document doc = new Document("D:\\project\\util\\src\\main\\resources\\poi\\report.docx");
  7.  
     
  8.  
    doc.updateTableOfContents();
  9.  
     
  10.  
    doc.saveToFile("目录2222311.docx", FileFormat.Docx_2010);
  11.  
    restWord("目录2222311.docx");
  12.  
    System.out.println((System.currentTimeMillis()-start)/1000);
  13.  
    }
  14.  
    private static void restWord(String docFilePath) {
  15.  
    try (FileInputStream in = new FileInputStream(docFilePath)) {
  16.  
    XWPFDocument doc = new XWPFDocument(OPCPackage.open(in));
  17.  
    List<XWPFParagraph> paragraphs = doc.getParagraphs();
  18.  
    if (paragraphs.size() < 1) return;
  19.  
    XWPFParagraph firstParagraph = paragraphs.get(0);
  20.  
    if (firstParagraph.getText().contains("Spire.Doc")) {
  21.  
    doc.removeBodyElement(doc.getPosOfParagraph(firstParagraph));
  22.  
    }
  23.  
    OutputStream out = new FileOutputStream(docFilePath);
  24.  
    doc.write(out);
  25.  
    out.close();
  26.  
    } catch (Exception e) {
  27.  
    e.printStackTrace();
  28.  
    }
  29.  
    }
  30.  
    }
学新通

2、aspose.word

aspose.word只有收费版本,如果公司允许,可以买了再用,没有预算的,可以找一个破解jar包引入即可,如果找不到,可以私信我:

先用poi-tl的模板生成word,然后用aspose.word打开,并更新域,再保存到原文件路径即可:

  1.  
    public class Demo5 {
  2.  
    public static void main (String[] args) throws Exception {
  3.  
    //加载已设置大纲级别的测试文档
  4.  
    long start = System.currentTimeMillis();
  5.  
    Document doc = new Document("D:\\project\\util\\src\\main\\resources\\poi\\report.docx");
  6.  
    doc.updateFields();
  7.  
    doc.save("33333.pdf", SaveFormat.PDF);//这里执行操作
  8.  
     
  9.  
    // restWord("目录33331221.docx");
  10.  
     
  11.  
    System.out.println((System.currentTimeMillis()-start)/1000);
  12.  
    }
  13.  
    private static void restWord(String docFilePath) {
  14.  
    try (FileInputStream in = new FileInputStream(docFilePath)) {
  15.  
    XWPFDocument doc = new XWPFDocument(OPCPackage.open(in));
  16.  
    List<XWPFParagraph> paragraphs = doc.getParagraphs();
  17.  
    if (paragraphs.size() < 1) {
  18.  
    return;
  19.  
    }
  20.  
    XWPFParagraph firstParagraph = paragraphs.get(0);
  21.  
    XWPFParagraph lastParagraph = paragraphs.get(paragraphs.size() - 1);
  22.  
    if (firstParagraph.getText().contains("Aspose.Words")) {
  23.  
    doc.removeBodyElement(doc.getPosOfParagraph(firstParagraph));
  24.  
    doc.removeBodyElement(doc.getPosOfParagraph(lastParagraph));
  25.  
    }
  26.  
    OutputStream out = new FileOutputStream(docFilePath);
  27.  
    doc.write(out);
  28.  
    out.close();
  29.  
    } catch (Exception e) {
  30.  
    e.printStackTrace();
  31.  
    }
  32.  
    }
学新通

总结,公司允许用破解版的,可以用aspose.word,效率高,生成PDF也给力;spire.doc是国内的产品,操作word耗时长,700 页的文档需要130s 的耗时,不能忍。

本篇文章来至:学新通

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通
  • 本文地址: https://www.swvq.com/boutique/detail/tanhcjjfcc
  • 联系方式: luke.wu#vfv.cc
系列文章
更多 icon
同类精品
更多 icon
我要评论
我的头像
精彩评论
继续加载