首页 > 编程语言 > 详细

java导出excel报表

时间:2018-06-06 10:23:36      阅读:258      评论:0      收藏:0      [点我收藏+]

1、使用的是ssm框架,maven构建项目

2、使用jxls

3、maven依赖:

<dependency>

    <groupId>net.sf.jxls</groupId>

    <artifactId>jxls-core</artifactId>

    <version>1.0.5</version>

   </dependency>

 

    <dependency>

      <groupId>net.sf.jxls</groupId>

      <artifactId>jxls-reader</artifactId>

      <version>1.0.5</version>

    </dependency>

4、代码实现

@RequestMapping("/alarmTopdf")

@ResponseBody

public AjaxResult alarmTopdf(HttpServletRequest req,HttpServletResponse response) throws OprException, ParseException{

AjaxResult ajaxResult = new AjaxResult();

List<RealData> list = (List<RealData>) SecurityUtils.getSubject().getSession().getAttribute("lm");

HashMap map = (HashMap) SecurityUtils.getSubject().getSession().getAttribute("pmap");

HashMap beans = new HashMap();   

String fileFlag = String.valueOf(System.currentTimeMillis());

String dataInfo = "";

 

String path = req.getSession().getServletContext().getRealPath("/"); 

String userInfo = null;

String templateDir = path+"/excel-template/historyalarm.xls";//

SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");

fileFlag = sf.format(new Date());

String outPutDir = path+"/outfile/报表告警数据"+fileFlag+".xls";

InputStream  inputStream = null;

 

XLSTransformer xls = new XLSTransformer();

beans.put("list", list);

beans.put("map", map);

try {

xls.transformXLS(templateDir,beans,outPutDir);

userInfo = "/outfile/报表告警数据"+fileFlag+".xls";

} catch (ParsePropertyException e) {

e.printStackTrace();

} catch (InvalidFormatException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

AjaxResult result = ajaxResult.success(userInfo);

return result;

}

 

分析

技术分享图片

在做这个的时候 遇到一个坑:一定要有这个的,不然是不会出现excel文件的

技术分享图片

 excel模板:jstl表达式实现

技术分享图片

 

java导出excel报表

原文:https://www.cnblogs.com/haoxiu1004/p/9143231.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!