public String getContent(String code) {
    Template tpl = loadByCode(code);
    if (tpl == null) {
      logger.warn("没有找到编码为'" + code + "'的模板!");
      return null;
    }
    // 纯文本类型
    if (tpl.isPureText()) return tpl.getContentEx();
    // 附件的扩展名
    String extension = StringUtils.getFilenameExtension(tpl.getPath());
    if (tpl.getTemplateType().getCode().equals("word-docx") && extension.equals("docx"))
      return DocxUtils.loadText(tpl.getInputStream());

    if (tpl.getTemplateType().getCode().equals("xls") && extension.equals("xls"))
      return XlsUtils.loadText(tpl.getInputStream());

    if (tpl.getTemplateType().getCode().equals("xlsx") && extension.equals("xlsx"))
      return XlsxUtils.loadText(tpl.getInputStream());

    if (tpl.getTemplateType().getCode().equals("html") && extension.equals("html"))
      return TemplateUtils.loadText(tpl.getInputStream());

    logger.warn("文件后缀名:" + extension + ",未转换为字符串类型");
    return null;
  }
  public Attach getAttach(
      String subject,
      String code,
      Map<String, Object> args,
      String ptype,
      String puid,
      ActorHistory author,
      Map<String, Object> formatParamSql)
      throws Exception {
    Assert.hasText(subject, "subject is Empty");
    Assert.hasText(code, "code is Empty");
    Assert.hasText(ptype, "ptype is Empty");
    Assert.hasText(puid, "puid is Empty");

    Template template = this.templateDao.loadByCode(code);
    if (template == null) throw new CoreException("Template code:" + code + " not find entity!");

    if (args == null) args = new HashMap<String, Object>();

    if (author == null) {
      if (SystemContextHolder.get() == null) {
        author = this.actorHistoryService.loadByCode("admin");
      } else {
        author = SystemContextHolder.get().getUserHistory();
      }
    }

    Map<String, Object> args4Param = this.getMapParams(template.getId(), formatParamSql);

    if (args4Param != null)
      // 最终替换参数
      args.putAll(this.getMapParams(template.getId(), formatParamSql));

    // 生成附件
    Attach attach = new Attach();
    attach.setAuthor(author);
    attach.setFileDate(Calendar.getInstance());
    attach.setSubject(subject);
    attach.setAppPath(false);
    attach.setFormat(template.getTemplateType().getExtension());
    attach.setStatus(BCConstants.STATUS_ENABLED);
    attach.setPtype(ptype);
    attach.setPuid(puid);

    // 文件存储的相对路径(年月),避免超出目录内文件数的限制
    Calendar now = Calendar.getInstance();
    String datedir = new SimpleDateFormat("yyyyMM").format(now.getTime());

    // 要保存的物理文件
    String realpath; // 绝对路径名
    // uuid
    String fileName =
        UUID.randomUUID().toString().replace("-", "")
            + "."
            + template.getTemplateType().getExtension(); // 不含路径的文件名
    realpath = Attach.DATA_REAL_PATH + "/" + datedir + "/" + fileName;

    // 构建文件要保存到的目录
    File file = new File(realpath);
    if (!file.getParentFile().exists()) {
      if (logger.isInfoEnabled()) {
        logger.info("mkdir=" + file.getParentFile().getAbsolutePath());
      }
      file.getParentFile().mkdirs();
    }

    OutputStream out = new BufferedOutputStream(new FileOutputStream(file));

    this.formatTo(template.getCode(), args, out);

    // 设置附件大小
    attach.setSize(new File(realpath).length());

    // 设置附件相对路径
    attach.setPath(datedir + "/" + fileName);

    return this.attachService.save(attach);
  }
  public void formatTo(String code, Map<String, Object> args, OutputStream out) {
    Template tpl = loadByCode(code);
    if (tpl == null) logger.warn("没有找到编码为'" + code + "'的模板!");
    if (tpl.isFormatted()) logger.warn("code=" + code + ",模板不可格式化。");

    // 纯文本类型
    if (tpl.isPureText()) {
      String source = this.getContent(code);
      String r = TemplateUtils.format(source, args);
      try {
        out.write(r.getBytes());
        out.flush();
      } catch (IOException e) {
        logger.warn("formatTo 写入数据到流错误:" + e.getMessage());
      } finally {
        try {
          out.close();
        } catch (IOException ex) {
        }
      }
    } else {
      InputStream is = tpl.getInputStream();

      // 附件的扩展名
      String extension = StringUtils.getFilenameExtension(tpl.getPath());

      if ("word-docx".equals(tpl.getTemplateType().getCode()) && "docx".equals(extension)) {
        XWPFDocument docx = DocxUtils.format(is, args);
        try {
          docx.write(out);
          out.flush();
        } catch (IOException e) {
          logger.warn("formatTo 写入数据到流错误:" + e.getMessage());
        } finally {
          try {
            is.close();
            out.close();
          } catch (IOException ex) {
          }
        }

      } else if ("xls".equals(tpl.getTemplateType().getCode()) && "xls".equals(extension)) {
        HSSFWorkbook xls = XlsUtils.format(is, args);
        try {
          xls.write(out);
          out.flush();
        } catch (IOException e) {
          e.printStackTrace();
          logger.warn("formatTo 写入数据到流错误:" + e.getMessage());
        } finally {
          try {
            is.close();
            out.close();
          } catch (IOException ex) {
          }
        }

      } else if ("xlsx".equals(tpl.getTemplateType().getCode())
          && "xlsx".equals(extension)) { // Excel2007+

        XSSFWorkbook xlsx = XlsxUtils.format(is, args);
        try {
          xlsx.write(out);
          out.flush();
        } catch (IOException e) {
          e.printStackTrace();
          logger.warn("formatTo 写入数据到流错误:" + e.getMessage());
        } finally {
          try {
            is.close();
            out.close();
          } catch (IOException ex) {
          }
        }

      } else if ("html".equals(tpl.getTemplateType().getCode())
          && "html".equals(extension)) { // html
        String source = FreeMarkerUtils.format(TemplateUtils.loadText(is), args);
        try {
          out.write(source.getBytes());
          out.flush();
        } catch (IOException e) {
          e.printStackTrace();
          logger.warn("formatTo 写入数据到流错误:" + e.getMessage());
        } finally {
          try {
            is.close();
            out.close();
          } catch (IOException ex) {
          }
        }
      } else {
        logger.warn("文件后缀名:" + extension + ",不能formatTo");
        try {
          is.close();
          out.close();
        } catch (IOException ex) {
        }
      }
    }
  }