一、自定义视图-Excel视图
1、Maven依赖 引入POI
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.11</version>
</dependency>
2、自定义视图解析器
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.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;
import com.cn21.calendar.spider.test.User;
public class ViewExcel extends AbstractExcelView {
@SuppressWarnings("unchecked")
@Override
protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request,
HttpServletResponse response) throws Exception {
String fileName = "用户列表excel.xls";
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();
List<User> userList = (List<User>) model.get("userList");
// 产生Excel表头
HSSFSheet sheet = workbook.createSheet("基本信息");
HSSFRow header = sheet.createRow(0);
// 产生标题列
header.createCell(0).setCellValue("ID");
header.createCell(1).setCellValue("名字");
header.createCell(2).setCellValue("邮箱");
header.createCell(3).setCellValue("密码");
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("mm/dd/yyyy"));
int rowNumber = 1;
for (User user : userList) {
HSSFRow row = sheet.createRow(rowNumber++);
// 产生标题列
row.createCell(0).setCellValue(user.getId());
row.createCell(1).setCellValue(user.getName());
row.createCell(2).setCellValue(user.getEmail());
row.createCell(3).setCellValue(user.getPassword());
}
workbook.write(outputStream);
outputStream.flush();
outputStream.close();
}
}
3、视图映射配置dispather-servlet.xml
<!-- 通过order属性来定义视图解析器的优先级,order的值越小优先级越低 -->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
<property name="order" value="0"></property>
</bean>
四、控制器代码示例
@RequestMapping(value = "excel")
public ModelAndView viewExcel() {
Map<String, Object> model = new HashMap<>();
model.put("userList", getStudents());
return new ModelAndView(new ViewExcel(), model);
}
private List<User> getStudents() {
List<User> userList = new ArrayList<>();
User user = new User("1", "Tome", "Tom@qq.com", "123456");
userList.add(user);
return userList;
}
五、测试结果
二、PDF视图
1、Maven依赖包引入
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
2、自定义视图解析器
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.view.document.AbstractPdfView;
import com.cn21.calendar.spider.test.User;
import com.lowagie.text.Document;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
public class ViewPDF extends AbstractPdfView {
@SuppressWarnings("unchecked")
@Override
protected void buildPdfDocument(Map<String, Object> model, Document document, PdfWriter writer,
HttpServletRequest request, HttpServletResponse response) throws Exception {
String fileName = "用户信息.pdf"; // 设置response方式,使执行此controller时候自动出现下载页面,而非直接使用excel打开
response.setCharacterEncoding("UTF-8");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition",
"attachment; filename=" + new String(fileName.getBytes(), "iso8859-1"));
List<User> userList = (List<User>) model.get("userList");
BaseFont bfChinese = BaseFont.createFont("C://Windows//Fonts//simfang.ttf", BaseFont.IDENTITY_H,
BaseFont.NOT_EMBEDDED);
com.lowagie.text.Font FontChinese = new com.lowagie.text.Font(bfChinese, 12, com.lowagie.text.Font.NORMAL);
for (int i = 0; i < userList.size(); i++) {
User user = userList.get(i);
String value = "ID: " + user.getId() + "姓名: " + user.getName() + ",邮箱: " + user.getEmail() + ",密码: "
+ user.getPassword();
document.add(new Paragraph(value, FontChinese));
}
}
}
3、视图映射配置dispather-servlet.xml
<!-- 通过order属性来定义视图解析器的优先级,order的值越小优先级越低 -->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
<property name="order" value="0"></property>
</bean>
4、控制器代码示例
@RequestMapping("pdf")
public ModelAndView viewPDF(){
Map<String, Object> model = new HashMap<>();
model.put("userList", getStudents());
return new ModelAndView(new ViewPDF(), model);
}
private List<User> getStudents() {
List<User> userList = new ArrayList<>();
User user = new User("1", "Tome", "Tom@qq.com", "123456");
userList.add(user);
return userList;
}
五、测试结果