2010-03-03 121 views
3

我想輸出一個Excel文件作爲Spring Roo 1.0.2中的視圖 這樣做的最快方法是什麼? (我必須添加新的映射等?)目前我使用默認的Roo AjaxUrlBasedViewResolver。spring roo excel view

感謝

回答

5

我不認爲有這樣做的具體的「袋鼠」的方式,但是Spring MVC的確實有使用Apache POI並不會造成任何問題的AbstractExcelView類 - 我認爲這是最好的你可以期待。

首先創建擴展AbstractExcelView並實現buildExcelDocument方法方法的視圖類:

import org.springframework.web.servlet.view.document.AbstractExcelView; 

public class XlsView extends AbstractExcelView { 

    @Override 
    protected void buildExcelDocument(
      Map<String, Object> model, HSSFWorkbook workbook, 
      HttpServletRequest request, HttpServletResponse response) throws Exception { 
     //Apache POI code to set up the HSSFWorkbook goes here 
     response.setHeader("Content-Disposition", "attachment; filename=\"file.xls\""); 
    } 
} 

然後添加以下到您的webmvc-config.xml中。我不認爲位置的問題,但我有它在我的TilesConfigurer在列出下面的部分:

<bean id="excelViewResolver" class="org.springframework.web.servlet.view.XmlViewResolver"> 
      <property name="order" value="1"/> 
      <property name="location" value="/WEB-INF/views/views-excel.xml"/> 
     </bean> 

最後創建視圖,excel.xml在它下面:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> 
    <bean name="XlsView" class="com.your.package.XlsView"/> 
    </beans>