java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > java poi之XWPFDocument读取word内容并创建新的word

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

作者:L-960

这篇文章主要介绍了java poi之XWPFDocument如何读取word内容并创建新的word问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

Poi的Word文档结构介绍

1、poi之word文档结构介绍之正文段落

一个文档包含多个段落,一个段落包含多个Runs,一个Runs包含多个Run,Run是文档的最小单元

2、poi之word文档结构介绍之正文表格

一个文档包含多个表格,一个表格包含多行,一行包含多列(格),每一格的内容相当于一个完整的文档

之后和正文段落一样

注:

3、poi之word文档结构介绍之页眉

一个文档可以有多个页眉(不知道怎么会有多个页眉。。。),页眉里面可以包含段落和表格

之后就一样了

4、poi之word文档结构介绍之页脚

页脚和页眉基本类似,可以获取表示页数的角标

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>

代码

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();
    }

}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文