春罗Excel视图

发布于 2024-08-23 13:19:40 字数 123 浏览 3 评论 0原文

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

谢谢

I am trying to output an Excel file as the view in Spring Roo 1.0.2
What is the quickest way to do this?
(Do I have to add new mapping etc?) At the moment I am using the default Roo AjaxUrlBasedViewResolver.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

老娘不死你永远是小三 2024-08-30 13:19:40

我不认为有一个特定的“Roo”方法可以做到这一点,但 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>

最后创建包含以下内容的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>

I don't think there's a specific "Roo" way of doing this, but Spring MVC does have an AbstractExcelView class that uses Apache POI and doesn't cause any problems - I think it's the best you can hope for.

First create a view class that extends AbstractExcelView and implements the buildExcelDocument method:

 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\"");
    }
 }

Then add the following to your webmvc-config.xml. I don't think the position matters, but I have it below the section where my TilesConfigurer is listed:

   <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>

Finally create views-excel.xml with the following in it:

  <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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文