java 使用word模板生成word
java使用word模板生成worda44102244:请问这个是怎么调用
java使用word模板生成word桑岩lk:大佬这是啥错误呀ThesupplieddataappearstobeintheOffice2007+XML.YouarecallingthepartofPOIthatdealswithOLE2OfficeDocuments.YouneedtocalladifferentpartofPOItoprocessthisdata(egXSSFinsteadofHSSF)
java使用word模板生成word康ii:为什么生成的doc文件打不开呢
h5canvas+js实现烟花效果人間_過客:你的链接指向了一个全是广告的页面,请注意检查一下
h5canvas+js实现烟花效果DeepLearning小舟:很不错的文章,学到东西了!
Java输出Word文档,Java生成Word文档
返回导航页返回导航页
业务来源需要将一些信息,生成word然后导出来。
工具推荐开源的,非常好用,直接跑个demo,你就能体会多方便了!deepoove
直接安排个demo操作word和操作excel不太一样,操作word首先需要准备一个模板,需要填充内容的地方,使用占位符,先把位置占好,然后在业务中进行占位符填充!
我解释明白了吗?不明白?那直接截图官方文档吧!!!还不明白?那直接看我写的吧!安排!第一步第二步第三步多种输出方式,这里代码演示网络流的输出!
/**获取报告*/@RequestMapping("/download/word**/***")publicvoiddownloadWord(HttpServletResponseresponse)throwsException{XWPFTemplatetemplate=XWPFTemplate.compile("src/main/resources/template.docx").render(newHashMap(){{put("title","这个是标题哈,填充进去");}});//文件流传输//template.writeAndClose(newFileOutputStream("output.docx"));//使用网络流进行传输response.setContentType("application/octet-stream");response.setHeader("Content-disposition","attachment;filename=""+"输出文件.docx"+""");//HttpServletResponseresponseOutputStreamout=response.getOutputStream();BufferedOutputStreambos=newBufferedOutputStream(out);template.write(bos);bos.flush();out.flush();PoitlIOUtils.closeQuietlyMulti(template,bos,out);}postman测试一把咯文件名中文的有问题,后续在处理吧!不过内容没得问题了。
大坑################################大坑#########################################
springboot打成jar包,会导致目录结构发生改变,绝对路径是没有用的;这个问题太坑了,本地测试都没有问题,上测试环境就出现问题了!!!!还有就是处理中文乱码问题。
解决方法:使用流
/**获取报告*/@RequestMapping("/download/word**/***")publicvoiddownloadWord(HttpServletResponseresponse)throwsException{ClassPathResourceresource=newClassPathResource("template.docx");InputStreaminputStream=resource.getInputStream();XWPFTemplatetemplate=XWPFTemplate.compile(inputStream).render(newHashMap(){{put("title","这个是标题哈,填充进去");}});//文件流传输//template.writeAndClose(newFileOutputStream("output.docx"));//使用网络流进行传输response.setContentType("application/octet-stream");response.setHeader("Content-Disposition","attachment;filename="+java.net.URLEncoder.encode("小可爱文档.docx","UTF-8"));//HttpServletResponseresponseOutputStreamout=response.getOutputStream();BufferedOutputStreambos=newBufferedOutputStream(out);template.write(bos);bos.flush();out.flush();PoitlIOUtils.closeQuietlyMulti(template,bos,out);}java导出生成word之XML方式
1.简介Word从2003开始支持XML格式,操作流程
先用office2003或者2007编辑好word的样式,然后另存为xml,将xml翻译为FreeMarker模板,最后用java来解析FreeMarker模板并输出Doc。经测试这样方式生成的word文档完全符合office标准,样式、内容控制非常便利,打印也不会变形,生成的文档和office中编辑文档完全一样。
2.集成使用新建项目
引入相关pom依赖 FreeMarker
org.springframework.bootspring-boot-starter-freemarker在application.propertes中添加相应配置
##Freemarker配置##模版存放路径(默认为classpath:/templates/)spring.freemarker.template-loader-path=classpath:/templates/##是否生成缓存,生成环境建议开启(默认为true)spring.freemarker.cache=false##编码spring.freemarker.charset=UTF-8spring.freemarker.check-template-location=true##content-type类型(默认为test/htjava使用POI导出word数据以及生成word表格
暑期在杭州实习了两个月,主要是使用vue+SpringMVC进行一个网页开发。而在开发的过程中,也遇到了比较常见的文件导出问题–以固定格式将数据存储在word、excel等office文件格式中。在网上搜索了许多方法,了解到要导出word文件,其实办法由很多,如jacob,java2word,FreeMarker,ApatchPOI等等。本文采用的是ApatchPOI中的一系列API,它可以操作基于MicroSoftOLE2CompoundDocumentFormat的各种格式文件,可以通过这些API在Java中读写Excel、Word等文件。原则意义上来讲,POI更适合excel文件格式的导出,格式固定且代码简单。
而在使用POI操作word文档时,比较麻烦的一点是.doc文件和.docx文件是不同的,也就是说,需要使用不同的API来操作.doc文件和.docx文件。
一、.doc文件:使用HWPFDocument1)模板文件新建word模板文件,并在word文件中设置【标签】。
普通数据:一般标签以${变量名}的格式命名,其中$和{}一定是英文符号,如${project},${time}。注意标签名不要重复表格文件:由于表格数据是由多组相同格式的数据组成,在计算机语言中,及由数组组成。其与普通数据不同,无需设置标签,只需要在word文件中新建空白表格,并设置表头即可,系统可以根据相应单元格的位置对表格数据进行设置。模板文件编辑完成之后,记得将模板文件放在项目中。本项目放在了template文件夹中,当然,放在哪里,在代码中就要使用相应的路径来获取相应的模板文件。2)文件导出在第一步中已经使用${}的格式对标签进行了设置,那么在代码中,我们只需要使用给相应的标签设置值即可。具体代码如下:
publicbooleanExport2GeotechnicalLayeringTable(Mapmap_data,ArrayListlist_data,StringtemplatePath,OutputStreamout){booleanresult=false;FileInputStreamin=null;HWPFDocumentdocument=null;try{in=newFileInputStream(templatePath);document=newHWPFDocument(in);Rangerange=document.getRange();range.replaceText("${project_id}",map_data.get("project_id").toString());//range.replaceText("${project_name}",map_data.get("project_name").toString());//range.replaceText("${depth}",map_data.get("depth").toString());//range.replaceText("${hole_id}",map_data.get("hole_id").toString());//range.replaceText("${hole_altitude}",map_data.get("hole_altitude").toString());//range.replaceText("${hole_mileage}",map_data.get("hole_mileage").toString());//range.replaceText("${endhole_depth}",map_data.get("endhole_depth").toString());////写入表格数据//遍历range范围内的table。TableIteratortableIter=newTableIterator(range);Tabletable;TableRowrow;while(tableIter.hasNext()){table=tableIter.next();introwNum=table.numRows();for(inti=0,j=2;i