ssslinppp

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
说明:

1.结合Spring MVC实现Excel导出功能;
2. 在MVC配置文件中配置Excel视图解析器;


需要的jar包

以poi开头的jar包都是必须的


web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  5. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  6. <context-param>
  7. <param-name>contextConfigLocation</param-name>
  8. <param-value>classpath:/conf/applicationContext.xml</param-value>
  9. </context-param>

  10. <listener>
  11. <listener-clas
  12. s>org.springframework.web.context.ContextLoaderListener</listener-class>
  13. </listener>

  14. <servlet>
  15. <servlet-name>baobaotao</servlet-name>
  16. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  17. <load-on-startup>1</load-on-startup>
  18. </servlet>
  19. <servlet-mapping>
  20. <servlet-name>baobaotao</servlet-name>
  21. <url-pattern>/</url-pattern>
  22. </servlet-mapping>
  23. </web-app>



baobaotao-servlet.xml ==>MVC配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
  8. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
  9. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
  10. <context:component-scan base-package="com.baobaotao.web" />
  11. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
  12. p:prefix="/WEB-INF/views/" p:suffix=".jsp" />
  13. <!-- Excel及PDF视图解析器配置 -->
  14. <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"
  15. p:order="10" />
  16. <bean id="userListExcel" class="com.baobaotao.web.UserListExcelView" />
  17. <!-- <bean id="userListPdf" class="com.baobaotao.web.UserListPdfView" /> -->
  18. </beans>

说明:Excel视图对应的视图解析器为:BeanNameViewResolver,即根据bean的名称来解析视图,解析到的视图实际上
是一个bean,即上面的<bean id="userListExcel" class="com.baobaotao.web.UserListExcelView" />;


UserController.java

  1. package com.baobaotao.web;
  2. import java.util.ArrayList;
  3. import java.util.Calendar;
  4. import java.util.GregorianCalendar;
  5. import java.util.List;
  6. import javax.validation.Valid;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Controller;
  9. import org.springframework.ui.ModelMap;
  10. import org.springframework.validation.BindingResult;
  11. import org.springframework.web.bind.annotation.ModelAttribute;
  12. import org.springframework.web.bind.annotation.PathVariable;
  13. import org.springframework.web.bind.annotation.RequestMapping;
  14. import org.springframework.web.bind.annotation.RequestMethod;
  15. import org.springframework.web.bind.annotation.RequestParam;
  16. import org.springframework.web.servlet.ModelAndView;
  17. import com.baobaotao.UserService;
  18. import com.baobaotao.domain.User;
  19. @Controller
  20. @RequestMapping("/user")
  21. public class UserController {

  22. @RequestMapping(value = "/showUserListByXls")
  23. public String showUserListInExcel(ModelMap mm) {
  24. Calendar calendar = new GregorianCalendar();
  25. List<User> userList = new ArrayList<User>();
  26. User user1 = new User();
  27. user1.setUserName("tom");
  28. user1.setRealName("汤姆");
  29. calendar.set(1980, 1, 1);
  30. user1.setBirthday(calendar.getTime());
  31. User user2 = new User();
  32. user2.setUserName("john");
  33. user2.setRealName("约翰");
  34. user2.setBirthday(calendar.getTime());
  35. userList.add(user1);
  36. userList.add(user2);
  37. mm.addAttribute("userList", userList);
  38. return "userListExcel";
  39. }
  40. }

说明:return "userListExcel";
视图解析器BeanNameViewResolver根据返回值userListExcel解析bean名称为userListExcel的bean;



UserListExcelView.java

  1. package com.baobaotao.web;
  2. import java.util.List;
  3. import java.util.Map;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. import org.apache.commons.lang.time.DateFormatUtils;
  7. import org.apache.poi.hssf.usermodel.HSSFCell;
  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.stereotype.Component;
  12. import org.springframework.web.servlet.view.document.AbstractExcelView;
  13. import com.baobaotao.domain.User;
  14. public class UserListExcelView extends AbstractExcelView {
  15. @Override
  16. protected void buildExcelDocument(Map<String, Object> model,
  17. HSSFWorkbook workbook, HttpServletRequest request,
  18. HttpServletResponse response) throws Exception {
  19. response.setHeader("Content-Disposition", "inline; filename="+
  20. new String("用户列表".getBytes(), "iso8859-1")); //iso8899-1:解决Excel文档名称出现中文乱码的问题
  21. List<User> userList = (List<User>) model.get("userList");
  22. HSSFSheet sheet = workbook.createSheet("users");
  23. HSSFRow header = sheet.createRow(0); //第0行
  24. for(int i=0;i<3;i++){
  25. header.createCell((short) i);
  26. sheet.getRow(0).getCell((short)i).setEncoding(HSSFCell.ENCODING_UTF_16); //解决Excel单元格出现中文乱码的问题
  27. sheet.getRow(0).getCell((short)i).setCellValue((i==0)?"账号":((i==1)?"姓名":"生日"));
  28. }
  29. int rowNum = 1;
  30. for (int i=0;i<userList.size();i++) {
  31. HSSFRow row = sheet.createRow(rowNum++); //创建行
  32. User user = userList.get(i);
  33. row.createCell((short) 0).setCellValue(user.getUserName());
  34. row.createCell((short) 1);
  35. row.getCell((short)1).setEncoding(HSSFCell.ENCODING_UTF_16); //解决Excel单元格出现中文乱码的问题
  36. row.getCell((short)1).setCellValue(user.getRealName());
  37. String createDate = DateFormatUtils.format(user.getBirthday(),
  38. "yyyy-MM-dd");
  39. row.createCell((short) 2).setCellValue(createDate);
  40. }
  41. }
  42. }


访问:

在浏览器中输入:http://localhost:8080/springQs/user/showUserListByXls 

执行流程如下:

1. user/showUserListByXls 请求到UserController.java类中的public String showUserListInExcel(ModelMap mm)方法;
2. 方法 String showUserListInExcel(ModelMap mm)显示的将userList添加到模型属性中,即:mm.addAttribute("userList", userList);
3. showUserListInExcel方法返回“userListExcel”,即:return "userListExcel";
4. 视图解析器BeanNameViewResolver:
  1. <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"
  2. p:order="10" />
根据返回值userListExcel找到名称为userListExcel的bean,即:
  1. <bean id="userListExcel" class="com.baobaotao.web.UserListExcelView" />
5. UserListExcelView.java类实现了AbstractExcelView类中的buildExcelDocument接口,
因此会生成Excel文档;

结果

会出现提示下载Excel,打开之后:
 




















posted on 2015-04-16 16:19  ssslinppp  阅读(621)  评论(0编辑  收藏  举报