3

我正在尝试将 Excel 文件作为 Spring Roo 1.0.2 中的视图输出最快的方法是什么?(我必须添加新的映射等吗?)目前我正在使用默认的 Roo AjaxUrlBasedViewResolver。

谢谢

4

1 回答 1

5

我不认为有一种特定的“Roo”方式可以做到这一点,但 Spring MVC 确实有一个 AbstractExcelView 类,它使用 Apache POI 并且不会引起任何问题——我认为这是你所希望的最好的。

首先创建一个扩展 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>

最后创建带有以下内容的views-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>
于 2010-11-20T13:42:20.847 回答