前端html内容
<button class="layui-btn xiaMore">下载模板说明</button>
前端jquery内容
$(".xiaMore").click(function () {
$.ajax({
type:"get",
url:'${pageContext.request.contextPath }/headword/newfilesm',
dataType:"json",
success:function(res){
if(res.code==6)
{
layer.confirm('请问是否确定下载?', {
btn: ['下载', '取消']
}, function(index, layero){
window.location.href="http://localhost:8080"+res.fileUrl;
layer.close(index);
}, function(index){
});
}
else if(res.code==5){
layer.msg('模板说明下载失败',{icon:5,time:1000},function(){
});
}
},
error:function(){
layer.msg('下载模板说明出现异常');
}
});
});
后台下载功能接口内容
@ResponseBody
@RequestMapping(value="/newfilesm")
public JSONObject newfilesm(HttpServletRequest request)
{
String fileoriname = request.getSession().getServletContext().getRealPath("") + "/statics/uploadfiles/explay.docx";
System.out.println("fileoriname====="+fileoriname);
String filenowname=request.getSession().getServletContext().getRealPath("")+"/statics/uploadfiles/moban.docx";
JSONObject jsonObject=new JSONObject();
boolean blu= WordUtil.generateWord(null, fileoriname, filenowname);
if(blu)
{
jsonObject.put("msg", "下载模板说明成功");
jsonObject.put("code", 6);
jsonObject.put("fileUrl", "/record/statics/uploadfiles/moban.docx");
}
else
{
jsonObject.put("msg", "没有读到文件");
jsonObject.put("code", 5);
}
return jsonObject;
}
插件 WordUtil类 功能接口内容
package com.tools;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
public class WordUtil {
public static boolean generateWord(Map<String, Object> param, String fileSrc, String fileDest) {
XWPFDocument doc = null;
OPCPackage pack = null;
try {
pack = POIXMLDocument.openPackage(fileSrc);
doc = new XWPFDocument(pack);
if (param != null && param.size() > 0) {
List<XWPFParagraph> paragraphList = doc.getParagraphs();
processParagraphs(paragraphList, param, doc);
}
} catch (Exception e) {
e.printStackTrace();
}
FileOutputStream fopts = null;
try {
fopts = new FileOutputStream(fileDest);
doc.write(fopts);
pack.close();
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
closeStream(fopts);
}
return true;
}
public static void closeStream(FileOutputStream fopts) {
try {
fopts.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void processParagraphs(List<XWPFParagraph> paragraphList, Map<String, Object> param, XWPFDocument doc)
throws InvalidFormatException, FileNotFoundException {
if (paragraphList != null && paragraphList.size() > 0) {
for (XWPFParagraph paragraph : paragraphList) {
List<XWPFRun> runs = paragraph.getRuns();
for (XWPFRun run : runs) {
String text = run.getText(0);
System.out.println("text=========" + text);
if (text != null) {
boolean isSetText = false;
for (Entry<String, Object> entry : param.entrySet()) {
String key = entry.getKey();
if (text.indexOf(key) != -1) {
isSetText = true;
Object value = entry.getValue();
if (value instanceof String) {
System.out.println("key==" + key);
if (text.contains(key)) {
String[] newtext = value.toString().split("\n");
if(newtext.length>0) {
for (int f = 0; f < newtext.length; f++) {
if (f == 0) {
run.setText(newtext[f].trim());
} else {
run.addBreak();
run.setText(newtext[f].trim());
}
}
text = text.replace(key, "");
}else {
text = text.replace(key, value.toString());
}
}
}
}
}
if (isSetText) {
run.setText(text, 0);
}
}
}
}
}
}
}