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.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;
import org.apache.poi.POIXMLDocument;
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;
public class RenderDocx {
public static XWPFDocument changWord(String inputUrl, Map<String, Object> textMap) {;
XWPFDocument document = null;
try {
document = new XWPFDocument(POIXMLDocument.openPackage(inputUrl));
renderText(document, textMap);
renderTable(document, textMap);
} catch (IOException e) {
e.printStackTrace();
}
return document;
}
private static void renderTable(XWPFDocument document, Map<String, Object> textMap) {
List<XWPFTable> tables = document.getTables();
for (int i = 0; i < tables.size(); i++) {
XWPFTable table = tables.get(i);
if(checkText(table.getText())){
List<XWPFTableRow> rows = table.getRows();
eachTable(document,rows, textMap);
}
}
}
private static void eachTable(XWPFDocument document, List<XWPFTableRow> rows, Map<String, Object> textMap) {
for (XWPFTableRow row : rows) {
List<XWPFTableCell> cells = row.getTableCells();
for (XWPFTableCell cell : cells) {
if(checkText(cell.getText())){
List<XWPFParagraph> paragraphs = cell.getParagraphs();
for (XWPFParagraph paragraph : paragraphs) {
List<XWPFRun> runs = paragraph.getRuns();
for (XWPFRun run : runs) {
Object ob = changeValue(run.toString(), textMap);
if (ob instanceof String || ob instanceof Long || ob instanceof BigDecimal || ob instanceof Double || ob instanceof Integer){
run.setText(ob.toString(),0);
}
}
}
}
}
}
}
private static void renderText(XWPFDocument document, Map<String, Object> textMap) {
List<XWPFParagraph> paragraphs = document.getParagraphs();
for (XWPFParagraph paragraph : paragraphs) {
String text = paragraph.getText();
if(checkText(text)){
List<XWPFRun> runs = paragraph.getRuns();
for (XWPFRun run : runs) {
if(run.toString().contains("$") || run.toString().contains("{") || run.toString().contains("}")){
Object ob = changeValue(run.toString(), textMap);
if (ob instanceof String || ob instanceof Long || ob instanceof BigDecimal || ob instanceof Double || ob instanceof Integer){
run.setText(ob.toString(),0);
}
}
}
}
}
}
private static Object changeValue(String value, Map<String, Object> textMap) {
Set<Entry<String, Object>> textSets = textMap.entrySet();
Object valu = "";
for (Entry<String, Object> textSet : textSets) {
String key = textSet.getKey();
if(value.indexOf(key)!= -1){
valu = textSet.getValue();
}
}
return valu;
}
private static boolean checkText(String text) {
boolean check = false;
if(text.indexOf("$")!= -1){
check = true;
}
return check;
}
public static void main(String[] args) {
}
public void downloadFile(String fileUrl, HttpServletResponse response, String name) {
OutputStream out = null;
InputStream inputStream = null;
try {
File file = new File(fileUrl);
inputStream = new FileInputStream(file);
response.reset();
response.setContentType("application/octet-stream; charset=utf-8");
response.setHeader("Content-Disposition",
"attachment; filename=" + new String(name.getBytes("GBK"), "ISO8859_1"));
out = response.getOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
file.delete();
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
} finally {
try {
inputStream.close();
out.close();
} catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
}
}
}