开发者

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

目录
  • Poi的Word文档结构介绍
    • 1、poi之word文档结构介绍之正文段落
    • 2、poi之word文档结构介绍之正文表格
    • 3、poi之word文档结构介绍之页眉
    • 4、poi之word文档结构介绍之页脚
    • pom依赖
    • 代码
  • 总结

    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>LcGxrVQQHuspringframework</groupId>
               <artifactId>spring-core</artifactId>
               <version>1.2.6</version>
           </dependency>
       </dependencies>

    代码

    • maven项目

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

    import org.apache.poi.xwpf.usermodel.*;
    import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocument1;
    
    
    import Java.io.*;
    import java.util.List;
    
    public class popythoni3 {
        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 = xw编程客栈pfParagraph.getRuns();
    
                for (XWPFRun run : runs) {
                    XWPFRun wordOutputParagraphRun = wordOutputParagraph.createRun();
                    // 赋值
                    //wordOutputParagraphRun.setText("哈哈哈哈~我修改过了");
               android     // 添加回车 硬回车
                    //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();
        }
    
    }

    总结

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程客栈(www.devze.com)。

    0

    上一篇:

    下一篇:

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    最新开发

    开发排行榜