前台js只需要一个方法,

1.Action:说明:dataMap是需要展示的数据,

String rootPath = SaveFileUtil.FILE_PATH;此处是为了判断盘符的,win系统和linux系统情况

 public void makeDispatch(){try {String weekuuid=request.getParameter("weekuuid");String dayuuid=request.getParameter("dayuuid");String doflag="sh";String mondaygroupper=weekPlanTaskService.queryPerson(dayuuid,doflag);String checkPer=weekPlanTaskService.queryCheckById(dayuuid);String puuid = UserUtil.getPersonUuid();Map person = getPerson(puuid);String userDeptName = (String)person.get("DEPT_NAME");//机构//获取编号---编号  String BUILD_NO=weekPlanTaskService.queryBuildNo();//路径String rootPath = SaveAndroidSubmitFileUtil.FILE_PATH;String dispatchPath = Configuration.getString("dispatch") + DateUtil.getCurrentDate("yyyyMMdd") + "/dispatch/";String path=rootPath+dispatchPath;//路径String template = "", fileName = ""; // 模版名和文件名Map<String, Object> dataMap = new HashMap<String, Object>(); // 要导出到word的数据dataMap.clear();template = "checkDispatchDoc.ftl";fileName = "么么单.doc";dataMap.put("CLIENTNAME", mondaygroupper);dataMap.put("CHECKPER", checkPer);dataMap.put("ORGNAME", userDeptName);dataMap.put("NO", BUILD_NO);// 导出wordDocumentHandler doc = new DocumentHandler();String docFlag = doc.createDocNew(dataMap, path, fileName, template);elog.debug("hughman: " + docFlag);//保存到数据库String wordPath = dispatchPath + fileName;Map m=new HashMap();m.put("BUILD_PERSON", puuid);m.put("DOC_URL", wordPath);m.put("BUILD_NO", BUILD_NO);m.put("dayuuid", dayuuid);weekPlanTaskService.ModifyDispatch(m);//返回路径String filepath=dispatchPath+fileName;response.getWriter().write("{\"filepath\":\""+ filepath + "\"}");     } catch (Exception e) {LogUtil.error(e);e.printStackTrace();}}

2.判断盘符:

/*** 保存台上传到的文件*/
public class SaveFileUtil {public static String FILE_PATH = "";  //文件地址public static String IMAGE_DEFAULT = "/include/images/default.jpg";static {Properties prop = System.getProperties();String os = prop.getProperty("os.name");if (os.indexOf("win") >= 0 || os.indexOf("Win") >= 0) {FILE_PATH = Configuration.getString("WIN_FILE_UPLOAD_PATH");} else {FILE_PATH = Configuration.getString("FILE_UPLOAD_PATH");}}

3.这里涉及获取编码:首先去库里查询 最大编码,然后再次基础上+1,编码的格式的 当前年与文件数的之和

//生成编码public String queryBuildNo(){String BuildNo=weekPlanTaskMapper.queryBuildNo();String year=DateUtil.getCurrentDate("yyyy");if(BuildNo.isEmpty()||BuildNo==""||BuildNo.equals("")){BuildNo=year+"0001";}else{String Peryear=BuildNo.substring(0, 4);String num=BuildNo.substring(4, 8);int endNum = Integer.parseInt(num);//年份是当年 就加1  不是的话换年份if(Peryear.equals(year)){endNum=endNum+1;String NUM=String.valueOf(endNum);if(NUM.length()==1){NUM="000"+NUM;}else if(NUM.length()==1){NUM="00"+NUM;}else if(NUM.length()==1){NUM="0"+NUM;}BuildNo=Peryear+NUM;}else{BuildNo=year+"0001";}}return BuildNo;}

4.模板的生成,首先将自己需要的word模板制作好,然后,需要导出的部门使用dataMap的字段写好,然后另存为xml格式,改名为ftl,然后搜索刚刚的字段,全部写成${字段} 即可,将ftl文件放到java的src的路径下,即可;

5.生成word的方法

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
/*** @description 导出word文档的公共类*/
public class DocumentHandler {private Configuration configuration = null;public DocumentHandler() {configuration = new Configuration();configuration.setDefaultEncoding("utf-8");}/*** * @param dataMap 要填入模本的数据文件* @param path 输出文档的路径* @param fileName 输出文档的名称* @param templateName 模版文件名称* @return flag 0000导出成功 0001模版不存在 0002文件编码异常 0003模版异常 0004导出异常*/public String createDocNew(Map<String, Object> dataMap, String path,String fileName, String templateName) {String flag = "0000";try {// 设置模本装置方法和路径,包名configuration.setClassForTemplateLoading(this.getClass(), "/com/icss/apcd/util/template");// .ftl为要装载的模板Template template = configuration.getTemplate(templateName);// 输出文档路径及名称File outFile = new File(path);if (!outFile.exists()) {outFile.mkdirs();}outFile = new File(path + fileName);/** 此处对流的编码不可或缺,使用main()单独调用时,应该可以* 但是如果是web请求导出时导出后word文档就会打不开,并且报XML文件错误,主要是编码格式不正确,无法解析*/FileOutputStream fos = new FileOutputStream(outFile);OutputStreamWriter osWriter = new OutputStreamWriter(fos, "UTF-8");Writer writer = new BufferedWriter(osWriter);template.process(dataMap, writer);writer.close();fos.close();} catch (FileNotFoundException e) {flag = "0001";e.printStackTrace();} catch (UnsupportedEncodingException e) {flag = "0002";e.printStackTrace();} catch (TemplateException e) {flag = "0003";e.printStackTrace();} catch (IOException e) {flag = "0004";e.printStackTrace();}return flag;}}

完结--生成word就此结束

下面的下载该word

/*** 下载文件*/public String downLoadFile() {String fileName="么么单.doc";//fileName的后缀名决定下载的文件类型String dayuuid=request.getParameter("dayuuid");String url=weekPlanTaskService.getURLById(dayuuid);response.setContentType( "application/msword");//response.setContentType("images/x-dcx");response.setHeader("Pragma", "No-cache");response.setHeader("Cache-Control", "no-cache");response.setDateHeader("Expires", 0);try {// 这个就就是弹出下载对话框的关键代码response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));String rootPath = SaveAndroidSubmitFileUtil.FILE_PATH;File resFile = new File(rootPath + url);InputStream input = new FileInputStream(resFile);ServletOutputStream out = response.getOutputStream();byte[] buffer = new byte[1024];int i = 0;while ((i = input.read(buffer)) != -1) {out.write(buffer, 0, i);}input.close();out.flush();out.close();} catch (Exception e) {e.printStackTrace();}return null;}

【Java】使用模板生成word文档到服务器,并下载相关推荐

  1. java根据模板生成word文档_Python办公自动化:使用python来自动生成word文档

    让python做办公自动化,让你闲下来 让python做自动化,让你闲下来 上节对python的excel Python办公自动化系列:自动操作Excel自动化做了介绍.这次介绍如何用python对w ...

  2. JAVA实现模板word文档导入,Java依据word模板生成word文档之后台解析和实现及部分代码(一)...

    Java根据word模板生成word文档之后台解析和实现及部分代码(一) 后台主要工作是解析XML定义的标签文件,并获取到数据集,放入到Map中,然后调用Jacob.jar中提供的相关方法来实现替换. ...

  3. Java使用poi-tl生成word文档

    Java使用poi-tl生成word文档,可以对模板文件进行文本替换,图片.表格.超链接添加.图表处理等.大概的说明都在代码注释里,只有一个地方需要注意,就是图表的替换,占位符{{barChart}} ...

  4. Java使用freemarker生成word文档并转pdf文档

    Java使用freemarker生成word文档后转pdf 先来看看效果图 进入正题 项目需求: 为订单后生成对应的pdf文档,文档内包含图片. 方案一:使用freemarker和itext把html ...

  5. Android 使用模板生成Word文档,支持手机直接查看word

    最近在项目工作中,碰到一个很棘手的需求,说是要在手机端根据模板生成word文档,而且不借助第三方的软件可以查看word文档,一开始听这个需求差不多蒙了,这要怎么做,为什么不把生成word文档这个工作放 ...

  6. 根据标准word模板生成word文档类库(开源)

    前言   最近因项目需要要自定义标准word模板,并以编码方式操作word模板.填充数据和生成word文档,于是自己写了条小"内裤"来实现这个功能.该"内裤"只 ...

  7. 使用word模板生成word文档的各类方案

    使用word模板生成word文档的各类方案 生成word的各种方案 word另存xml进行后续处理 2003版本word(.doc)的xml处理并生成word 2007版本word(.docx)的xm ...

  8. freemarker根据模板生成word文档,换行

    freemarker根据模板生成word文档,其它地方已经说的非常清除了,在此简单再说以下. 1.制作word模板,另存为xml文件.在此我另存为的时windows xml,它和windows 200 ...

  9. PHP 使用word模板生成word文档示例

    <?php namespace Home\Controller; use PhpOffice\PhpWord\TemplateProcessor; use Think\Controller; c ...

最新文章

  1. oracle引号的嵌套,3.4.2 在一个直接量字符串中嵌入单引号
  2. python编程自学能学会吗-Python能自学成功吗?
  3. wamp2.2-64位 localhost和localhost/phpmyadmin不能访问问题解决
  4. Request load inbound error - COM_ATTRFRG_GEN 066
  5. SAP Cloud for Customer Sales Order Pricing Date的配置
  6. 大前端时代,从前端小工到架构师的进阶锦囊!
  7. Map对象,Set对象使用(1)
  8. 组合模式Composite
  9. IntelliJ IDEA使用技巧(七)——常用快捷键Mac篇
  10. 20200411使用Potplayer对视频进行逐帧截图
  11. 计算机技术和信息技术结合,浅谈通信技术与计算机技术融合发展
  12. ckplayer php,ckplayer播放器
  13. 全国大学生GIS应用技能大赛(开发试题参考)
  14. 【笔记】如果使用类的话,需要让类实现Serializable,这个接口是个标记接口,要给序列号,如果想要某个数据不显示的话可以在成员变量前加transient
  15. [渝粤教育] 中国地质大学 机械电气安全技术 复习题 (2)
  16. kvm直通sata_将双系统塞入A4主机:OSX-KVM 显卡直通教程
  17. 【转】用天文方法计算二十四节气(下)
  18. java.lang.RuntimeException: Performing stop of activity that is not resumed OOM了也会造成之前Activity被系统杀掉
  19. 交换机的三种端口类型
  20. Cocos2d-x之动作类

热门文章

  1. ftp文件夹错误无法访问此文件夹_你不能访问此共享文件夹,因为你组织的安全策略组织未经身份验证的来宾访问。...
  2. nginx转发rtmp流
  3. 这所院校23年分数线暴涨45分,临时扩招69人!!
  4. 杭电oj —— 2010
  5. 秋招|阿里测试开发岗面经(一共七次面试)
  6. 微信小程序答题功能(二)- - - 按选项答题
  7. python 阴阳师 识别图像_Python爬取阴阳师式神全图鉴图片
  8. 大学计算机应用技术基础,win7_《计算机应用技术基础》ppt课件_ppt_大学课件预览_高等教育资讯网...
  9. 【ARMNN/编译】tensorflow lite版本
  10. 基于STM32和RPR220光电传感器的智能停车系统【遮挡检测LED显示】