VUE动态生成word的实现
不废话,直接上代码。
前端代码:
|
|
生成申请单
后台:
/** *生成license申请单 */ @RequestMapping(value="/note",method=RequestMethod.POST) publicvoidrequestNote(@RequestBodyLicenseRequestNoteModelnoteModel,HttpServletRequestreq,HttpServletResponseresp){ Filefile=null; InputStreamfin=null; ServletOutputStreamout=null; try{ req.setCharacterEncoding("utf-8"); file=ExportDoc.createWord(noteModel,req,resp); fin=newFileInputStream(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=newbyte[512];//缓冲区 intbytesToRead=-1; //通过循环将读入的Word文件的内容输出到浏览器中 while((bytesToRead=fin.read(buffer))!=-1){ out.write(buffer,0,bytesToRead); } }catch(Exceptione){ e.printStackTrace(); }finally{ try{ if(fin!=null)fin.close(); if(out!=null)out.close(); if(file!=null)file.delete();//删除临时文件 }catch(IOExceptione){ e.printStackTrace(); } } }
publicclassExportDoc{ privatestaticfinalLoggerlogger=LoggerFactory.getLogger(ExportDoc.class); //针对下面这行有的报空指针,是目录问题,我的目录(项目/src/main/java,项目/src/main/resources),这块也可以自己指定文件夹 privatestaticfinalStringtemplateFolder=ExportDoc.class.getClassLoader().getResource("/").getPath(); privatestaticConfigurationconfiguration=null; privatestaticMapallTemplates=null; static{ configuration=newConfiguration(); configuration.setDefaultEncoding("utf-8"); allTemplates=newHashedMap(); try{ configuration.setDirectoryForTemplateLoading(newFile(templateFolder)); allTemplates.put("resume",configuration.getTemplate("licenseApply.ftl")); }catch(IOExceptione){ e.printStackTrace(); thrownewRuntimeException(e); } } publicstaticFilecreateWord(LicenseRequestNoteModelnoteModel,HttpServletRequestreq,HttpServletResponseresp)throwsException{ Filefile=null; req.setCharacterEncoding("utf-8"); //调用工具类WordGenerator的createDoc方法生成Word文档 file=createDoc(getData(noteModel),"resume"); returnfile; } publicstaticFilecreateDoc(Map,?>dataMap,Stringtype){ Stringname="temp"+(int)(Math.random()*100000)+".doc"; Filef=newFile(name); Templatet=allTemplates.get(type); try{ //这个地方不能使用FileWriter因为需要指定编码类型否则生成的Word文档会因为有无法识别的编码而无法打开 Writerw=newOutputStreamWriter(newFileOutputStream(f),"utf-8"); t.process(dataMap,w); w.close(); }catch(Exceptionex){ ex.printStackTrace(); thrownewRuntimeException(ex); } returnf; } privatestaticMap getData(LicenseRequestNoteModelnoteModel)throwsException{ Map map=newHashedMap(); 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()); returnmap; } }
publicclassLicenseRequestNoteModel{ privateStringorgName=null; privateStringapplyName=null; privateStringapplyPhone=null; privateStringncVersionModel=null; privateStringenvironmentModel=null; privateStringapplyTypeModel=null; @JsonFormat(pattern="yyyy-MM-dd",timezone="GMT+8") @DateTimeFormat(pattern="yyyy-MM-dd") privateDatestartData=null; @JsonFormat(pattern="yyyy-MM-dd",timezone="GMT+8") @DateTimeFormat(pattern="yyyy-MM-dd") privateDateendData=null; privateString[]hostName=null; privateStringvmemo=null; privateStringapplyMAC=null; privateStringapplyIP=null; publicStringgetOrgName(){ returnorgName; } publicvoidsetOrgName(StringprojectName){ this.orgName=projectName; } publicStringgetApplyName(){ returnapplyName; } publicvoidsetApplyName(StringapplyName){ this.applyName=applyName; } publicStringgetApplyPhone(){ returnapplyPhone; } publicvoidsetApplyPhone(StringapplyPhone){ this.applyPhone=applyPhone; } publicStringgetNcVersionModel(){ returnncVersionModel; } publicvoidsetNcVersionModel(StringncVersionModel){ this.ncVersionModel=ncVersionModel; } publicStringgetEnvironmentModel(){ returnenvironmentModel; } publicvoidsetEnvironmentModel(StringenvironmentModel){ this.environmentModel=environmentModel; } publicStringgetApplyTypeModel(){ returnapplyTypeModel; } publicvoidsetApplyTypeModel(StringapplyTypeModel){ this.applyTypeModel=applyTypeModel; } publicDategetStartData(){ returnstartData; } publicvoidsetStartData(DatestartData){ this.startData=startData; } publicDategetEndData(){ returnendData; } publicvoidsetEndData(DateendData){ this.endData=endData; } publicString[]getHostName(){ returnhostName; } publicStringgetHostNames(){ returnStringUtils.join(this.hostName,","); } publicvoidsetHostName(String[]hostName){ this.hostName=hostName; } publicStringgetVmemo(){ returnvmemo; } publicvoidsetVmemo(Stringvmemo){ this.vmemo=vmemo; } publicStringgetApplyMAC(){ returnapplyMAC; } publicvoidsetApplyMAC(StringapplyMAC){ this.applyMAC=applyMAC; } publicStringgetApplyIP(){ returnapplyIP; } publicvoidsetApplyIP(StringapplyIP){ this.applyIP=applyIP; } }
补充知识:vueelementui页面预览导入excel表格数据
html代码:
数据预览
js代码:
importXLSXfrom'xlsx' data(){ return{ tableData:'', tableHeader:'' } }, mounted:{ document.getElementsByClassName('el-upload__input')[0].setAttribute('accept','.xlsx,.xls') document.getElementsByClassName('el-upload__input')[0].onchange=(e)=>{ constfiles=e.target.filesconstitemFile=files[0]//onlyusefiles[0]if(!itemFile) returnthis.readerData(itemFile) } }, methods:{ generateDate({tableTitle,header,results}){ this.tableTitle=tableTitle this.tableData=results this.tableHeader=header }, handleDrop(e){ e.stopPropagation() e.preventDefault() constfiles=e.dataTransfer.files if(files.length!==1){ this.$message.error('Onlysupportuploadingonefile!') return } constitemFile=files[0]//onlyusefiles[0] this.readerData(itemFile) e.stopPropagation() e.preventDefault() }, handleDragover(e){ e.stopPropagation() e.preventDefault() e.dataTransfer.dropEffect='copy' }, readerData(itemFile){ if(itemFile.name.split('.')[1]!='xls'&&itemFile.name.split('.')[1]!='xlsx'){ this.$message({message:'上传文件格式错误,请上传xls、xlsx文件!',type:'warning'}); }else{ constreader=newFileReader() reader.onload=e=>{ constdata=e.target.result constfixedData=this.fixdata(data) constworkbook=XLSX.read(btoa(fixedData),{type:'base64'}) constfirstSheetName=workbook.SheetNames[0]//第一张表sheet1 constworksheet=workbook.Sheets[firstSheetName]//读取sheet1表中的数据deleteworksheet['!merges']letA_l=worksheet['!ref'].split(':')[1]//当excel存在标题行时 worksheet['!ref']=`A2:${A_l}` consttableTitle=firstSheetName constheader=this.get_header_row(worksheet) constresults=XLSX.utils.sheet_to_json(worksheet) this.generateDate({tableTitle,header,results}) } reader.readAsArrayBuffer(itemFile) } }, fixdata(data){ leto='' letl=0 constw=10240 for(;l以上这篇VUE动态生成word的实现就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。