首页 > 代码库 > java 导出EXCEL

java 导出EXCEL

!!!声明 文中用到的util 请参见百度云 http://pan.baidu.com/s/1mibbL8W  如已失效请加qq 1940978083

1、添加html

<form action="<%=path%>/black/exportExcel.html"    //此处后台地址onsubmit="return checkUser();"><div class="panel-heading"><div class="row"><div class="col-lg-3 col-md-3 col-xs-3"style="margin-right: 0px;padding-right: 0px;"><form class="form-inline"><div class="form-group"><div class="input-group"><div class="input-group-addon">黑名单号码</div><input type="text" class="form-control input-sm" id="name"name="blackname"></div></div></form>

  

2、

@RequestMapping("exportExcel")public void exportExcel(HttpServletRequest req, HttpServletResponse res) throws Exception {Domain domain = (Domain) req.getSession().getAttribute("Domainlogin");String name = req.getParameter("blackname");if (name == null) {name = "";}List<Blacklist> list = blacklistservice.findAllNumber(domain.getDomainid(), name);res.reset();res.setContentType("application/vnd.ms-excel;charset=utf-8");res.setContentType("application/x-xls");// 定义输出类型String date = DateUtil.dateToStringT(new Date());res.setHeader("Content-disposition", "attachment;filename=" + date + ".xls");ExcelExportUtil<Blacklist> exportUtil = new ExcelExportUtil<Blacklist>();String[] headers = { "id", "创建时间", "黑名单号码" };   String[] columns = { "id", "createtime", "number" };OutputStream outputStream = res.getOutputStream();exportUtil.expoortExcelx("黑名单", headers, columns, list, outputStream, "yyyy-MM-dd HH:mm:ss");outputStream.flush();outputStream.close();}

解释   headers 是导出excel的列名    columns 是对应的数据库的字段      

 

java 导出EXCEL