读取word,根据标题来区分标题1标题2还是正文,然后分别存,采用的是边读边存的形式
package com.zhonghui.taxdoc.webapp.facade;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFStyle;
import org.apache.poi.xwpf.usermodel.XWPFStyles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;import com.zhonghui.taxdoc.webapp.po.DocContentWithBLOBs;
import com.zhonghui.taxdoc.webapp.po.DocWord;
import com.zhonghui.taxdoc.webapp.service.DocWordService;
import com.zhonghui.taxdoc.webapp.utils.ChineseToEnglish;
import com.zhonghui.taxdoc.webapp.vo.UserVo;@Service
public class DocWordFacade {@Autowired
private DocWordService docWordService;Map<Integer, Long> map = new HashMap<>();
@Transactional(rollbackFor = Exception.class)
public boolean readDocx(File file, UserVo user, String fileName, int type) {
Long id = 0L;
Long cpid = 0L;
boolean flag = true;
String content = "";
InputStream in = null;
XWPFParagraph para = null;
try {
// 主表
fileName = StringUtils.substringBeforeLast(fileName, ".");
String nameSpell = ChineseToEnglish.getPingYin(fileName);
DocWord word = new DocWord();
word.setDocName(fileName);
word.setUserId(user.getId());
word.setUserName(user.getRealname());
word.setStatus(1);
word.setSpell(nameSpell);
word.setType(type);
Long wordId = docWordService.insertWord(word);
in = new FileInputStream(file);
XWPFDocument doc = new XWPFDocument(in);
XWPFStyles style = doc.getStyles();
Iterator<XWPFParagraph> iterator = doc.getParagraphsIterator();
// initMap();
while (iterator.hasNext()) {
para = iterator.next();
String pstyleId = para.getStyleID();
if (pstyleId != null) {
XWPFStyle xstyle = style.getStyle(pstyleId);
String styleName = xstyle.getName();
boolean isTitle = styleName.indexOf("1") >= 0 || styleName.indexOf("2") >= 0
|| styleName.indexOf("3") >= 0 || styleName.indexOf("4") >= 0;
if (flag == false && isTitle == true) {
DocContentWithBLOBs w = new DocContentWithBLOBs();
w.setWordId(wordId);
w.setContent(content);
w.setParentId(cpid);
w.setType(2);
docWordService.insertContent(w);
content = "";
}
if (styleName.indexOf("1") >= 0) {
DocContentWithBLOBs w = new DocContentWithBLOBs();
w.setWordId(wordId);
w.setTitle(para.getText());
w.setParentId(0L);
w.setType(1);
id = docWordService.insertContent(w);
initMap();
map.put(1, id);
} else if (styleName.indexOf("2") >= 0) {
DocContentWithBLOBs w = new DocContentWithBLOBs();
w.setWordId(wordId);
w.setTitle(para.getText());
w.setParentId(map.get(1));
w.setType(1);
id = docWordService.insertContent(w);
map.put(2, id);
} else if (styleName.indexOf("3") >= 0) {
DocContentWithBLOBs w = new DocContentWithBLOBs();
w.setWordId(wordId);
w.setType(1);
w.setTitle(para.getText());
if (!map.get(2).equals(0L)) {
w.setParentId(map.get(2));
} else {
w.setParentId(map.get(1));
}
id = docWordService.insertContent(w);
map.put(3, id);
} else if (styleName.indexOf("4") >= 0) {
DocContentWithBLOBs w = new DocContentWithBLOBs();
w.setWordId(wordId);
w.setType(1);
w.setTitle(para.getText());
if (!map.get(3).equals(0L)) {
w.setParentId(map.get(3));
} else if (!map.get(2).equals(0L)) {
w.setParentId(map.get(2));
} else {
w.setParentId(map.get(1));
}
id = docWordService.insertContent(w);
map.put(4, id);
} else {
flag = false;
cpid = id;
content = content + "\r\n" + para.getText().replaceAll("\n", "\r\n");
}} else {
flag = false;
cpid = id;
content = content + "\r\n" + para.getText().replaceAll("\n", "\r\n");
}
}
if (flag == false) {
DocContentWithBLOBs w = new DocContentWithBLOBs();
w.setWordId(wordId);
w.setContent(content);
w.setParentId(cpid);
w.setType(2);
docWordService.insertContent(w);
content = "";
}
} catch (Exception e) {
e.printStackTrace();
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
return false;
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}public void initMap() {
map.put(0, 0L);
map.put(1, 0L);
map.put(2, 0L);
map.put(3, 0L);
map.put(4, 0L);
}}