html代码
<a class="btn btn-primary fa fa-export" href="javascript:void(0);" onclick="expExcel()"><span>导出excel</span></a>
js代码
function expExcel() { var ids=new Array(); $("input:checkbox:checked").each(function(){ ids.push($(this).attr("id")); }); if(ids.length==0){ alert("请至少选中一条记录"); return; } if (confirm("导出选中页面数据?")) { var url = __ctx + '/platform/xxx/xxx/exportExcel.htm?myId='+ ids; var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串 if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1) {//判断是否IE浏览器 window.location.href(url); } else { window.open(url, "导出报表"); } } }
java代码1
@RequestMapping({ "exportExcel" }) public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws Exception { SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); String[] aryIds = RequestUtil.getStringAryByStr(request, "myId"); QueryFilter queryFilter = getQuerFilter(request); String myIds = ""; for (int i = 0; i < aryIds.length; i++) { String tmp = aryIds[i].replace("jqg_jzjxjhGrid_", ""); if (i != aryIds.length - 1) { myIds += "'" + tmp + "',"; } else { myIds += "'" + tmp + "'"; } } queryFilter.addParamsFilter("whereSql", "MY_ID_ in (" + myIds + ")"); queryFilter.addParamsFilter("orderBySql", "KS ASC"); List<Jzjxjh> query = jzjxjhManager.query(queryFilter); String title = "xxx计划"; String[] headers = {"序号","xx","xx","xx","开始","结束","工期","xx","发电类型"}; Integer[] columnWidth={18,18,18,18,18,18,18,18,18}; String[][] values = new String[query.size()][headers.length]; for (int i = 0; i < query.size(); i++) { Jzjxjh e = query.get(i); values[i][0]=e.getXh()+""; values[i][1]=e.getDw(); values[i][2]=e.getJz(); values[i][3]=e.getRl()+""; values[i][4]=sdf.format(e.getKs()); values[i][5]=sdf.format(e.getJs()); values[i][6]=e.getGq()+""; values[i][7]=e.getLx(); values[i][8]=e.getZt(); } HSSFWorkbook wb = ExportExcelUtil.getHSSFWorkbook(title, headers, values,columnWidth); String fileName = "xxxx计划_" + ".xls"; ExportExcelUtil.exportExcel(request, response, wb, fileName); }
java代码2(跟上面1差不多,优点用到了反射类工具类,不用一个个get实体类的数据方便快捷,该工具类也可以从本博客util标签中找)
@RequestMapping({ "exportExcel" }) public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws Exception { String[] aryIds = RequestUtil.getStringAryByStr(request, "myId"); QueryFilter queryFilter = getQuerFilter(request); String myIds = ""; for (int i = 0; i < aryIds.length; i++) { String tmp = aryIds[i].replace("jqg_jzjxjhGrid_", ""); if (i != aryIds.length - 1) { myIds += "'" + tmp + "',"; } else { myIds += "'" + tmp + "'"; } } queryFilter.addParamsFilter("whereSql", "MY_ID_ in (" + myIds + ")"); queryFilter.addParamsFilter("orderBySql", "KS ASC"); List<Jzjxjh> query = jzjxjhManager.query(queryFilter); String title = "xxx计划"; String[] headers = {"序号","单位","机组","容量","开始","结束","工期","类型","发电类型"}; Integer[] columnWidth={18,18,18,18,18,18,18,18,18}; String[] headersVar = {"xh","dw","jz","rl","ks","js","gq","lx","zt"}; String[][] values = new String[query.size()][headers.length]; for (int i = 0; i < query.size(); i++) { Jzjxjh obj = query.get(i); for (int j = 0; j < headersVar.length; j++) { values[i][j]=ReflectUtil.getStringValue(headersVar[j], obj, "yyyy/MM/dd"); } } HSSFWorkbook wb = ExportExcelUtil.getHSSFWorkbook(title, headers, values,columnWidth); String fileName = "xxx计划_" + ".xls"; ExportExcelUtil.exportExcel(request, response, wb, fileName); }
适用场景:poi 4.0.1 ,ExportExcelUtil.java可以在本博客util中找
导出效果
第一行是合并单元格的一个标题
第二行是表头
第三行开始每一列都是数据
效果图如下