ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java poi之XWPFDocument读取word内容并创建新的word

2021-04-26 18:31:29  阅读:2934  来源: 互联网

标签:段落 word 表格 List XWPFDocument 文档 poi 页眉


参考:https://www.cnblogs.com/unruly/p/7479518.html
Poi的Word文档结构介绍

1、poi之word文档结构介绍之正文段落
一个文档包含多个段落,一个段落包含多个Runs,一个Runs包含多个Run,Run是文档的最小单元
获取所有段落:List paragraphs = word.getParagraphs();
获取一个段落中的所有Runs:List xwpfRuns = xwpfParagraph.getRuns();
获取一个Runs中的一个Run:XWPFRun run = xwpfRuns.get(index);

2、poi之word文档结构介绍之正文表格
一个文档包含多个表格,一个表格包含多行,一行包含多列(格),每一格的内容相当于一个完整的文档
获取所有表格:List xwpfTables = doc.getTables();
获取一个表格中的所有行:List xwpfTableRows = xwpfTable.getRows();
获取一行中的所有列:List xwpfTableCells = xwpfTableRow.getTableCells();
获取一格里的内容:List paragraphs = xwpfTableCell.getParagraphs();
之后和正文段落一样

注:
表格的一格相当于一个完整的docx文档,只是没有页眉和页脚。里面可以有表格,使用xwpfTableCell.getTables()获取,and so on
在poi文档中段落和表格是完全分开的,如果在两个段落中有一个表格,在poi中是没办法确定表格在段落中间的。(当然除非你本来知道了,这句是废话)。只有文档的格式固定,才能正确的得到文档的结构

3、poi之word文档结构介绍之页眉:
一个文档可以有多个页眉(不知道怎么会有多个页眉。。。),页眉里面可以包含段落和表格
获取文档的页眉:List headerList = doc.getHeaderList();
获取页眉里的所有段落:List paras = header.getParagraphs();
获取页眉里的所有表格:List tables = header.getTables();
之后就一样了

4、poi之word文档结构介绍之页脚:
页脚和页眉基本类似,可以获取表示页数的角标

  • IBodyElement -------------------迭代器(段落和表格)
  • XWPFComment -------------------评论(个人理解应该是批注)
  • XWPFSDT
  • XWPFFooter -------------------页脚
  • XWPFFootnotes -------------------脚注
  • XWPFHeader -------------------页眉
  • XWPFHyperlink -------------------超链接
  • XWPFNumbering -------------------编号
  • XWPFParagraph -------------------段落
  • XWPFPictureData -------------------图片
  • XWPFStyles -------------------样式(设置多级标题的时候用)
  • XWPFTable -------------------表格

pom依赖

<dependencies>
       <!--解析doc文档HWPFDocument-->
       <dependency>
           <groupId>org.apache.poi</groupId>
           <artifactId>poi-scratchpad</artifactId>
           <version>4.1.2</version>
       </dependency>
       <dependency>
           <groupId>org.apache.poi</groupId>
           <artifactId>poi</artifactId>
           <version>4.1.2</version>
       </dependency>
       <dependency>
           <groupId>org.apache.poi</groupId>
           <artifactId>poi-ooxml</artifactId>
           <version>4.1.2</version>
       </dependency>

       <dependency>
           <groupId>springframework</groupId>
           <artifactId>spring-core</artifactId>
           <version>1.2.6</version>
       </dependency>
   </dependencies>

代码

maven项目

目录树


import org.apache.poi.xwpf.usermodel.*;


import java.io.*;
import java.util.List;

public class poi3 {
    public static void main(String[] args) throws IOException {

        // 获取文件输入流
        FileInputStream fileInputStream = getFileInputStream("测试文档.docx");

        dealDocx(fileInputStream, "副本.docx");

    }

    private static FileInputStream getFileInputStream(String name) throws FileNotFoundException {
        String dir = poi3.class.getResource("").getPath() + name;
        FileInputStream fileInputStream = new FileInputStream(dir);
        return fileInputStream;
    }

    private static void dealDocx(InputStream inputStream, String newFileName) throws IOException {
        // 创建输出文件
        File file = new File(poi3.class.getResource("").getPath() + newFileName);

        // 获取文件输出流
        FileOutputStream fileOutputStream = new FileOutputStream(file);

        // 创建操作word的对象
        XWPFDocument wordInput = new XWPFDocument(inputStream);
        XWPFDocument wordOutput = new XWPFDocument();

        // 获取所有段落
        List<XWPFParagraph> xwpfParagraphs = wordInput.getParagraphs();

        // 迭代每一个段落
        for (XWPFParagraph xwpfParagraph : xwpfParagraphs) {

            // 原文档有多少个段落 我就创建多少个
            XWPFParagraph wordOutputParagraph = wordOutput.createParagraph();

            // 获取当前段落的所有run
            List<XWPFRun> runs = xwpfParagraph.getRuns();

            for (XWPFRun run : runs) {
                XWPFRun wordOutputParagraphRun = wordOutputParagraph.createRun();
                // 赋值
                //wordOutputParagraphRun.setText("哈哈哈哈~我修改过了");
                // 添加回车
                //wordOutputParagraphRun.addCarriageReturn();
                wordOutputParagraphRun.setText(run.getText(run.getCharacterSpacing()));
            }

        }

        // 获取所有表格
        List<XWPFTable> xwpfTables = wordInput.getTables();
        for (XWPFTable xwpfTable : xwpfTables) {
            XWPFTable wordOutputTable = wordOutput.createTable();
            // 获取一个表格中的所有行
            List<XWPFTableRow> xwpfTableRows = xwpfTable.getRows();
            for (XWPFTableRow xwpfTableRow : xwpfTableRows) {
                XWPFTableRow wordOutputTableRow = wordOutputTable.createRow();
                // 获取一行的所有列
                List<XWPFTableCell> xwpfTableCell = xwpfTableRow.getTableCells();
                for (XWPFTableCell tableCell : xwpfTableCell) {
                    XWPFTableCell wordOutputTableRowCell = wordOutputTableRow.createCell();
                    // 获取单个列
                    //wordOutputTableRowCell.setText("哈哈哈哈~我修改过了");
                    wordOutputTableRowCell.setText(tableCell.getText());
                }
            }
            wordOutputTable.removeRow(0);
        }
        wordOutput.write(fileOutputStream);
        wordInput.close();
        wordOutput.close();
        inputStream.close();
        fileOutputStream.close();
    }

}

标签:段落,word,表格,List,XWPFDocument,文档,poi,页眉
来源: https://blog.csdn.net/weixin_43702146/article/details/116159448

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有