可以参考博客http://www.cnblogs.com/parryyang/p/5683600.html,举例很清晰。
对自定义的视图名称匹配不同的解析器进行解析。
作用:自己定义视图,视图继承view类或者abstractExcelView或者abstractPdfView,将内容以Excel或者PDF格式显示。
关键的实现过程:
1. 创建excelView视图,继承AbstractXlsxView。
参考如下代码,实现功能:从modelAndView中获取model数据,作为excel视图显示。
package com.tiekui.springmvc.views;import java.io.OutputStream;import java.util.List;import java.util.Map;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.poi.hssf.usermodel.HSSFCellStyle;import org.apache.poi.hssf.usermodel.HSSFDataFormat;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.ss.usermodel.CellStyle;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;import org.springframework.web.servlet.view.document.AbstractXlsxView;import com.tiekui.springmvc.pojo.User;public class excelView extends AbstractXlsxView { @Override protected void buildExcelDocument(Mapmodel, Workbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception { String fileName = "UserList.xlsx"; response.setCharacterEncoding("UTF-8"); response.setContentType("application/ms-excel"); response.setHeader("Content-Disposition", "inline; filename="+new String(fileName.getBytes(),"iso8859-1")); OutputStream outputStream = response.getOutputStream(); System.out.println(model.get("userList")); List userList = (List ) model.get("userList"); // 产生Excel表头 Sheet sheet = workbook.createSheet("基本信息"); Row header = sheet.createRow(0); // 产生标题列 header.createCell(0).setCellValue("ID"); header.createCell(1).setCellValue("名字"); header.createCell(2).setCellValue("邮箱"); header.createCell(3).setCellValue("密码"); CellStyle cellStyle = workbook.createCellStyle(); cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("mm/dd/yyyy")); int rowNumber = 1; for (User user : userList) { Row row = sheet.createRow(rowNumber++); // 产生标题列 row.createCell(0).setCellValue(user.getAge()); row.createCell(1).setCellValue(user.getUsername()); row.createCell(2).setCellValue(user.getEmail()); row.createCell(3).setCellValue(user.getPassword()); } workbook.write(outputStream); outputStream.flush(); outputStream.close(); }}
2.创建控制器。
参考如下代码,实现功能:访问相关URL时,直接去访问创建的excelView视图。
package com.tiekui.springmvc.handlers;import java.util.ArrayList;import java.util.HashMap;import java.util.Map;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.validation.Errors;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;import com.sun.org.apache.xpath.internal.operations.Mod;import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;import com.tiekui.springmvc.pojo.User;import com.tiekui.springmvc.views.excelView;@Controllerpublic class ExcelViewTest { @RequestMapping("excelView") public ModelAndView excelViewTest(User user) { Mapmodel = new HashMap<>(); ArrayList userlist = new ArrayList<>(); userlist.add(user); model.put("userList", userlist); ModelAndView modelAndView = new ModelAndView(new excelView(),model); return modelAndView; } }
3.访问视图index.jsp
4.SpringMVC配置文件中必须添加以下内容: