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

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 org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocument1;


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

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

        // 获取文件输入流
        FileInputStream fileInputStream = getFileInputStream("666.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.addBreak(); // 软回车
                wordOutputParagraphRun.setText(run.getText(run.getCharacterSpacing()));
            }

        }

        // 获取所有表格
        List<XWPFTable> xwpfTables = wordInput.getTables();
        for (XWPFTable xwpfTable : xwpfTables) {
            XWPFTable wordOutputTable = wordOutput.createTable();
            // 获取一个表格中的所有行
            List<XWPFTableRow> xwpfTableRows = xwpfTable.getRows();
            System.out.println("xwpfTableRows个数"+xwpfTableRows.size());
            for (XWPFTableRow xwpfTableRow : xwpfTableRows) {

                XWPFTableRow wordOutputTableRow = wordOutputTable.createRow();
                // 获取一行的所有列
                List<XWPFTableCell> xwpfTableCell = xwpfTableRow.getTableCells();
                System.out.println("xwpfTableCell个数"+xwpfTableCell.size());
                int index = 0;
                for (XWPFTableCell tableCell : xwpfTableCell) {
                    index++;
                    XWPFTableCell wordOutputTableRowCell = wordOutputTableRow.createCell();
                    // 获取单个列
                    //wordOutputTableRowCell.setText("哈哈哈哈~我修改过了");
                    System.out.println(tableCell.getText());
                    wordOutputTableRowCell.setText(tableCell.getText());
                    System.out.println("index:"+index);
                }

            wordOutputTable.removeRow(0);
            }
            //wordOutputTable.removeBorders(); 虚线边框

        }

        CTDocument1 document = wordInput.getDocument();
        System.out.println();


        wordOutput.write(fileOutputStream);
        wordInput.close();
        wordOutput.close();
        inputStream.close();
        fileOutputStream.close();
    }

}


要使用Java POI读取Word文档,您可以按照以下步骤进行操作: 1. 首先,您需要在项目中导入POI的依赖。如果使用Maven进行项目管理,可以在pom.xml文件中添加以下依赖项: ```xml <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> ``` 2. 创建一个`File`对象,指定要读取Word文档的路径。 ```java File file = new File("path/to/your/word/document.docx"); ``` 3. 使用`FileInputStream`读取Word文档。 ```java FileInputStream fis = new FileInputStream(file); ``` 4. 创建一个`XWPFDocument`对象,用于表示Word文档。 ```java XWPFDocument document = new XWPFDocument(fis); ``` 5. 遍历文档中的段落和表格,并提取所需的内容。 ```java List<XWPFParagraph> paragraphs = document.getParagraphs(); for (XWPFParagraph paragraph : paragraphs) { // 处理段落内容 String text = paragraph.getText(); // 进一步处理文本内容... } List<XWPFTable> tables = document.getTables(); for (XWPFTable table : tables) { // 处理表格内容 List<XWPFTableRow> rows = table.getRows(); for (XWPFTableRow row : rows) { // 处理行内容 List<XWPFTableCell> cells = row.getTableCells(); for (XWPFTableCell cell : cells) { // 处理单元格内容 String text = cell.getText(); // 进一步处理文本内容... } } } ``` 6. 最后,记得关闭文件流。 ```java fis.close(); ``` 通过以上步骤,您可以使用Java POI读取Word文档中的内容。请注意,POI库也可以读取旧版的`.doc`格式的Word文档,只需要将上述代码中的`XWPFDocument`替换为`HWPFDocument`即可。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

L-960

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值