用模板导出Excel文件 然后需要把这个Excel文件作为邮件附件自动发送邮件 怎么添加附件

qq_41269651 2017-12-27 05:08:17
我:
/**
* 解析模板生成Excel stream
* @param templateName 模板名称
* @param data 数据参数
* @throws IOException
* @throws TemplateException
*/
public static ByteArrayOutputStream parse(String templateName,Map<String,Object> data) throws IOException, TemplateException {
//初始化工作
Configuration cfg = new Configuration();
//设置默认编码格式为UTF-8
cfg.setDefaultEncoding(Constants.DEFAULT_URL_ENCODING);
//全局数字格式
cfg.setNumberFormat("0.00");
//指定模板路径
org.springframework.core.io.Resource resource =new DefaultResourceLoader().getResource(TEMPLATE_PATH);
// //设置模板文件位置
cfg.setDirectoryForTemplateLoading(resource.getFile());
cfg.setObjectWrapper(new DefaultObjectWrapper());
// //加载模板
Template template = cfg.getTemplate(templateName,Constants.DEFAULT_URL_ENCODING);
ByteArrayOutputStream outStream = null;

Writer out = null;
try{
outStream = new ByteArrayOutputStream();
out = new OutputStreamWriter(outStream,Constants.DEFAULT_URL_ENCODING);
template.process(data, out);
return outStream;
}finally{
out.close();
}
}
工具类
我:
/**
* 导出excle统一处理
* @param resultList 需要导出exlce的数据
* @param templateName excle模版的名字
* @param excleName excel的文件名
* @throws Exception
*/
protected void processExcel(List<?> resultList,String templateName,String excleName) throws Exception{
Map<String,Object> data = new HashMap<String, Object>();
String officialDate = request.getParameter("officialDate");
data.put("resultList", resultList);

try {
ByteArrayOutputStream baos = null;
baos = TemplateParseUtil.parse(templateName, data);
if(StringUtil.isBlank(officialDate)){
officialDate=Constants.All;
}else if(officialDate.length()>4){//部分报表的 开始时间只有年份
officialDate=DateUtil.stringToString(officialDate, PubConsts.DATE_FORMAT,PubConsts.YYYY_MM_DD_LONG_TIME);
}
String fileName = excleName+officialDate+Constants.XLS;//导出excel的文件名
response.setHeader("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes(),"iso-8859-1"));
ServletOutputStream out = response.getOutputStream();
out.write(baos.toByteArray());
out.flush();
out.close();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TemplateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我:
basecontroller
我:
/**
* Excel文件的生成
*/
@RequestMapping("/typeExportExcel")
public void exportExcel()throws Exception{
String officialDate=null;
String signDate=null;
if(null!=request.getParameter("officialDate")){
officialDate=request.getParameter("officialDate");
}else if(null!=request.getParameter("signDate")){
officialDate="1000-01-01";
}

if(null!=request.getParameter("signDate")){
signDate=request.getParameter("signDate");
}else if(null!=request.getParameter("officialDate")){
signDate="9999-12-31";
}

ServiceTypeVO ddeptVo=new ServiceTypeVO();
ddeptVo.setSignDate(signDate);
ddeptVo.setOfficialDate(officialDate);
List<ServiceTypePO> resultList = ServiceTypeService.getQueryList(ddeptVo);
String templateName ="ServiceType.ftl";
String excleName=Constants.SAL_DEP_TYPE;
this.processExcel(resultList, templateName, excleName);
}
controller层
想用spring发送邮件功能 然后添加附件时候 需要附件路径 名字 请问怎么获取
...全文
709 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
tianfang 2017-12-29
  • 打赏
  • 举报
回复
分解问题:文件是否已经可以正确生成?放在服务器的什么地方? 系统中有邮件模块吗?有demo吗
qq_41269651 2017-12-29
  • 打赏
  • 举报
回复
Excel文件可以正常生成 在浏览器上能够下载 就是找不到放在服务器的什么地方

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧