因为最近接触到一种新的需求,甲方要求根据他们的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的,不是的话建议另存为一下。亲测有效加粗样式
另外 我是根据具体需求具体做了显示判断,你们最后根据自己需求修改一下想要的判断就好。