为什么使用Freemarker将html导出成doc文档,有些人使用WPS打开文件会乱码???

发布于 2021-12-05 10:24:03 字数 3759 浏览 744 评论 2

service层代码如下:
@Override public InputStream exportWord0(RequestServer requestServer,String fileName) throws IOException{ File file = new File(fileName); DateFormat df =DateUtil.getsdfs(); //像freemarker模板填入数据 Map dataMap = new HashMap(); dataMap.put("workOrder", (null!=requestServer.getWorkOrder())?requestServer.getWorkOrder():""); dataMap.put("sendDate", (null!=requestServer.getSendDate())?df.format(requestServer.getSendDate()):""); dataMap.put("ownerName", (null!=requestServer.getOwnerName())?requestServer.getOwnerName():""); dataMap.put("sendUserName", (null!=requestServer.getSendUserName())?requestServer.getSendUserName():""); dataMap.put("sendUserPhone", (null!=requestServer.getMobilePhone())?requestServer.getMobilePhone():""); dataMap.put("responseUserName", (null!=requestServer.getResponseUserName())?requestServer.getResponseUserName():""); dataMap.put("treatment", (null!=requestServer.getTreatment())?requestServer.getTreatment():""); dataMap.put("title", (null!=requestServer.getTitle())?requestServer.getTitle():""); dataMap.put("requestContent", (null!=requestServer.getRequestContent())?requestServer.getRequestContent():""); dataMap.put("evaluteView", (null!=requestServer.getEvaluateView())?requestServer.getEvaluateView():""); dataMap.put("serverGrade", (null!=requestServer.getServerGrade())?requestServer.getServerGrade():""); ProcessDetails processDetails = new ProcessDetails(); if(null != requestServer.getRequestId() && StringUtils.isNotBlank(requestServer.getRequestId().toString())){ processDetails.setRequestId(requestServer.getRequestId()); } if(null != requestServer.getEventId() && StringUtils.isNotBlank(requestServer.getEventId().toString())){ processDetails.setEventid(requestServer.getEventId()); } List list = processDetailsDao.queryPaging(processDetails); dataMap.put("processDetailsList", (null!=list)?list:""); processDetails.setType("1"); processDetails.setModel("1"); ProcessDetails processDetails2 = processDetailsDao.queryProcessDetails(processDetails); if(null != processDetails2){ dataMap.put("qStartTime", (null!=processDetails2.getStartTime())?df.format(processDetails2.getStartTime()):""); dataMap.put("qEndTime", (null!=processDetails2.getStartTime())?df.format(processDetails2.getStartTime()):""); } List> list1 = new ArrayList>(); if(null != requestServer.getHardWareIds()){ String[] hardWareId = requestServer.getHardWareIds().split("\[\*\]"); Map map = new HashMap(); for (int i = 0; i < hardWareId.length; i++) { Hardware hardware = hardwareDao.queryByHardwareId(hardWareId[i]); if(null != hardware){ map.put("hardName", (null!=hardware.getHardName())?hardware.getHardName():""); map.put("hardSerial", (null!=hardware.getHardSerial())?hardware.getHardSerial():""); map.put("ip", (null!=hardware.getIp())?hardware.getIp():""); list1.add(map); } } } dataMap.put("hardwareList", (null!=list1)?list1:""); //文件路径 //String path=ServletActionContext.getServletContext().getRealPath("/")+"upload"; //freemaker模板文件名称 String templateFileName = "IT技术服务工单样式.ftl"; //创建配置实例 Configuration configuration = new Configuration(); //设置编码 configuration.setDefaultEncoding("UTF-8"); //模板文件所在路径,ftl模板文件统一放至 com.roots.template 包下面 configuration.setClassForTemplateLoading(ExportDocServiceImpl.class,"/com/roots/cloudserver/template/"); //获取模板 Template template = configuration.getTemplate(templateFileName); //生成文件 Writer out = new OutputStreamWriter(new FileOutputStream(file), "UTF-8"); InputStream in=new FileInputStream(file); try { template.process(dataMap, out); out.close(); } catch (TemplateException e) { // TODO Auto-generated catch block e.printStackTrace(); } //返回最终生成的word文件流 return in; }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

刘备忘录 2021-12-08 17:56:50

就不能格式化一下吗

柠檬 2021-12-08 15:06:41

我整个人都乱码了

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文