poi根据标题样式解析word文档(知识点满满)

因为最近接触到一种新的需求,甲方要求根据他们的word文档解析数据并存入数据库,大概一听挺懵的,仔细一想,想到了座右铭:只要能想到哪有苦逼程序员干不到的那
说着就干,开始找了许多方法,因为word文档不如excel好解析,我这就找到了根据标题进行解析的思路。
费话不多说,直接上干货!!!

package com.example.controller;

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

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

public class WordUtils {


    public static void main(String[] args) {
        String filePath = "文件.docx";
        try {
            printword(filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void printword(String filePath) throws IOException {

        XWPFDocument document = new XWPFDocument(POIXMLDocument.openPackage(filePath));
        List<XWPFParagraph> paragraphs = document.getParagraphs();

        //   List<Item> ietmList = new ArrayList<>();
        String title1 = null, title2 = null, title3 = null;
        StringBuffer content = new StringBuffer();
        boolean isParagraph = false;
        int paragraphCount = paragraphs.size();
//        Iterator iter = document.getBodyElementsIterator();
        Iterator iter = document.getBodyElementsIterator();
        int i = -1;
        while (iter.hasNext()) {
            IBodyElement elem = (IBodyElement) iter.next();
//            System.out.println(elem.getClass().getTypeName());
            if (elem instanceof XWPFParagraph) {

//            } else if (elem instanceof XWPFTable) {
//
//            }
//        }
//        for (int i = 0; i < paragraphCount; i++) {
//            XWPFParagraph paragraph = paragraphs.get(i);
                i++;
                XWPFParagraph paragraph = (XWPFParagraph) elem;
                //获取段落中的句列表
                String text = paragraph.getParagraphText();
                String style = paragraph.getStyle();
//            paragraph.getCTP().getPPr().getOutlineLvl()

                if ("2".equals(style)) {
                    //System.out.println("一级标题:==========" + text + ":================" + style);
                    title1 = text;
                    isParagraph = true;
                    content = new StringBuffer();
                    title2 = title3 = null;
                } else if ("3".equals(style)) {
                    // System.out.println("二级标题:==========" + text + ":================" + style);
                    title2 = text;
                    isParagraph = true;
                    content = new StringBuffer();
                    title3 = null;
                } else if ("4".equals(style)) {
                    // System.out.println("三级标题:==========" + text + ":================" + style);
                    title3 = text;
                    isParagraph = true;
                    content = new StringBuffer();
                } else {
                    //System.out.println("三级标题:==========" + paragraph.getText());
                    if (isParagraph) {
                        content = new StringBuffer();
                    }
                    if (title1 == null) {
                        content = null;
                    } else {
                        content.append(paragraph.getText());
                    }
                    isParagraph = false;
//                continue;


                }
                XWPFParagraph nextGaragraph = null;
                if (i + 1 < paragraphCount)
                    nextGaragraph = paragraphs.get(i + 1);
                if (content != null && !content.toString().equals("") && (title3 == null ||
                        (i + 1 == paragraphCount) || (content != null && nextGaragraph != null
                        && nextGaragraph.getStyle() != null && !nextGaragraph.getStyle().equals("0")))) {
                    System.out.println(String.format("%s %s %s %s", title1, title2, title3, content));


                }
                List<XWPFRun> runs = paragraph.getRuns();
                for (XWPFRun run : runs) {
                    List<XWPFPicture> embeddedPictures = run.getEmbeddedPictures();
                    for (int j = 0; j < embeddedPictures.size(); j++) {
                        System.out.println(embeddedPictures.get(j).getPictureData().getData());//包含的图片字节码文件
                    }
                }

            } else if (elem instanceof XWPFTable) {
                System.out.println(((XWPFTable) elem).getText());//解析表格
            }
        }
    }
}

注:目前好像由于版本原因只支持解析.docx的,不是的话建议另存为一下。亲测有效加粗样式

另外 我是根据具体需求具体做了显示判断,你们最后根据自己需求修改一下想要的判断就好。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值