1. 程式人生 > >SpringMVC實現匯出為Excel

SpringMVC實現匯出為Excel

Spring MVC實現將資料匯出為Excel檔案

  1. 宣告介面,通過實現該介面實現將模型中的資料寫入Workbook中

    public interface ExcelExportService {
    
       void makeWorkBook(Map<String, Object> model, Workbook workBook);
    
    }
  2. 建立自定義的檢視類繼承org.springframework.web.servlet.view.document.AbstractXlsView抽象類,並且實現抽象方法buildExcelDocument(…)方法。

public class
ExcelView extends AbstractXlsView {
private String filename = null; /*自定義的介面,實現將資料模型中的資料寫入Workbook中*/ private ExcelExportService excelExpService = null; public ExcelView(ExcelExportService excelExpService) { this.excelExpService = excelExpService; } public ExcelView
(String filename, ExcelExportService excelExpService) { this.filename = filename; this.excelExpService = excelExpService; } @Override protected void buildExcelDocument(Map<String, Object> model, Workbook workbook , HttpServletRequest request, HttpServletResponse response) throws
Exception { if (excelExpService == null) { throw new RuntimeException("到處服務介面不能為null"); } // 檔名不為空,則設定下載時的檔名;否則,使用路徑的名字 if (!StringUtils.isEmpty(filename)) { String reqCharset = request.getCharacterEncoding(); reqCharset = reqCharset == null ? "UTF-8" : reqCharset; filename = new String(filename.getBytes(reqCharset), "ISO8859-1"); response.setHeader("Content-disposition", "attachment;filename=" + filename); } excelExpService.makeWorkBook(model, workbook); } -----------getter and setter----------- }
  1. 建立一個方法實現自定義的介面ExcelExportService

    private ExcelExportService exportService() {
       //使用Lambda表示式自定義匯出excel規則
       return (Map<String, Object> model, Workbook workbook) -> {
           //獲取使用者列表
           List<Role> roleList = (List<Role>) model.get("roleList");
           //生成Sheet
           Sheet sheet= workbook.createSheet("所有角色");
           //載入標題
           Row title = sheet.createRow(0);
           title.createCell(0).setCellValue("編號");
           title.createCell(1).setCellValue("名稱");
           title.createCell(2).setCellValue("備註");
           //便利角色列表,生成一行行的資料
           for (int i=0; i<roleList.size(); i++) {
               Role role = roleList.get(i);
               int rowIdx = i + 1;
               Row row = sheet.createRow(rowIdx);
               row.createCell(0).setCellValue(role.getId());
               row.createCell(1).setCellValue(role.getRoleName());
               row.createCell(2).setCellValue(role.getNote());
           }
       };
    }
  2. 在Controller中呼叫

    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public ModelAndView exportRoles() {
       ModelAndView mv = new ModelAndView();
       // 建立檢視
       ExcelView ev = new ExcelView(exportService());
       ev.setFilename("所有角色.xls");
       // 構造查詢資料庫引數
       RoleParam roleParam = new RoleParam();
       PageParam pageParam = new PageParam();
       pageParam.setStart(0);
       pageParam.setLimit(100);
       roleParam.setPageParam(pageParam);
       // 查詢資料庫
       List<Role> roles = iRoleService.findRoles(roleParam);
       // 將資料放入模型中
       mv.addObject("roleList", roles);
       mv.setView(ev);
       return mv;
    }