第一步、前端获取参数并且通过form表单或者URL方式访问后台
//定义params对象接收键值对参数
var params = { isChecked: 1, startTime: "2017-03-05", endTime: "2017-03-08", status: 1, arbType: 2423, claimsStatus: 3412, arbDutyId: 542, siteId: 54, provinceId: 1342 };//把param对象拼接成字符串
var encodeParam = function(json) { var tmps = []; for ( var key in json) { tmps.push(key + '=' + json[key]); } return tmps.join('&'); }//定义url
var url=basePath+"submenuController/export?"+encodeParam(query); //判断是否有id为_exportForm的form表单,如果没有则创建一个隐藏的form,把url放入,然后submit var exportForm = document.getElementById("_exportForm"); if (!exportForm){ exportForm = document.createElement("form"); exportForm.setAttribute('id',"_exportForm"); exportForm.setAttribute("action", url); exportForm.setAttribute("method", "post"); } document.body.appendChild(exportForm); exportForm.submit();第二步、后台接收参数,拼接Excel Title,查询出list,调用Excel生成工具类
@RequestMapping(value ="export",method = RequestMethod.POST) @ResponseBody public void exportSiteDeclareList(HttpServletResponse response,SubmenuQuery query){ String[] headArray = {"运单号","理赔情况","申报网点名称","省区","申报类型","责任类型", "仲裁登记时间","寄件日期","仲裁审核日期","分单人","分单时间"}; List<SubmenuVO> list = this.submenuFacade.findSubmenuPageQuery(query).getRows(); List<Object[]> contentList = new ArrayList<>(); if(list.size()>0) { for (SubmenuVO entity : list) { Object[] o = { entity.getEwbNo(),//"运单号 entity.getClaimsStatus(),//理赔情况 entity.getDeclareSiteName(),//申报网点名称 entity.getProvinceAreaName(),//省区 entity.getArbTypeName(),//申报类型 entity.getArbDutyName(),//责任类型 entity.getArbRegisterTime(),//仲裁登记时间 entity.getEwbDate(),//寄件日期 entity.getArbCheckTime(),//仲裁审核日期 entity.getUserName(),//分单人 entity.getCreateTime()//分单时间 }; contentList.add(o); } }else{ return; } try { ExeclUtil.ExportExcel(response, headArray, contentList, "SubmenuList.xls"); } catch (Exception e) { LOGGER.error("网点理赔申报导出报错"+e); } }
第三步、Excel工具类
package util; import org.apache.poi.hssf.usermodel.*; import org.apache.poi.hssf.util.HSSFColor; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.servlet.http.HttpServletResponse; /** * Excel导出工具类 */ public class ExeclUtil { /** * @param title * @param headers * @param dataset */ public static InputStream exportExcel(String title, String[] headers, List<Object[]> dataset) { InputStream is; ByteArrayOutputStream os = new ByteArrayOutputStream(); // 声明一个工作薄 HSSFWorkbook workbook = new HSSFWorkbook(); // 生成一个表格 HSSFSheet sheet = workbook.createSheet(title); // 设置表格默认列宽度为15个字节 sheet.setDefaultColumnWidth((short) 15); // 生成一个样式 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); } // 遍历集合数据,产生数据行 int index = 0; for (Object[] o : dataset) { index++; row = sheet.createRow(index); for (short i = 0; i < o.length; i++) { HSSFCell cell = row.createCell(i); cell.setCellStyle(style2); try { Object value = o[i]; if(value== null){ value=""; } // 判断值的类型后进行强制类型转换 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( "yyyy-MM-dd HH:mm:ss"); 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) 7, index, (short) 7, index); anchor.setAnchorType(2); patriarch.createPicture(anchor, workbook.addPicture( bsValue, HSSFWorkbook.PICTURE_TYPE_JPEG)); } else { // 其它数据类型都当作字符串简单处理 textValue = value.toString(); } // 如果不是图片数据,就利用正则表达式判断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); HSSFFont font3 = workbook.createFont(); font3.setColor(HSSFColor.BLUE.index); richString.applyFont(font3); cell.setCellValue(richString); } } } catch (Exception e) { e.printStackTrace(); } finally { // 清理资源 } } } try { workbook.write(os); } catch (IOException e) { e.printStackTrace(); } is = new ByteArrayInputStream(os.toByteArray()); return is; } public static void ExportExcel(HttpServletResponse response, String[] headArray, List<Object[]> contentList, String fileName) throws Exception { // 读到流中 InputStream inStream = exportExcel(fileName, headArray, contentList); // 设置输出的格式 response.reset(); response.setContentType("bin"); response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); // 循环取出流中的数据 byte[] b = new byte[100]; int len; try { while ((len = inStream.read(b)) > 0) response.getOutputStream().write(b, 0, len); inStream.close(); } catch (IOException e) { e.printStackTrace(); } } }