前臺js只須要一個方法,css
1.Action:說明:dataMap是須要展現的數據,java
String rootPath = SaveFileUtil.FILE_PATH;此處是爲了判斷盤符的,win系統和linux系統狀況linux
/** * * @Description: 生成單 * @date 2017年4月10日 下午4:29:04 * @version 1.0 * @param */ 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); // 導出word DocumentHandler 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.判斷盤符:web
/** * 保存臺上傳到的文件 */ 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的路徑下,便可;app
5.生成word的方法ui
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文檔的公共類 * @author * @date */ 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就此結束this
下面的下載該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; }