poi导出excel 之工具类(一)

来源:互联网 发布:苹果软件下载大全 编辑:程序博客网 时间:2024/06/06 07:07

实现poi导出excel表格,最方便的就是封装工具类,以便公用,下面直接贴代码,亲测可用。需求差异请适当修改源码!

工具类源码见文尾。。。。。。


代码测试

一、前端请求:

<button type="button" id="downFun" class="btn btn-sm btn-default" title="数据导出"><i class="fa fa-download "></i>导出</button>

<script type="text/javascript"> $("#downFun").click("click", function() {        downFun();  });function downFun() {    location.href = "${ctx}/modules/uws/usergroup/down";}</script>

二、后台代码:

@RequestMapping(value = "/down")@ResponseBodypublic String exprotExcel(@CurrentUser User user, HttpServletResponse response, UserGroup userGroup) {ExcelUtil<UserGroup> excelUtil = new ExcelUtil<UserGroup>();OutputStream out = null;try {out = response.getOutputStream();// 取得输出流// out.flush();response.reset();// 清空输出流response.setHeader("Content-disposition","attachment; filename=" + new String("用户表".getBytes("GB2312"), "8859_1") + ".xls");// 设定输出文件头response.setContentType("application/msexcel");// 定义输出类型} catch (IOException e) {e.printStackTrace();}String[] headers = { "用户组名称", "区域名称" };String[] columns = { "groupName", "areaName" };//准备数据list --根据实际项目RequestBody rqb = new RequestBody();rqb.setQryid("1");rqb.setQrycount("" + Integer.MAX_VALUE);rqb.setSerialNum(LSUtil.getLsNum());rqb.setOpMrg(user.getUsername());List<Qrylist> qrylist = userGroupService.getUserGroupList(rqb).getQrylist();List<UserGroup> uList = new ArrayList<UserGroup>();for (Qrylist qList : qrylist) {UserGroup ug = new UserGroup();ug.setGroupName(qList.getGroupName());uList.add(ug);}try {excelUtil.exportExcel("用户组表", headers, columns, uList, out, "");} catch (Exception e1) {e1.printStackTrace();}try {out.close();} catch (IOException e) {e.printStackTrace();}return null;}

执行后效果图:





源码下载请点击:工具类源码