Java 读取 Word 文本中的标题

1660阅读 0评论2021-04-26 fhadmin
分类:Java

概述

Word中的标题可通过“样式”中的选项来快速设置(如图1),

图1

在添加目录时,可将“有效样式”设置为“目录级别”显示(如图2),一定程度上来说,标题大纲也可以作为目录来参考。

图2

本文,将通过后端Java程序代码介绍如何来获取Word中的标题内容。

注:如果在Word中进行了如图2中的设置,此方法也可以作为获取目录的方法;若没有对应设置,则想要通过此方法来获取的目录内容可能不完整。

本次测试的Word文档如下图所示,【标题样式】和【目录级别】经过相应设置,在获取标题大纲时等同于获取目录:

图3

代码测试环境

Java 程序代码


点击(此处)折叠或打开

  1. import com.spire.doc.*;
  2. import com.spire.doc.documents.Paragraph;
  3. import java.io.BufferedWriter;
  4. import java.io.File;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. //java 项目 www fhadmin org
  8. public class GetTitle {
  9.     public static void main(String[] args)throws IOException {
  10.         //加载Word测试文档
  11.         Document doc = new Document();
  12.         doc.loadFromFile("sample.docx");
  13.         //保存标题内容到.txt文档
  14.         File file = new File("GetTitle.txt");
  15.         if (file.exists())
  16.         {
  17.             file.delete();
  18.         }
  19.         file.createNewFile();
  20.         FileWriter fw = new FileWriter(file, true);
  21.         BufferedWriter bw = new BufferedWriter(fw);
  22.         //遍历section
  23.         for (int i = 0; i < doc.getSections().getCount(); i++)
  24.         {
  25.             Section section = doc.getSections().get(i);
  26.             //遍历Paragraph
  27.             for (int j = 0; j < section.getParagraphs().getCount(); j++)
  28.             {
  29.                 Paragraph paragraph = section.getParagraphs().get(j);
  30.                 //获取标题
  31.                 if ( paragraph.getStyleName().matches("1"))//段落为“标题1”的内容
  32.                 {
  33.                     //获取段落标题内容
  34.                     String text = paragraph.getText();
  35.                     //写入文本到txt文档
  36.                     bw.write("标题1: "+ text + "\r");
  37.                }
  38.                 //获取标题
  39.                 if ( paragraph.getStyleName().matches("2"))//段落为“标题2”的内容
  40.                 {
  41.                     //获取段落标题内容
  42.                     String text = paragraph.getText();
  43.                     //写入文本到txt文档
  44.                     bw.write("标题2: " + text + "\r");
  45.                 }
  46.                 //获取标题
  47.                 if ( paragraph.getStyleName().matches("3"))//段落为“标题3”的内容
  48.                 {
  49.                     //获取段落标题内容
  50.                     String text = paragraph.getText();
  51.                     //写入文本到txt文档
  52.                     bw.write("标题3: " + text+"\r");
  53.                 }
  54.                 //获取标题
  55.                 if ( paragraph.getStyleName().matches("4"))//段落为“标题4”的内容
  56.                 {
  57.                     //获取段落标题内容
  58.                     String text = paragraph.getText();
  59.                     //写入文本到txt文档
  60.                     bw.write("标题4: " + text+"\r");
  61.                 }
  62.                 bw.write("\n");
  63.             }
  64.         }
  65.         bw.flush();
  66.         bw.close();
  67.         fw.close();
  68.     }
  69. }

标题(目录)获取结果:

上一篇:soringboot ehcache 配置和用法
下一篇:网关 zuul 与 spring-cloud gateway的区别