First you have to make a word template, turn into xml, and organized into a style he wants to add into the project
Then the code is as follows:
Reception
function createWord() {
debugger
var form = $("<form></form>").attr("action", "CreateWordRrport.action").attr("method", "post");
//创建form
form.append($("<input></input>").attr("type", "hidden").attr("name", "downloadType").attr("value", "简报管理"));
for(var item in sendData){
form.append($("<input></input>").attr("type", "hidden").attr("name", item).attr("value", sendData[item]));
}
form.appendTo('body').submit().remove();
}
Which is sendData background Getaction returned data assigned to sendData
Output word background code
private String filePath; //文件路径
private String fileName; //文件名称
private String fileOnlyName; //文件唯一名称
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getFileOnlyName() {
return fileOnlyName;
}
public void setFileOnlyName(String fileOnlyName) {
this.fileOnlyName = fileOnlyName;
}
public String CreateWordRrport(){
HttpServletRequest request= ServletActionContext.getRequest();
Session dbSession = HibernateSessionFactory.getSession();
String downloadType=request.getParameter("downloadType");
String staffId=request.getParameter("staffId");
String reportName=request.getParameter("reportName");
String reportTime=request.getParameter("reportTime");
String pageviews=request.getParameter("pageviews");
Map<String, Object> dataMap = new HashMap<>();
dataMap.put("staffId", staffId);
dataMap.put("reportName", reportName);
dataMap.put("reportTime", reportTime);
dataMap.put("pageviews", pageviews);
/** 文件名称,唯一字符串 */
Random r=new Random();
SimpleDateFormat sdf1=new SimpleDateFormat("yyyyMMdd_HHmmss_SSS");
StringBuffer sb=new StringBuffer();
sb.append(sdf1.format(new Date()));
sb.append("_");
sb.append(r.nextInt(100));
//文件路径
filePath=ServletActionContext.getServletContext().getRealPath("/")+"Report";
//文件唯一名称
fileOnlyName = downloadType+sb+".doc";
//文件名称
fileName=downloadType+sb+".doc";
/** 生成word */
DocCreater.createWord(dataMap, "简报管理.xml", filePath, fileOnlyName);
return "createWordSuccess";
}
public String dowloadWord() {
/** 先判断文件是否已生成 */
try {
//解决中文乱码
filePath = URLDecoder.decode(filePath, "UTF-8");
fileOnlyName = URLDecoder.decode(fileOnlyName, "UTF-8");
fileName = URLDecoder.decode(fileName, "UTF-8");
//如果文件不存在,则会跳入异常,然后可以进行异常处理
new FileInputStream(filePath + File.separator + fileOnlyName);
} catch (Exception e) {
e.printStackTrace();
return "error";
}
return "dowloadWord";
}
xml configuration
<action name="CreateWordRrport" class="com.ps.action.WordAction" method="CreateWordRrport">
<result name="createWordSuccess" type="redirectAction">
<param name="actionName">dowloadWord</param>
<param name="filePath">${filePath}</param>
<param name="fileOnlyName">${fileOnlyName}</param>
<param name="fileName">${fileName}</param>
<param name="encode">true</param>
</result>
</action>