页面js:
// 导出button $exportOrderListBtn.unbind("click").bind("click",function(){ // 地区 var areaId = $("#areaIdSel").val(); // 稽核类型 var auditType = $("#auditTypeSel").val(); // 开始时间 var beginDate = $("#beginDate").val(); // 结束时间 var endDate = $("#endDate").val(); // 清单类型 var listType = $("#listTypeSel").val(); // 订单状态 var olStateCd = $("#olStateCdSel").val(); // 稽核人员 var auditStaffId = $("#auditStaffId").val(); // 受理渠道 var channelId = $("#channelId").val(); // 受理员工 var acceptStaffId = $("#acceptStaffId").val(); var tipMsg = ""; if(beginDate==null || beginDate.length==0){ tipMsg += "'开始时间'不能为空!<br/>"; } if(endDate==null || endDate.length==0){ tipMsg += "'结束时间'不能为空!"; } if(tipMsg!=null && tipMsg.length>0){ parent.Public.tips({type:1,content:tipMsg}); return; } $.dialog.confirm("将导出报表结果,确认导出?", function() { parent.location.href = url + "/auditReport/exportTwoLevelAuditOrderList?areaId="+areaId+"&auditType="+auditType+"&beginDate="+beginDate+"&endDate="+endDate+"&olStateCd="+olStateCd+"&channelId="+channelId+"&acceptStaffId="+acceptStaffId+"&staffId="+auditStaffId; }); });
后端java:
Controller的action方法:
/** * 导出二级稽核清单报表 */ public void exportTwoLevelAuditOrderList() throws Exception{ Map<String, Object> paramMap = this.getParaKeyAndValue(); HttpServletResponse response = this.getResponse(); response.setContentType("octets/stream"); String fileName = "二级稽核清单报表结果"; response.addHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes("gb2312"),"ISO-8859-1")+".zip"); OutputStream out = response.getOutputStream(); ZipOutputStream zip = new ZipOutputStream(out); // 查询结果 LoginedStaffInfo lsf = getUserBeanInfo(); paramMap.put("auditStaffTwoId", lsf.getStaffId()); int startNum= 1; // 起始值 int endNum = 2000; // 结束值 int excelRowNum = 2000; // excel行数 List<TwoLevelAuditOrderListReportObj> orderList = null; while(true){ List<Record> list = AuditReportDao.getTwoLevelAuditOrderList(startNum, endNum, paramMap, lsf); if(startNum>1 && CollectionUtils.isEmpty(list)){ break; } orderList = new ArrayList<TwoLevelAuditOrderListReportObj>(); for(Record record : list) { TwoLevelAuditOrderListReportObj item = new TwoLevelAuditOrderListReportObj(); item.convert(record); orderList.add(item); } ExportUtils<TwoLevelAuditOrderListReportObj> exportUtils = new ExportUtils<TwoLevelAuditOrderListReportObj>(); String title = "二级稽核清单报表结果"; String[] headers = {"购物车ID","购物车流水","接入号","客户名称","受理渠道","受理员工","订单状态","稽核类型","地区","域"}; String dataPattern = "yyyy-MM-dd"; exportUtils.exportExcelZip(title, headers, orderList, zip, dataPattern,startNum,endNum); startNum = startNum+excelRowNum; endNum = endNum+excelRowNum; } zip.close(); this.renderNull(); }
Dao方法:
/** * 二级稽核 -> 清单报表 导出 * <br/> 最多2000条 */ public static List<Record> getTwoLevelAuditOrderList(int startNum,int endNum,Map<String,Object> paramMap,LoginedStaffInfo lsf){ List<Object> paramList = new ArrayList<Object>(); // 参数list StringBuffer selectSb = new StringBuffer(); selectSb.append("select *"); selectSb.append("from (select row_.*, rownum rownum_ from ("); selectSb.append("select aol.ol_id as olId,"); selectSb.append("aol.ol_nbr as olNbr,"); selectSb.append("aol.access_number as accessNumber,"); selectSb.append("(select p.name from party p where p.party_id=aol.party_id and p.domain = aol.domain) as partyName,"); selectSb.append("(select c.name from channel c where c.channel_id=aol.channel_id and c.domain = aol.domain) as channelName,"); selectSb.append("(select sn.name||'['||sn.staff_number||']' from staff_number sn where sn.staff_id=aol.staff_id and sn.domain = aol.domain) as staffName,"); selectSb.append("(select ols.name from order_list_state ols where ols.ol_state_cd=aol.ol_state_cd) as olStateName,"); selectSb.append("(select os.p_name from item_spec os where os.spec_id=9001 and os.p_code=aol.marking) as marking,"); selectSb.append("aol.ol_state_cd as olStateCd,"); selectSb.append("aol.action_type_name as actionTypeName,"); selectSb.append("pc.staff_id as auditId,"); selectSb.append("(select sn.name||'['||sn.staff_number||']' from staff_number sn where sn.staff_id=pc.staff_id and sn.domain = aol.domain) as auditName,"); selectSb.append("decode((select count(1) from ol_2_es es where es.ol_id=aol.ol_id),0,0,1) as checkType,"); selectSb.append("(select name from area where area_id=aol.area_id) as areaName,"); selectSb.append("aol.marking as mark,"); selectSb.append("aol.domain as domain"); selectSb.append(" from audit_order_list aol, party_2_channel pc"); selectSb.append(" where aol.channel_id = pc.channel_id"); if(paramMap.get("olStateCd") != null) { if(StringUtils.equals(paramMap.get("olStateCd").toString(), "13")){ selectSb.append(" and aol.ol_state_cd in (6,7)"); }else if(StringUtils.equals(paramMap.get("olStateCd").toString(), "23")){ selectSb.append(" and aol.ol_state_cd in (5,10,8)"); }else{ selectSb.append(" and aol.ol_state_cd=?"); // 参数 paramList.add(paramMap.get("olStateCd").toString()); } } if(paramMap.get("staffId") != null) { selectSb.append(" and pc.staff_id = ?"); // 参数 paramList.add(paramMap.get("staffId").toString()); } if(paramMap.get("auditStaffTwoId")!=null){ selectSb.append(" and pc.staff_id in (select pp.party_id from party_2_party pp where pp.audit_party_id=?)"); // 参数 paramList.add(paramMap.get("auditStaffTwoId").toString()); } if(paramMap.get("olNbr")!=null){ selectSb.append(" and aol.ol_nbr=?"); // 参数 paramList.add(paramMap.get("olNbr").toString()); } if(paramMap.get("channelId") != null){ selectSb.append(" and aol.channel_id=?"); // 参数 paramList.add(paramMap.get("channelId").toString()); } if(paramMap.get("acceptStaffId") != null){ selectSb.append(" and aol.staff_id=?"); // 参数 paramList.add(paramMap.get("acceptStaffId").toString()); } if(paramMap.get("auditType") != null) { selectSb.append(" and aol.marking = ?"); // 参数 paramList.add(paramMap.get("auditType").toString()); } if(!StaffDao.isHaveProvinceConfigRole(lsf.getStaffId())){ selectSb.append(" and substr(aol.area,0,3)=substr(?,0,3)"); // 参数 paramList.add(lsf.getAreaId()); } if(paramMap.get("areaId")!=null && !StringUtils.equals(paramMap.get("areaId").toString(), "2")) { selectSb.append(" and substr(aol.area,0,3)= substr(?,0,3)"); // 参数 paramList.add(paramMap.get("areaId").toString()); } if(paramMap.get("beginDate") != null) { selectSb.append(" and aol.so_date > to_date(?, 'yyyy-mm-dd')"); // 参数 paramList.add(paramMap.get("beginDate").toString()); } if(paramMap.get("endDate") != null) { selectSb.append(" and aol.so_date < to_date(?, 'yyyy-mm-dd') + 1"); // 参数 paramList.add(paramMap.get("endDate").toString()); } selectSb.append(" order by aol.ol_id"); selectSb.append(") row_"); selectSb.append(" where rownum <= "+ endNum +") table_alias"); selectSb.append(" where table_alias.rownum_ >= "+ startNum); Object[] params = paramList.toArray(); return Db.find(selectSb.toString(),params); }
excel导出公共类:
import java.io.IOException; import java.io.OutputStream; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.text.SimpleDateFormat; import java.util.Collection; import java.util.Date; import java.util.Iterator; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFClientAnchor; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFRichTextString; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor; /** * @ClassName ExportExcelUtils * @ClassDescription 导出excel帮助类 */ public class ExportUtils<T> { /** * 单个工作薄流 excel文件输出 * @param title 表格标题名 * @param headers 表格属性列名数组 * @param dataset 需要显示的数据集合,集合中一定要放置符合JavaBean风格的类的对象。 * @param out 输出流对象 * @param pattern 如果有时间数据,设定输出格式。默认为"yyyy-MM-dd" */ public void exportExcel(String title, String[] headers, Collection<T> dataset, OutputStream out, String pattern) { // 声明一个工作薄 HSSFWorkbook workbook = new HSSFWorkbook(); // 生成一个表格 HSSFSheet sheet = workbook.createSheet(title); // 设置表格默认列宽度为20个字节 sheet.setDefaultColumnWidth((short) 20); // 生成一个样式 HSSFCellStyle style = workbook.createCellStyle(); // 设置这些样式 style.setFillForegroundColor(HSSFColor.SKY_BLUE.index); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style.setBorderBottom(HSSFCellStyle.BORDER_THIN); style.setBorderLeft(HSSFCellStyle.BORDER_THIN); style.setBorderRight(HSSFCellStyle.BORDER_THIN); style.setBorderTop(HSSFCellStyle.BORDER_THIN); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 生成一个字体 HSSFFont font = workbook.createFont(); font.setColor(HSSFColor.VIOLET.index); font.setFontHeightInPoints((short) 12); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 把字体应用到当前的样式 style.setFont(font); // 生成并设置另一个样式 HSSFCellStyle style2 = workbook.createCellStyle(); style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index); style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style2.setBorderBottom(HSSFCellStyle.BORDER_THIN); style2.setBorderLeft(HSSFCellStyle.BORDER_THIN); style2.setBorderRight(HSSFCellStyle.BORDER_THIN); style2.setBorderTop(HSSFCellStyle.BORDER_THIN); style2.setAlignment(HSSFCellStyle.ALIGN_CENTER); style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 生成另一个字体 HSSFFont font2 = workbook.createFont(); font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); // 把字体应用到当前的样式 style2.setFont(font2); // // 声明一个画图的顶级管理器 // HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); // // 定义注释的大小和位置,详见文档 // HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0, // 0, 0, 0, (short) 4, 2, (short) 6, 5)); // // 设置注释内容 // comment.setString(new HSSFRichTextString("可以在POI中添加注释!")); // // 设置注释作者,当鼠标移动到单元格上是可以在状态栏中看到该内容. // comment.setAuthor("leno"); // 产生表格标题行 HSSFRow row = sheet.createRow(0); for (short i = 0; i < headers.length; i++) { HSSFCell cell = row.createCell(i); cell.setCellStyle(style); HSSFRichTextString text = new HSSFRichTextString(headers[i]); cell.setCellValue(text); } // 遍历集合数据,产生数据行 Iterator<T> it = dataset.iterator(); int index = 0; HSSFFont font3 = workbook.createFont(); font3.setColor(HSSFColor.BLUE.index); while (it.hasNext()) { index++; row = sheet.createRow(index); T t = (T) it.next(); // 利用反射,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值 Field[] fields = t.getClass().getDeclaredFields(); for (short i = 0; i < fields.length; i++) { HSSFCell cell = row.createCell(i); cell.setCellStyle(style2); Field field = fields[i]; String fieldName = field.getName(); String getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); try { Class tCls = t.getClass(); Method getMethod = tCls.getMethod(getMethodName, new Class[] {}); Object value = getMethod.invoke(t, new Object[] {}); // 判断值的类型后进行强制类型转换 String textValue = null; if (value instanceof Boolean) { boolean bValue = (Boolean) value; textValue = "男"; if (!bValue) { textValue = "女"; } } else if (value instanceof Date) { Date date = (Date) value; SimpleDateFormat sdf = new SimpleDateFormat(pattern); textValue = sdf.format(date); } else if (value instanceof byte[]) { // 有图片时,设置行高为60px; row.setHeightInPoints(60); // 设置图片所在列宽度为80px,注意这里单位的一个换算 sheet.setColumnWidth(i, (short) (35.7 * 80)); // sheet.autoSizeColumn(i); byte[] bsValue = (byte[]) value; HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 1023, 255, (short) 6, index, (short) 6, index); anchor.setAnchorType(2); // patriarch.createPicture(anchor, workbook.addPicture( // bsValue, HSSFWorkbook.PICTURE_TYPE_JPEG)); } else { // 其它数据类型都当作字符串简单处理 if(value!=null){ textValue = value.toString(); } } if("ruleContent".equals(fieldName)){//规则内容,列宽设置为15000 sheet.setColumnWidth(i,15000); } // 如果不是图片数据,就利用正则表达式判断textValue是否全部由数字组成 if (textValue != null) { Pattern p = Pattern.compile("^//d+(//.//d+)?$"); Matcher matcher = p.matcher(textValue); if (matcher.matches()) { // 是数字当作double处理 cell.setCellValue(Double.parseDouble(textValue)); } else { HSSFRichTextString richString = new HSSFRichTextString(textValue); richString.applyFont(font3); cell.setCellValue(richString); } } } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } finally { // 清理资源 } } } try { workbook.write(out); out.flush(); } catch (IOException e) { e.printStackTrace(); } } /** * 多个excel工作薄流 zip文件输出 * @param title 表格标题名 * @param headers 表格属性列名数组 * @param dataset 需要显示的数据集合,集合中一定要放置符合JavaBean风格的类的对象。 * @param out 输出流对象 * @param pattern 如果有时间数据,设定输出格式。默认为"yyyy-MM-dd" * @param startNum 起始值 * @param endNum 结束值 */ @SuppressWarnings("finally") public void exportExcelZip(String title, String[] headers, Collection<T> dataset, ZipOutputStream zip, String pattern,int startNum,int endNum) { // 声明一个工作薄 HSSFWorkbook workbook = new HSSFWorkbook(); // 生成一个表格 HSSFSheet sheet = workbook.createSheet(title); // 设置表格默认列宽度为20个字节 sheet.setDefaultColumnWidth((short) 20); // 生成一个样式 HSSFCellStyle style = workbook.createCellStyle(); // 设置这些样式 style.setFillForegroundColor(HSSFColor.SKY_BLUE.index); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style.setBorderBottom(HSSFCellStyle.BORDER_THIN); style.setBorderLeft(HSSFCellStyle.BORDER_THIN); style.setBorderRight(HSSFCellStyle.BORDER_THIN); style.setBorderTop(HSSFCellStyle.BORDER_THIN); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 生成一个字体 HSSFFont font = workbook.createFont(); font.setColor(HSSFColor.VIOLET.index); font.setFontHeightInPoints((short) 12); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 把字体应用到当前的样式 style.setFont(font); // 生成并设置另一个样式 HSSFCellStyle style2 = workbook.createCellStyle(); style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index); style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style2.setBorderBottom(HSSFCellStyle.BORDER_THIN); style2.setBorderLeft(HSSFCellStyle.BORDER_THIN); style2.setBorderRight(HSSFCellStyle.BORDER_THIN); style2.setBorderTop(HSSFCellStyle.BORDER_THIN); style2.setAlignment(HSSFCellStyle.ALIGN_CENTER); style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 生成另一个字体 HSSFFont font2 = workbook.createFont(); font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); // 把字体应用到当前的样式 style2.setFont(font2); // // 声明一个画图的顶级管理器 // HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); // // 定义注释的大小和位置,详见文档 // HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0, // 0, 0, 0, (short) 4, 2, (short) 6, 5)); // // 设置注释内容 // comment.setString(new HSSFRichTextString("可以在POI中添加注释!")); // // 设置注释作者,当鼠标移动到单元格上是可以在状态栏中看到该内容. // comment.setAuthor("leno"); // 产生表格标题行 HSSFRow row = sheet.createRow(0); for (short i = 0; i < headers.length; i++) { HSSFCell cell = row.createCell(i); cell.setCellStyle(style); HSSFRichTextString text = new HSSFRichTextString(headers[i]); cell.setCellValue(text); } // 遍历集合数据,产生数据行 Iterator<T> it = dataset.iterator(); int index = 0; // 字体 HSSFFont font3 = workbook.createFont(); font3.setColor(HSSFColor.BLUE.index); while (it.hasNext()) { index++; row = sheet.createRow(index); T t = (T) it.next(); // 利用反射,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值 Field[] fields = t.getClass().getDeclaredFields(); for (short i = 0; i < fields.length; i++) { HSSFCell cell = row.createCell(i); cell.setCellStyle(style2); Field field = fields[i]; String fieldName = field.getName(); String getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1); try { Class tCls = t.getClass(); Method getMethod = tCls.getMethod(getMethodName, new Class[] {}); Object value = getMethod.invoke(t, new Object[] {}); // 判断值的类型后进行强制类型转换 String textValue = null; if (value instanceof Boolean) { boolean bValue = (Boolean) value; textValue = "男"; if (!bValue) { textValue = "女"; } } else if (value instanceof Date) { Date date = (Date) value; SimpleDateFormat sdf = new SimpleDateFormat(pattern); textValue = sdf.format(date); } else if (value instanceof byte[]) { // 有图片时,设置行高为60px; row.setHeightInPoints(60); // 设置图片所在列宽度为80px,注意这里单位的一个换算 sheet.setColumnWidth(i, (short) (35.7 * 80)); // sheet.autoSizeColumn(i); byte[] bsValue = (byte[]) value; HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 1023, 255, (short) 6, index, (short) 6, index); anchor.setAnchorType(2); // patriarch.createPicture(anchor, workbook.addPicture( // bsValue, HSSFWorkbook.PICTURE_TYPE_JPEG)); } else { // 其它数据类型都当作字符串简单处理 if(value!=null){ textValue = value.toString(); } } if("ruleContent".equals(fieldName)){//规则内容,列宽设置为15000 sheet.setColumnWidth(i,15000); } // 如果不是图片数据,就利用正则表达式判断textValue是否全部由数字组成 if (textValue != null) { Pattern p = Pattern.compile("^//d+(//.//d+)?$"); Matcher matcher = p.matcher(textValue); if (matcher.matches()) { // 是数字当作double处理 cell.setCellValue(Double.parseDouble(textValue)); } else { HSSFRichTextString richString = new HSSFRichTextString(textValue); richString.applyFont(font3); cell.setCellValue(richString); } } } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } finally { // 清理资源 } } } try { ZipEntry entry = new ZipEntry(startNum+"~"+endNum+".xls"); zip.putNextEntry(entry); workbook.write(zip); zip.flush(); } catch (IOException e) { e.printStackTrace(); } } }
导出实体类:
import com.jfinal.plugin.activerecord.Record; /** * @ClassName TwoLevelAuditOrderListReportObj * @ClassDescription 二级稽核清单报表导出对象 */ public class TwoLevelAuditOrderListReportObj { /** 购物车ID */ private Long olId; /** 购物车流水 */ private String olNbr; /** 接入号 */ private String accessNumber; /** 客户名称 */ private String partyName; /** 受理渠道 */ private String channelName; /** 受理员工 */ private String staffName; /** 订单状态 */ private String olStateName; /** 稽核类型 */ private String marking; /** 地区 */ private String areaName; /** 域 */ private String domain; public TwoLevelAuditOrderListReportObj(){ } public Long getOlId() { return olId; } public void setOlId(Long olId) { this.olId = olId; } public String getOlNbr() { return olNbr; } public void setOlNbr(String olNbr) { this.olNbr = olNbr; } public String getAccessNumber() { return accessNumber; } public void setAccessNumber(String accessNumber) { this.accessNumber = accessNumber; } public String getPartyName() { return partyName; } public void setPartyName(String partyName) { this.partyName = partyName; } public String getChannelName() { return channelName; } public void setChannelName(String channelName) { this.channelName = channelName; } public String getStaffName() { return staffName; } public void setStaffName(String staffName) { this.staffName = staffName; } public String getOlStateName() { return olStateName; } public void setOlStateName(String olStateName) { this.olStateName = olStateName; } public String getMarking() { return marking; } public void setMarking(String marking) { this.marking = marking; } public String getAreaName() { return areaName; } public void setAreaName(String areaName) { this.areaName = areaName; } public String getDomain() { return domain; } public void setDomain(String domain) { this.domain = domain; } public TwoLevelAuditOrderListReportObj convert(Record record){ this.olId = record.getBigDecimal("olId").longValue(); this.olNbr = record.getStr("olNbr"); this.accessNumber = record.getStr("accessNumber"); this.partyName = record.getStr("partyName"); this.channelName = record.getStr("channelName"); this.staffName = record.getStr("staffName"); this.olStateName = record.getStr("olStateName"); this.marking = record.getStr("marking"); this.areaName = record.getStr("areaName"); this.domain = record.getStr("domain"); return this; } }
导出结果:
数据量大的情况,数据分多个excel,示例中每个excel最多2000行记录,压缩为zip文件导出,zip文件小很多,下载也快很多。