标签:
注意: 1. 模板需放在 WEB-INF 目录下 2. 指定模板路径时不需要添加扩展名, Spring将自动添加 .xls 到URL 属性中. 3. 在指定URL前需先设置 view 的 ApplicationContext |
1 <bean id="beanNameViewResolver"
2 class="org.springframework.web.servlet.view.BeanNameViewResolver" />
3
4 <bean id="viewController" class="ViewController">
5 <property name="dataModel">
6 <ref bean="model-DataModel"/>
7 </property>
8 </bean>
9
10 <bean id="urlMapping"
11 class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
12 <property name="mappings">
13 <props>
14 <prop key="/view/*.report">viewController</prop>
15 </props>
16 </property>
17 </bean>
1 public class ExcelView extends AbstractExcelView {
2
3 protected void buildExcelDocument(Map<String, Object> model,
4 HSSFWorkbook wb, HttpServletRequest request, HttpServletResponse reqponse)
5 throws Exception {
6
7 //...
8 Object[] datas = (Object[]) model.get("data");
9 //...
10
11 HSSFSheet sheet;
12 //get the sheet 1 in template
13 sheet = wb.getSheetAt(0);
14 //handle this sheet
15 //...
16 }
17 }
1 public class ViewController extends MultiActionController {
2
3 private DataModel dataModel;
4
5 public void setDataModel(DataModel dataModel){
6 this.dataModel = dataModel;
7 }
8
9 public ModelAndView exportToExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
10
11 Map<String,Object> model=new HashMap<String,Object>();
12
13 Object[] datas;
14
15 datas = dataModel.getDatas.toArray();
16
17 model.put("data", datas);
18
19 ExcelView excelView = new ExcelView();
20
21 excelView.setApplicationContext(this.getWebApplicationContext());
22 excelView.setUrl("/WEB-INF/Template");
23
24 return new ModelAndView(excelView,model);
25 }
26 }
1 <servlet>
2 <servlet-name>viewController</servlet-name>
3 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
4 <init-param>
5 <param-name>contextConfigLocation</param-name>
6 <param-value>/WEB-INF/config/control-context.xml</param-value>
7 </init-param>
8 <load-on-startup>1</load-on-startup>
9 </servlet>
10 <servlet-mapping>
11 <servlet-name>viewController</servlet-name>
12 <url-pattern>*.report</url-pattern>
13 </servlet-mapping>
Spring 中 AbstractExcelView 支持根据模板生成Excel文件. 通过设置 view 的 URL 属性指定模板的路径
标签:
原文地址:http://www.cnblogs.com/walk-the-Line/p/5387383.html