解析word(docx格式的)获取数据存数据库 读取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.apachemons.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 ansaction.annotation.Transactional;
import ansaction.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;
replaceall()XWPFParagraph para = null;
try {
// 主表
fileName = StringUtils.substringBeforeLast(fileName, ".");
String nameSpell = PingYin(fileName);
DocWord word = new DocWord();
word.setDocName(fileName);
word.Id());
word.Realname());
word.setStatus(1);
word.setSpell(nameSpell);
word.setSpell(nameSpell);
word.setType(type);
Long wordId = docWordService.insertWord(word);
in = new FileInputStream(file);
XWPFDocument doc = new XWPFDocument(in);
XWPFStyles style = Styles();
Iterator<XWPFParagraph> iterator = ParagraphsIterator();
// initMap();
while (iterator.hasNext()) {
para = ();
String pstyleId = StyleID();
if (pstyleId != null) {
XWPFStyle xstyle = Style(pstyleId);
String styleName = Name();
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.Text());
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.Text());
w.(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.Text());
if (!(2).equals(0L)) {
w.(2));
} else {
w.(1));
}
id = docWordService.insertContent(w);
map.put(3, id);
} else if (styleName.indexOf("4") >= 0) {
DocContentWithBLOBs w = new DocContentWithBLOBs();
DocContentWithBLOBs w = new DocContentWithBLOBs();
w.setWordId(wordId);
w.setType(1);
w.Text());
if (!(3).equals(0L)) {
w.(3));
} else if (!(2).equals(0L)) {
w.(2));
} else {
w.(1));
}
id = docWordService.insertContent(w);
map.put(4, id);
} else {
flag = false;
cpid = id;
content = content + "\r\n" + Text().replaceAll("\n", "\r\n");                    }
} else {
flag = false;
cpid = id;
content = content + "\r\n" + Text().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);
}
} }

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。