/**
* poi操作文档(2007+)替换书签
*/
public static Boolean poiOPDocx(String inputFile, String outPutFile, Map<String, String> bookmarkMap,
List<BookMarkTable> bookMarkTableList) {
InputStream is = null;
FileOutputStream fs = null;
try {
File file = new File(inputFile);
if(!file.exists()){
logger.info("文件不存在");
return false;
}
XWPFDocument doc;
try{
is = new FileInputStream(file);
doc = new XWPFDocument(is);
}catch (Exception e){
logger.info(" 文件非docx类型");
return false;
}
List<XWPFParagraph> paragraphs = doc.getParagraphs();
List<XWPFTable> tables = doc.getTables();
if(org.apache.commons.collections4.CollectionUtils.isNotEmpty(paragraphs)){
replaceInParagraphs(bookmarkMap, paragraphs);
}
if(org.apache.commons.collections4.CollectionUtils.isNotEmpty(tables)){
replaceInTables(bookmarkMap, tables);
}
if(org.apache.commons.collections4.CollectionUtils.isNotEmpty(bookMarkTableList)){
insertTable(doc, bookMarkTableList);
}
fs = new FileOutputStream(new File(outPutFile));
doc.write(fs);
}catch (Exception e){
logger.error("poiOPDocx err", e);
return false;
}finally {
try{
is.close();
}catch (Exception e){
}
try{
fs.close();
}catch (Exception e){
}
}
return true;
}
public static void insertTable(XWPFDocument doc, List<BookMarkTable> bookMarkTableList) throws Exception {
try {
if(org.apache.commons.collections4.CollectionUtils.isEmpty(bookMarkTableList)){
return;
}
for(BookMarkTable bookMarkTable : bookMarkTableList){
String bookMark = bookMarkTable.getBookMarkName();
List<String> title = bookMarkTable.getTitles();
List<List<String>> valueList = bookMarkTable.getRowValueList();
List<XWPFParagraph> paragraphList = doc.getParagraphs();
if (paragraphList != null && paragraphList.size() > 0) {
for (XWPFParagraph paragraph : paragraphList) {
List<XWPFRun> runs = paragraph.getRuns();
for (int i = 0; i < runs.size(); i++) {
if(runs.get(i) == null || runs.get(i).getText(0) == null){
continue;
}
String text = runs.get(i).getText(0).trim();
if (text != null) {
if (text.indexOf(bookMark) >= 0) {
runs.get(i).setText(text.replace(bookMark, ""), 0);
XmlCursor cursor = paragraph.getCTP().newCursor();
// 在指定游标位置插入表格
XWPFTable table = doc.insertNewTbl(cursor);
CTTblPr tablePr = table.getCTTbl().getTblPr();
CTTblWidth width = tablePr.addNewTblW();
width.setW(BigInteger.valueOf(8500));
inserInfo(table, title, valueList);
break;
}
}
}
}
}
}
} catch (Exception e) {
logger.error("insertTable err", e);
}
}
/**
* 替换段落中的文本
* @param replacements
* @param paragraphs
* @return
*/
private static long replaceInParagraphs(Map<String, String> replacements, List<XWPFParagraph> paragraphs) {
long count = 0;
for (XWPFParagraph paragraph : paragraphs) {
List<XWPFRun> runs = paragraph.getRuns();
for (Map.Entry<String, String> replPair : replacements.entrySet()) {
String oldText = replPair.getKey();
String newText = replPair.getValue();
// 获取文本段
TextSegment tSegement = paragraph.searchText(oldText, new PositionInParagraph());
if (tSegement != null) {
int beginRun = tSegement.getBeginRun();
int endRun = tSegement.getEndRun();
System.out.println(beginRun + " " + endRun);
count++;
if (beginRun == endRun) {
// whole search string is in one Run
XWPFRun run = runs.get(beginRun);
String runText = run.getText(0);
System.out.println("runText:" + runText);
String replaced = runText.replace(oldText, newText);
run.setText(replaced, 0);
} else {
// The search string spans over more than one Run
// Put the Strings together
StringBuilder b = new StringBuilder();
for (int runPos = beginRun; runPos <= endRun; runPos++) {
XWPFRun run = runs.get(runPos);
b.append(run.getText(0));
}
String connectedRuns = b.toString();
String replaced = connectedRuns.replace(oldText, newText);
// The first Run receives the replaced String of all
// connected Runs
XWPFRun partOne = runs.get(beginRun);
partOne.setText(replaced, 0);
// Removing the text in the other Runs.
for (int runPos = beginRun + 1; runPos <= endRun; runPos++) {
XWPFRun partNext = runs.get(runPos);
partNext.setText("", 0);
}
}
}
}
}
return count;
}
/**
* 替换表格中的文本
*/
private static long replaceInTables(Map<String, String> replacements, List<XWPFTable> tables) {
long count = 0;
for (XWPFTable table : tables) {
for (XWPFTableRow row : table.getRows()) {
for (XWPFTableCell cell : row.getTableCells()) {
List<XWPFParagraph> paragraphs = cell.getParagraphs();
count += replaceInParagraphs(replacements, paragraphs);
}
}
}
return count;
}