public static List getWordTitles(String path) throws IOException{
InputStream is = new FileInputStream(path);
List list = new ArrayList();
XWPFDocument doc = new XWPFDocument(is);
List paras = doc.getParagraphs();
for (XWPFParagraph graph : paras) {
String text = graph.getParagraphText();
String style = graph.getStyle();
if ("1".equals(style)) {
System.out.println(text+"--["+style+"]");
}else if ("2".equals(style)) {
System.out.println(text+"--["+style+"]");
}else if ("3".equals(style)) {
System.out.println(text+"--["+style+"]");
}else{
continue;
}
list.add(text);
}
return list;
}
可以获取到标题的文本和标题的级别。