文本处理之word

参考资料:

最简单易用的word处理----spire

最全面的word处理----poi

poi介绍文档

参考demo 


其他的一些参考资料:

POI按照Word文档指定标题进行拆分
XWPFDocument在word文档最上边加一个段落

概述:

  • POI是操作word最强大的API,但是学习成本高,且很难找到系统的学习资料
  • spire也拥有强大的操作word的能力,能满足日常的基本开发需求,但是他是按照节(Section)的概念来进行分的,在有表格和图文混在一起时,无法找到他们的相对顺序,所以需要poi的辅助
  • 下面是对上述demo的示例进行讲解

文本处理之word_第1张图片

  •  其中poi文件夹下的遍历word元素(WordIterate),或者ReportUtil中的getTables可以用来帮助spire在表格和图文混排的时候,确定表格的相对位置

一些代码:

下面给出的例子在demo中,特意拿出来方便使用

  • 编辑文档序号
package com.example.worddemo.test;

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.collections.SectionCollection;
import com.spire.doc.documents.ListPatternType;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.formatting.ListFormat;

import java.util.Objects;

/**
 * @program: word-demo
 * @description:   用于编辑word的序号
 * @author: wjl
 * @create: 2023-05-12 16:59
 **/
public class WordOrder {

    //文件路径
    private static String FILE_PATH_IN = "";
    private static String FILE_PATH_OUT = "";

    static {
        // 获取本地路径 并且 初始化
        FILE_PATH_IN = System.getProperty("user.dir") + "\\src\\main\\resources\\doc\\";
        FILE_PATH_OUT = System.getProperty("user.dir") + "\\src\\main\\resources\\outputdoc\\";
    }

    /**
     * 参考网址
     * https://jingyan.baidu.com/article/3065b3b624850efecff8a4aa.html
     * https://jiuaidu.com/jianzhan/1127614/
     * @param args
     */
    public static void main(String[] args) {

        //创建 Document 类的对象
        Document document = new Document();

        //从文件加载 Word 文档
        document.loadFromFile("E:\\temp\\1.docx");

        //获取文档第一节
        SectionCollection sections = document.getSections();
        int count = sections.getCount();

        //在第4到6段中循环
/*        for(int i = 3; i <= 5; i++){
            Paragraph para = section.getParagraphs().get(i);
            ListFormat listFormat = para.getListFormat();
            listFormat.applyStyle("ssss");
            //应用项目符号列表
            listFormat.applyBulletStyle();
            listFormat.setListLevelNumber(1);//设置列表级别(不设置列表级别时,默认级别为1级列表)
            //设置列表横向位置
            //设置目录为中文简体
            listFormat.getCurrentListLevel().setPatternType(ListPatternType.Chinese_Counting_Thousand);
            listFormat.getCurrentListLevel().setNumberPosition(-10);
        }*/

        for (int num = 0; num < count; num++) {
            Section section = sections.get(num);
            for(int i = 0; i 

你可能感兴趣的:(word)