利用Springmvc的AbstractXlsxView下载Excel文件
设计一个模型,在针对多中数据类型进行拓展
public abstract class ExcelView extends AbstractXlsxView { public CellStyle cellStyle; /** * 设置样式 */ protected abstract void setStyle(Workbook workbook); /** * 设置row由子类实现 */ protected abstract void setRowStyle(Sheet sheet, Map<String, Object> map); @Override protected void buildExcelDocument( Map<String, Object> model, Workbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception{ //自定义文件名称 String excelName = DateUtil.getSdfTimes()+".xlsx"; String Agent = request.getHeader("User-Agent"); if (null !=Agent){ Agent = Agent.toLowerCase(); //针对火狐乱码的处理 if (Agent.indexOf("firebox") !=-1){ response.setHeader("content-disposition", String.format("attachment;filename*=utf-8'zh_cn'%s", URLEncoder.encode(excelName, "utf-8"))); } else { response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(excelName, "utf-8")); } } response.setContentType("application/ms-excel; charset=UTF-8"); Sheet sheet = workbook.createSheet("User Detail"); sheet.setDefaultColumnWidth(30); this.setStyle(workbook); setRowStyle(sheet, model); } }
拓展的数据类型
public class OrdersExcleView extends ExcelView{ //样式设置 @Override protected void setStyle(Workbook workbook) { } @Override protected void setRowStyle(Sheet sheet, Map<String, Object> map) { //创建行的头部 Row header = sheet.createRow(0); header.createCell(0).setCellValue("订单ID"); header.getCell(0).setCellStyle(super.cellStyle); header.createCell(1).setCellValue("订单总价"); header.getCell(1).setCellStyle(super.cellStyle); header.createCell(2).setCellValue("收货人"); header.getCell(2).setCellStyle(super.cellStyle); header.createCell(3).setCellValue("手机号码"); header.getCell(3).setCellStyle(super.cellStyle); header.createCell(4).setCellValue("省份"); header.getCell(4).setCellStyle(super.cellStyle); header.createCell(5).setCellValue("地址"); header.getCell(5).setCellStyle(super.cellStyle); List<PageData> orderList= (List<PageData>) map.get("orderList"); int rowCount = 1; for (PageData pageData : orderList) {
// String totalprice = pageData.getString("TOTALPRICE"); String totalprice = String.valueOf(pageData.get("TOTALPRICE")); System.out.println(totalprice); Row userRow = sheet.createRow(rowCount++); userRow.createCell(0).setCellValue(pageData.getString("ORDERSN")); userRow.createCell(1).setCellValue(totalprice); userRow.createCell(2).setCellValue(pageData.getString("CONSIGNEE")); userRow.createCell(3).setCellValue(pageData.getString("MOBILE")); userRow.createCell(4).setCellValue(pageData.getString("PROVINCENAME")); userRow.createCell(5).setCellValue(pageData.getString("ADDRESS")); } } }
下面是controller
@RequestMapping(value = "/download",method = RequestMethod.GET) public ModelAndView download() throws Exception { PageData pageData = new PageData(); List<PageData> list = ordersService.listAll(pageData); Map<String, Object> map = new HashMap<String, Object>(); map.put("orderList",list); // ExcelView excelView = new ExcelView(); ExcelView view = new OrdersExcleView(); return new ModelAndView(view, map); }