前台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. 机器学习中的算法(4.3):SVM----针对线性不可分问题理解
  2. 超详干货!Linux 环境变量配置全攻略
  3. 使用jQuery for Asp.Net 我的开发环境配置
  4. Redis: under the hood---转载
  5. 进程比线程更多资源_为什么我们不应该使用比我们需要更多的线程
  6. sql注入基于错误-单引号-字符型
  7. js 数据写到本地记事本_微信小程序连接Mysql数据库步骤
  8. mysql5.7安装完成后密码是多少_安装了mysql5.7后,如何进行配置(密码等)初始化...
  9. A CAPTCHA Server Control for ASP.NET C# 版
  10. matlab实现直方图均衡化
  11. ansys toolkit教程_「实用」ANSYS电磁仿真工具推荐,做仿真的你可以试试
  12. 最近公司准备启动一个风险系统【Springboot + urule 】
  13. aliyun资源编排 介绍和实例
  14. ASEMI场效应管12N65参数,12N65规格书,12N65特征
  15. 参数估计与假设检验—拒绝域的数学证明(手写)
  16. 树莓派 + Home Assistant + HomeKit 从零开始打造个人智能家居系统 篇二:初步配置 Home Assistant 并连接小米设备与 HomeKit
  17. 红米note3全网通版刷机救砖 9008强刷(无需短接)
  18. iOS开发月报#11|201905
  19. STM32自学笔记15-步进电机驱动项目-磁编码器MT6816驱动
  20. linux上安装java失败,Linux下安装jdk失败怎么办

热门文章

  1. Google Earth Engine(GEE)——土地分类/覆盖案例分析含各类土地面积统计和精度评定(印度班加罗尔为例)
  2. 工作那些事(十八)入职体检,真囧!
  3. 思维导图 基础篇(14)应用-阅读书籍
  4. c语言printf snm,甘肃三校生高考模拟试题专业基础知识测试(二)
  5. mac下使用lsof命令查看链路状态
  6. 易优cms 后台地址改了,忘记了,去哪个文件能查看啊 Eyoucms快速入门
  7. 多少开发人员 饿了么_开发一款类似美团饿了么APP要多少钱?
  8. 程序人生也需要行为规范
  9. 专家汇 | 车联网这样上云,真的很酷!
  10. matlab异步电机仿真,基于MATLAB的异步电动机仿真.doc