配置
- spring-mvc.xml配置XmlViewResolver
- 默認(rèn)地,XmlViewResolver將從/WEB-INF/views.xml中加載視圖bean
- 可通過location進行路徑的覆蓋
- 在views.xml配置需要注入的Excel的Bean
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="xxxxxExcelView" class="類路徑" />
</beans>
- 設(shè)置aop攔截的界面
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
<property name="order" value="1" />
</bean>
- 配置控制層入口
方式1: Mapping的配置
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
方式2:設(shè)置掃描到@Controller注解的Bean
<context:component-scan base-package="掃描包名" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
</context:component-scan>
設(shè)置use-default-filters="false"的原因 http://jinnianshilongnian.iteye.com/blog/1762632
5.定義控制層
返回ModelAndView垂睬,入?yún)⒅付ㄟM入的xxxxxExcelView媳荒,指定傳輸?shù)絰xxxxExcelView的傳輸key以及value
如果需要進行數(shù)據(jù)讀取之類的交換可在對應(yīng)的service實現(xiàn)中進行對應(yīng)的數(shù)據(jù)操作,然后再返回ModelAndView驹饺。
6.定義ExcelView(POI)
- 類繼承AbstractExcelView钳枕,從入?yún)odel中通過key取得控制層傳輸?shù)膙alue
- 對sheet進行初始化 HSSFSheet
- 可設(shè)置默認(rèn)列寬 setDefaultColumnWidth
- 對樣式進行初始化 HSSFCellStyle
- 背景顏色 字體 字號 加粗 字體顏色 入?yún)閟hort格式的對應(yīng)樣式類的索引
- 以行為單位進行內(nèi)容的填充
- 設(shè)置行 HSSFRow
- 設(shè)置單元格 HSSFCell
- 往單元格填充內(nèi)容和樣式
結(jié)束
參考實例:http://www.mkyong.com/spring-mvc/spring-mvc-export-data-to-excel-file-via-abstractexcelview/