不废话,直接上代码。

前端代码:

<template><Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="110"><FormItem label="项目(全称):" prop="orgName"><Input v-model="formValidate.orgName" placeholder="请输入项目名称"></Input></FormItem><FormItem label="申请人:" prop="applyName" ><Input v-model="formValidate.applyName" placeholder="请输入申请人"></Input></FormItem><FormItem label="电话:" prop="applyPhone"><Input v-model="formValidate.applyPhone" placeholder="请输入电话"></Input></FormItem><FormItem label="生效日期:" style="float: left"><Row><FormItem prop="startDate"><DatePicker type="date" format="yyyy-MM-dd" placeholder="请选择生效日期" v-model="formValidate.startData"></DatePicker></FormItem></Row></FormItem><FormItem label="失效日期:"><Row><FormItem prop="endDate"><DatePicker type="date" format="yyyy-MM-dd" placeholder="请选择失效日期" v-model="formValidate.endData"></DatePicker></FormItem></Row></FormItem><FormItem label="备注:" prop="vmemo"><Input v-model="formValidate.vmemo" type="textarea" :autosize="{minRows: 2,maxRows: 5}" placeholder="备注"></Input></FormItem><FormItem><Button type="primary" @click="handleSubmit('formValidate')">生成申请单</Button></FormItem></Form>
</template>
<script>import axios from 'axios';export default {data () {return {formValidate: {orgName: '',applyName: '',applyPhone: '',startDate: '',endDate: '',vmemo:''},ruleValidate: {orgName: [{ required: true, message: '项目名称不能为空!', trigger: 'blur' }],applyName: [{ required: true, message: '申请人不能为空!', trigger: 'blur' }],applyPhone: [{ required: true, message: '电话不能为空!', trigger: 'change' }],startDate: [{ required: true, type: 'date', message: '请输入license有效期!', trigger: 'change' }],endDate: [{ required: true, type: 'date', message: '请输入license有效期!', trigger: 'change' }],}}},methods: {handleSubmit (name) {this.$refs[name].validate((valid) => {if (valid) {axios({method: 'post',url: this.$store.getters.requestNoteUrl,data: this.formValidate,responseType: 'blob'}).then(res  => {this.download(res.data);});}});},download (data) {if (!data) {return}let url = window.URL.createObjectURL(new Blob([data]))let link = document.createElement('a');link.style.display = 'none';link.href = url;link.setAttribute('download', this.formValidate.orgName+'('+ this.formValidate.applyName +')'+'-申请单.doc');document.body.appendChild(link);link.click();}}}
</script>
 

后台:

/*** 生成license申请单*/
@RequestMapping(value = "/note", method = RequestMethod.POST)
public void requestNote(@RequestBody LicenseRequestNoteModel noteModel, HttpServletRequest req, HttpServletResponse resp) {File file = null;InputStream fin = null;ServletOutputStream out = null;try {req.setCharacterEncoding("utf-8");file = ExportDoc.createWord(noteModel, req, resp);fin = new FileInputStream(file);resp.setCharacterEncoding("utf-8");resp.setContentType("application/octet-stream");resp.addHeader("Content-Disposition", "attachment;filename="+ noteModel.getOrgName()+"申请单.doc");resp.flushBuffer();out = resp.getOutputStream();byte[] buffer = new byte[512];  // 缓冲区int bytesToRead = -1;// 通过循环将读入的Word文件的内容输出到浏览器中while ((bytesToRead = fin.read(buffer)) != -1) {out.write(buffer, 0, bytesToRead);}} catch (Exception e) {e.printStackTrace();} finally {try {if (fin != null) fin.close();if (out != null) out.close();if (file != null) file.delete(); // 删除临时文件} catch (IOException e) {e.printStackTrace();}}}
public class ExportDoc {private static final Logger logger = LoggerFactory.getLogger(ExportDoc.class);// 针对下面这行有的报空指针,是目录问题,我的目录(项目/src/main/java,项目/src/main/resources),这块也可以自己指定文件夹private static final String templateFolder = ExportDoc.class.getClassLoader().getResource("/").getPath();private static Configuration configuration = null;private static Map<String, Template> allTemplates = null;static {configuration = new Configuration();configuration.setDefaultEncoding("utf-8");allTemplates = new HashedMap();try {configuration.setDirectoryForTemplateLoading(new File(templateFolder));allTemplates.put("resume", configuration.getTemplate("licenseApply.ftl"));} catch (IOException e) {e.printStackTrace();throw new RuntimeException(e);}}public static File createWord(LicenseRequestNoteModel noteModel, HttpServletRequest req, HttpServletResponse resp) throws Exception {File file = null;req.setCharacterEncoding("utf-8");// 调用工具类WordGenerator的createDoc方法生成Word文档file = createDoc(getData(noteModel), "resume");return file;}public static File createDoc(Map<?, ?> dataMap, String type) {String name = "temp" + (int) (Math.random() * 100000) + ".doc";File f = new File(name);Template t = allTemplates.get(type);try {// 这个地方不能使用FileWriter因为需要指定编码类型否则生成的Word文档会因为有无法识别的编码而无法打开Writer w = new OutputStreamWriter(new FileOutputStream(f), "utf-8");t.process(dataMap, w);w.close();} catch (Exception ex) {ex.printStackTrace();throw new RuntimeException(ex);}return f;}private static Map<String, Object> getData(LicenseRequestNoteModel noteModel) throws Exception {Map<String, Object> map = new HashedMap();map.put("orgName", noteModel.getOrgName());map.put("applyName", noteModel.getApplyName());map.put("applyPhone", noteModel.getApplyPhone());map.put("ncVersion", noteModel.getNcVersionModel());map.put("environment", noteModel.getEnvironmentModel());map.put("applyType", noteModel.getApplyTypeModel());map.put("mac", GetLicenseSource.getMacId());map.put("ip", GetLicenseSource.getLocalIP());map.put("startData", DateUtil.Date(noteModel.getStartData()));map.put("endData", DateUtil.Date(noteModel.getEndData()));map.put("hostName", noteModel.getHostNames());map.put("vmemo", noteModel.getVmemo());return map;}}
public class LicenseRequestNoteModel{private String orgName = null;private String applyName = null;private String applyPhone = null;private String ncVersionModel= null;private String environmentModel= null;private String applyTypeModel= null;@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")@DateTimeFormat(pattern = "yyyy-MM-dd")private Date startData= null;@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")@DateTimeFormat(pattern = "yyyy-MM-dd")private Date endData= null;private String[] hostName= null;private String vmemo= null;private String applyMAC= null;private String applyIP= null;public String getOrgName() {return orgName;}public void setOrgName(String projectName) {this.orgName = projectName;}public String getApplyName() {return applyName;}public void setApplyName(String applyName) {this.applyName = applyName;}public String getApplyPhone() {return applyPhone;}public void setApplyPhone(String applyPhone) {this.applyPhone = applyPhone;}public String getNcVersionModel() {return ncVersionModel;}public void setNcVersionModel(String ncVersionModel) {this.ncVersionModel = ncVersionModel;}public String getEnvironmentModel() {return environmentModel;}public void setEnvironmentModel(String environmentModel) {this.environmentModel = environmentModel;}public String getApplyTypeModel() {return applyTypeModel;}public void setApplyTypeModel(String applyTypeModel) {this.applyTypeModel = applyTypeModel;}public Date getStartData() {return startData;}public void setStartData(Date startData) {this.startData = startData;}public Date getEndData() {return endData;}public void setEndData(Date endData) {this.endData = endData;}public String[] getHostName() {return hostName;}public String getHostNames() {return StringUtils.join(this.hostName,",");}public void setHostName(String[] hostName) {this.hostName = hostName;}public String getVmemo() {return vmemo;}public void setVmemo(String vmemo) {this.vmemo = vmemo;}public String getApplyMAC() {return applyMAC;}public void setApplyMAC(String applyMAC) {this.applyMAC = applyMAC;}public String getApplyIP() {return applyIP;}public void setApplyIP(String applyIP) {this.applyIP = applyIP;}
}

VUE动态生成word相关推荐

  1. 使用Vue动态生成form表单的实例代码

    具有数据收集.校验和提交功能的表单生成器,包含复选框.单选框.输入框.下拉选择框等元素以及,省市区三级联动,时间选择,日期选择,颜色选择,文件/图片上传功能,支持事件扩展. 欢迎大家star学习交流: ...

  2. C#实现动态生成Word

    1. 一个控制台例子,实现动态生成Word. 首先,添加引用:COM->Microsoft Word 11.0 Object Library. using System; using Syste ...

  3. Freemarker - 根据模板动态生成word文档

    文章目录 Freemarker 根据模板动态生成word文档 Freemarker 介绍: Freemarker 使用: freemarker加载模板目录的方法 参考资料 Freemarker 根据模 ...

  4. Springboot中使用freemarker动态生成word文档

    文章目录 freemarker模板动态生成word文档 前言 准备 简单模板准备 <一> `word 2003` 新建`.doc` 模板 <二> 另存为`.xml` 文件,格式 ...

  5. 【.NET】用Aspose.Words for .NET动态生成word文档中的数据表格

    1.概述 最近项目中有一个这样的需求:导出word 文档,要求这个文档的格式不是固定的,用户可以随便的调整,导出内容中的数据表格列是动态的,例如要求导出姓名和性别,你就要导出这两列的数据,而且这个文档 ...

  6. java自动生成word,java动态生成word解决方案

    java动态生成word 我想弄一个java网站上的可以生成word,上网搜索了几天,找到几个可以java令jsp(html)转换成word的有jacob和poi, 还有JS,还有在jsp上添加头文件 ...

  7. Java读取word模板,并动态生成word

    Java读取word模板,并动态生成word ​ 最近有个需求是将数据库里存入的用户个人信息生成一个word然后供用户下载,第一时间就就想到了poi来做,所以记录一下免得自己忘了,忘了也可以回来看看

  8. Java动态生成word文档(图文并茂)

    很多情况下,软件开发者需要从数据库读取数据,然后将数据动态填充到手工预先准备好的Word模板文档里,这对于大批量生成拥有相同格式排版的正式文件非常有用,这个功能应用PageOffice的基本动态填充功 ...

  9. Java中利用freemarker模板动态生成word含表格

    最近公司有导出word的需求,由于word的样式有的很复杂所以记录一下Java中利用freemarker模板动态生成word含表格,以防以后忘记. 1.word表格的模板 删掉无用的数据留下基础的样式 ...

最新文章

  1. 独家 | 11步转行数据科学家 (送给数据员/ MIS / BI分析师)
  2. .net微软消息队列(msmq)简单案例
  3. 都在抢论文第一作者,如何处理?
  4. 虚拟化与云计算(一)之 Lab1 使用 Hadoop Mapreduce 进行数据处理
  5. 远控免杀专题(24)-CACTUSTORCH免杀
  6. Python函数传入的参数是否改变(函数参数、指针、引用)
  7. Go语言init函数你必须记住的六个特征
  8. 三大前端框架,哪个框架组件间交互像js方法传值一样简单
  9. JNI_OnLoad
  10. Ubuntu18.04中安装virtualenv和virtualenvwrapper
  11. 海思开发记录(一):3559A开发环境搭建
  12. 接口测试工具设计与实现
  13. # Idea,2.5,软件安装,提示If you already have a 64-bit JDK installed ,defined a JAVA_HOME variable in Compu
  14. android 稳定的定时器,Multi Timer「多工计时器」v2.8.2 for Android 特别高级版
  15. 树莓派 PHP白屏,树莓派系统安装及3.5寸显示屏白屏解决办法
  16. Esp32-C3使用gpio唤醒深度睡眠,rtc gpio0~5始终置低,导致低电平唤醒一直复位,高电平唤醒无效?
  17. pg事务:隔离级别历史与SSI
  18. 没想到这一天来的这么快 大数据之下再无隐私
  19. 【数据可视化应用】Python反距离权重(IDW)插值计算及可视化绘制
  20. “影响力之父”西奥迪尼:人类就像录音机,按一下就播放

热门文章

  1. 状压dp、数位dp、概率dp
  2. 『NLP自然语言处理』中文文本的分词、去标点符号、去停用词、词性标注
  3. 【机器学习基础】Scipy(科学计算库) 手把手手把手
  4. 饥荒海难创建显示专用服务器,饥荒海难控制台使用教程及小技巧_快吧单机游戏...
  5. Cause: com.microsoft.sqlserver.jdbc.SQLServerException: 关键字 'user' 附近有语法错误
  6. 10个提升PPT幻灯片制作效率的方法
  7. AndroidManifest.xml中常用属性及含义
  8. Spring事务报错Transaction synchronization is not active
  9. Effective-Java 检查参数有效性
  10. 九度oj 题目1365:贝多芬第九交响曲