package com.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xwpf.usermodel.BreakType;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import com.util.LogUtil;
public class WordUtil {
private static String regex = "【[^】]*】+";
private static String regexCustom = "【[^】]*】+";
public static String generateWordByTemplate(String basePath,String filePath,
String templateName,Map<String, String> map,String regexCus){
if(StringUtils.isBlank(regexCus)){
regexCustom=regex;
}else{
regexCustom=regexCus;
}
return generateWordByTemplate(basePath, filePath, templateName, map);
}
public static String generateWordByTemplate(String basePath,String filePath,
String templateName,Map<String, String> map){
Pattern pattern = Pattern.compile(regexCustom);
InputStream is=null;
XWPFDocument doc =null;
OutputStream os =null;
try {
is =WordUtil.class.getClassLoader().getResourceAsStream(templateName);
doc = new XWPFDocument(is);
List<XWPFParagraph> list = doc.getParagraphs();
// 替换占位符
for (int i = 0; i < list.size(); i++) {
XWPFParagraph xwpfParagraph = list.get(i);
List<XWPFRun> runs = xwpfParagraph.getRuns();
if (runs != null && runs.size() > 0) {
for (int j = runs.size() - 1; j >= 0; j--) {
String text = runs.get(j).text();
for (Entry<String,String> entry : map.entrySet()) {
text=text.replaceAll(entry.getKey(), entry.getValue());
}
runs.get(j).setText(text, 0);
}
}
}
// 替换表格中的占位符
List<XWPFTable> tables = doc.getTables();
for (XWPFTable xwpfTable : tables) {
for (int i = 0; i < xwpfTable.getNumberOfRows(); i++) {
XWPFTableRow row = xwpfTable.getRow(i);
List<XWPFTableCell> tableCells = row.getTableCells();
for (XWPFTableCell xwpfTableCell : tableCells) {
List<XWPFParagraph> paragraphs = xwpfTableCell.getParagraphs();
for (XWPFParagraph xwpfParagraph : paragraphs) {
List<XWPFRun> runs = xwpfParagraph.getRuns();
for (XWPFRun xwpfRun : runs) {
String trim = xwpfRun.text();
Matcher matcher = pattern.matcher(trim);
String temp = "";
while (matcher.find()) {
temp = matcher.group();
}
if (StringUtils.isNotBlank(temp)) {
String v = map.get(temp);
if(v==null){
trim = trim.replace(temp, "");
}else{
trim = trim.replace(temp, map.get(temp));
}
}
String[] split = trim.split("\n");
for (int j = 0; j < split.length; j++) {
xwpfRun.setText(split[j],j);
if(j!=split.length-1){
xwpfRun.addBreak(BreakType.TEXT_WRAPPING);//换行
}
}
}
}
}
}
}
File file = new File(basePath+"/"+filePath);
if(!file.getParentFile().exists())
file.getParentFile().mkdirs();
os = new FileOutputStream(file);
doc.write(os);
} catch (Exception e) {
LogUtil.error(e);
}finally{
if(os!=null){
try {
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(doc!=null){
try {
doc.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return "/"+filePath;
}
public static void main(String[] args) throws IOException {
Pattern pattern = Pattern.compile(regexCustom);
Map<String, String> map = new HashMap<String, String>();
map.put("qbdj", "你好");
map.put("zlhtbt", "姓名");
map.put("fwqh", "性别");
map.put("qbly", "年龄");
map.put("lrsj", "生日");
map.put("bt", "职业");
map.put("xxzw", "工作城市");
map.put("bb", "武汉光谷");
map.put("ss", "武汉光谷");
map.put("ff", "武汉光谷");
map.put("gg", "武汉光谷");
map.put("hh", "武汉光谷");
map.put("dd", "武汉光谷");
String filePath = "D:\\report.docx";
InputStream is = new FileInputStream(filePath);
@SuppressWarnings("resource")
XWPFDocument doc = new XWPFDocument(is);
List<XWPFParagraph> list = doc.getParagraphs();
// 替换占位符
for (int i = 0; i < list.size(); i++) {
XWPFParagraph xwpfParagraph = list.get(i);
List<XWPFRun> runs = xwpfParagraph.getRuns();
if (runs != null && runs.size() > 0) {
for (int j = runs.size() - 1; j >= 0; j--) {
if (map.containsKey(runs.get(j).text())) {
System.out.println("run:" + runs.get(j).text());
runs.get(j).setText(map.get(runs.get(j).text()), 0);
}
}
}
}
// 替换表格中的占位符
List<XWPFTable> tables = doc.getTables();
for (XWPFTable xwpfTable : tables) {
for (int i = 0; i < xwpfTable.getNumberOfRows(); i++) {
XWPFTableRow row = xwpfTable.getRow(i);
List<XWPFTableCell> tableCells = row.getTableCells();
for (XWPFTableCell xwpfTableCell : tableCells) {
List<XWPFParagraph> paragraphs = xwpfTableCell.getParagraphs();
for (XWPFParagraph xwpfParagraph : paragraphs) {
List<XWPFRun> runs = xwpfParagraph.getRuns();
for (XWPFRun xwpfRun : runs) {
String trim = xwpfRun.text();
Matcher matcher = pattern.matcher(trim);
String temp = "";
while (matcher.find()) {
temp = matcher.group();
}
if (StringUtils.isNotBlank(temp)) {
trim = trim.replace(temp, map.get(temp));
}
xwpfRun.setText(trim, 0);
}
}
}
}
}
OutputStream os = new FileOutputStream(
"D:\\report2.docx");
doc.write(os);
os.close();
is.close();
}
}