下载word文档方法功能 读取json字段

 
D:\maven\Repository\net\sf\json-lib\json-lib\2.4\json-lib-2.4-jdk15.jar
//读取json
List<Address> orgs = super.query(Address.class, Cnd.cri());
            List<CommonSelect> list = Utils.parseToSelect(orgs, "addName",
                    "id", "parentId");
            Map<String, String> completeAddNameKey = new HashMap<String, String>();
            for (CommonSelect commonSelect : list) {
                completeAddNameKey.put(commonSelect.getValue(),
                        commonSelect.getKey());
            }


导出word
/**
      * 下载word文档方法
      *
      * @param recordId
      *            参数id
      * @return
      */
     @At("/downloadWorkDiaryDoc")
     @Ok("raw:stream")
     public Object downloadWorkDiaryDoc(@Param("diaryId") String diaryId) {
          if (Utils.isNullOrEmpty(diaryId)) {
              return null;
          }
          Map<String, Object> map = this.getWordData(diaryId);
          if (map.isEmpty()) {
              log.error("日记map为空!");
              return Results.parse(Constants.STATE_FAIL, "未查询到日记数据!");
          }
          // 创建文件输出流
          Writer out = null;
          try {
              Configuration configuration = new Configuration();
              configuration.setDefaultEncoding("UTF-8");
              HttpServletRequest request = Mvcs.getReq();
              HttpServletResponse resp = Mvcs.getResp();
              String path = "/wordTemplet/workDiary"; // 定义模板路径
              String TemplateName = ""; // 模板名字
              String downloadFileName = "";
              String name = null;
              if (map.get("userTypeStr") != null
                        && map.get("userTypeStr").equals("总监")) {
                   TemplateName = "ZJWorkDiary.ftl"; // 总监模板
                   name = (map.get("time") == null ? "" : map.get("time"))
                             + "_"
                             + (map.get("username") == null ? "" : map
                                      .get("username")) + "_总监日记_" + Math.random()
                             * 10000 + ".doc";
                   String agent = request.getHeader("USER-AGENT");
                   // 处理火狐浏览器的编译问题
                   if (agent != null && agent.toLowerCase().indexOf("firefox") > 0) {
                        downloadFileName = "=?UTF-8?B?"
                                  + (new String(Base64.encodeBase64(name
                                           .getBytes("UTF-8")))) + "?=";
                   } else {
                        downloadFileName = java.net.URLEncoder
                                  .encode(name, "UTF-8");
                   }
              } else {
                   TemplateName = "JLWorkDiary.ftl"; // 监理模板
                   name = (map.get("time") == null ? "" : map.get("time"))
                             + "_"
                             + (map.get("username") == null ? "" : map
                                      .get("username")) + "_监理日记_" + Math.random()
                             * 10000 + ".doc";
                   String agent = request.getHeader("USER-AGENT");
                   // 处理火狐浏览器的编译问题
                   if (agent != null && agent.toLowerCase().indexOf("firefox") > 0) {
                        downloadFileName = "=?UTF-8?B?"
                                  + (new String(Base64.encodeBase64(name
                                           .getBytes("UTF-8")))) + "?=";
                   } else {
                        downloadFileName = java.net.URLEncoder
                                  .encode(name, "UTF-8");
                   }
              }
              String baseDir = ParseZip.rootPath() + "/"
                        + PropertiesInfo.get("diaryDir") + "/";
              File baseDirFile = new File(baseDir);
              if (!baseDirFile.exists()) {
                   baseDirFile.mkdirs();
              }
              String filePath = baseDir + name;
              File outFile = new File(filePath); // 保存位置
              resp.setHeader("Content-Disposition", "attachment; filename="
                        + downloadFileName);
               configuration.setServletContextForTemplateLoading(
                        Mvcs.getServletContext(), path);// FTL文件所存在的位置
              Template t = null;
              t = configuration.getTemplate(TemplateName); // 文件名,获取文件模板
              t.setEncoding("UTF-8");
              // outFile.setWritable(false); // 设置成不只读模式
              out = new BufferedWriter(new OutputStreamWriter(
                        new FileOutputStream(outFile), "utf-8"));
              t.process(map, out); // 将值放进文件中,并下载下来
              out.flush();
              return outFile;
          } catch (FileNotFoundException e1) {
              e1.printStackTrace();
              return null;
          } catch (TemplateException e) {
              e.printStackTrace();
              return null;
          } catch (IOException e) {
              e.printStackTrace();
              return null;
          } finally {
              if (out != null) {
                   try {
                        out.close();
                   } catch (IOException e) {
                        log.error("关闭流错误。。。WorkDiaryModule的downloadworkrecord方法");
                   }
              }
          }
     }
posted on 2017-07-19 16:11  古人诚不我欺  阅读(288)  评论(0编辑  收藏  举报