Springmvc 使用 AbstractExcelView 导出excel
$("#exportBtn").click(function () { location.href = "${pageContext.request.contextPath}/report/export?" + $("form").serialize() });
1.新建类继承AbstractExcelView
1 import java.io.OutputStream; 2 import java.util.Map; 3 import javacommon.util.MyUtils; 4 import javax.servlet.http.HttpServletRequest; 5 import javax.servlet.http.HttpServletResponse; 6 import org.apache.poi.hssf.usermodel.HSSFCell; 7 import org.apache.poi.hssf.usermodel.HSSFCellStyle; 8 import org.apache.poi.hssf.usermodel.HSSFRow; 9 import org.apache.poi.hssf.usermodel.HSSFSheet; 10 import org.apache.poi.hssf.usermodel.HSSFWorkbook; 11 import org.springframework.web.servlet.view.document.AbstractExcelView; 12 public class ViewExcel extends AbstractExcelView { 13 @Override 14 protected void buildExcelDocument(Map<String, Object> obj, 15 HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) 16 throws Exception { 17 HSSFSheet sheet = workbook.createSheet("list"); 18 sheet.setDefaultColumnWidth((short) 12); 19 HSSFCell cell = getCell(sheet, 0, 0); 20 setText(cell, "Spring Excel test"); 21 HSSFCellStyle dateStyle = workbook.createCellStyle(); 22 //dateStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("mm/dd/yyyy")); 23 cell = getCell(sheet, 1, 0); 24 cell.setCellValue("日期:2008-10-23"); 25 //cell.setCellStyle(dateStyle); 26 getCell(sheet, 2, 0).setCellValue("测试1"); 27 getCell(sheet, 2, 1).setCellValue("测试2"); 28 HSSFRow sheetRow = sheet.createRow(3); 29 for (short i = 0; i < 10; i++) { 30 sheetRow.createCell(i).setCellValue(i * 10); 31 } 32 String filename = "测试.xls";//设置下载时客户端Excel的名称 33 filename = MyUtils.encodeFilename(filename, request);//处理中文文件名 34 response.setContentType("application/vnd.ms-excel"); 35 response.setHeader("Content-disposition", "attachment;filename=" + filename); 36 OutputStream ouputStream = response.getOutputStream(); 37 workbook.write(ouputStream); 38 ouputStream.flush(); 39 ouputStream.close(); 40 } 41 }
处理中文文件名的方法:
1 /** 2 * 设置下载文件中文件的名称 3 * 4 * @param filename 5 * @param request 6 * @return 7 */ 8 public static String encodeFilename(String filename, HttpServletRequest request) { 9 /** 10 * 获取客户端浏览器和操作系统信息 11 * 在IE浏览器中得到的是:User-Agent=Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; Alexa Toolbar) 12 * 在Firefox中得到的是:User-Agent=Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.7.10) Gecko/20050717 Firefox/1.0.6 13 */ 14 String agent = request.getHeader("USER-AGENT"); 15 try { 16 if ((agent != null) && (-1 != agent.indexOf("MSIE"))) { 17 String newFileName = URLEncoder.encode(filename, "UTF-8"); 18 newFileName = StringUtils.replace(newFileName, "+", "%20"); 19 if (newFileName.length() > 150) { 20 newFileName = new String(filename.getBytes("GB2312"), "ISO8859-1"); 21 newFileName = StringUtils.replace(newFileName, " ", "%20"); 22 } 23 return newFileName; 24 } 25 if ((agent != null) && (-1 != agent.indexOf("Mozilla"))) 26 return MimeUtility.encodeText(filename, "UTF-8", "B"); 27 28 return filename; 29 } catch (Exception ex) { 30 return filename; 31 } 32 }
2接下来在你的下载 Controller里面返回ModelAndView 即可
1 /** 2 * 导出Excel 3 * @param model 4 * @param projectId 5 * @param request 6 * @return 7 */ 8 @RequestMapping(value="/dcExcel",method=RequestMethod.GET) 9 public ModelAndView toDcExcel(ModelMap model, HttpServletRequest request){ 10 List list = new ArrayList(); //测试数据没有用到 11 Map map = new HashMap(); 12 list.add("test1"); 13 list.add("test2"); 14 map.put("list", list); 15 ViewExcel viewExcel = new ViewExcel(); 16 return new ModelAndView(viewExcel, model); 17 }
代码很简单,只实现下载,关于Excel 格式数据方面请参考 poi 框架