java导出excel报表
1、使用的是ssm框架,maven构建项目
2、使用jxls
3、maven依赖:
p.p1 { margin: 0; font: 11px Monaco; color: rgba(78, 145, 146, 1) }
p.p2 { margin: 0; font: 11px Monaco }
p.p3 { margin: 0; font: 11px Monaco; min-height: 15px }
span.s1 { color: rgba(0, 145, 147, 1) }
span.s2 { color: rgba(78, 145, 146, 1) }
span.s3 { color: rgba(0, 0, 0, 1) }
span.s4 { text-decoration: underline; color: rgba(0, 0, 0, 1) }
<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、代码实现
p.p1 { margin: 0; font: 11px Monaco; color: rgba(119, 119, 119, 1) }
p.p2 { margin: 0; font: 11px Monaco }
p.p3 { margin: 0; font: 11px Monaco; min-height: 15px }
p.p4 { margin: 0; font: 11px Monaco; color: rgba(57, 51, 255, 1) }
p.p5 { margin: 0; font: 11px Monaco; color: rgba(126, 80, 79, 1) }
span.s1 { color: rgba(0, 0, 0, 1) }
span.s2 { color: rgba(57, 51, 255, 1) }
span.s3 { color: rgba(147, 26, 104, 1) }
span.s4 { text-decoration: underline }
span.s5 { color: rgba(126, 80, 79, 1) }
span.s6 { text-decoration: underline; color: rgba(57, 51, 255, 1) }
span.s7 { text-decoration: underline; color: rgba(126, 80, 79, 1) }
span.s8 { color: rgba(78, 144, 114, 1) }
span.Apple-tab-span { white-space: pre }
@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表达式实现