Spring 教程示例 - Apache excel 使用 java 配置导出 POI

Spring tutorial example - Apache excel POI export using java config

我正在查看这个 mkyong tutorial 以导出到 excel。

它看起来很简单,也不太复杂。除了最后一步,我能够完成所有步骤。最后一步是 XML 配置步骤。我使用 java 注释配置设置了我的项目,教程中的示例在 xml 中。如何在 'java config' / annotaiton 中配置以下 XmlViewResolver 而不是 xml?请参阅下面的代码片段。


这是我不知道如何配置的部分。我正在尝试以 annotation/java 配置格式获取它。

Spring配置

<beans ...>

  <bean
  class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />

    <bean class="com.mkyong.common.controller.RevenueReportController" />

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

</beans>

文件:spring-excel-views.xml

<bean id="ExcelRevenueSummary"
    class="com.mkyong.common.view.ExcelRevenueReportView">
</bean>

这是我的控制器的样子。

@Controller
public class RevenueReportController extends AbstractController{

    private Logger log = LoggerFactory.getLogger(getClass());

    @Override
    @RequestMapping(value = "/search/exportToExcel", method = RequestMethod.GET)
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {

        String output = ServletRequestUtils.getStringParameter(request, "output");

        //dummy data
        Map<String,String> revenueData = new HashMap<String,String>();
        revenueData.put("Jan-2010", "0,000,000");
        revenueData.put("Feb-2010", "0,000,000");
        revenueData.put("Mar-2010", "0,000,000");
        revenueData.put("Apr-2010", "0,000,000");
        revenueData.put("May-2010", "0,000,000");

        if(output ==null || "".equals(output)){
            //return normal view
            return new ModelAndView("RevenueSummary","revenueData",revenueData);

        }else if("EXCEL".equals(output.toUpperCase())){
            //return excel view
            return new ModelAndView("ExcelRevenueSummary","revenueData",revenueData);

        }else{
            //return normal view
            return new ModelAndView("RevenueSummary","revenueData",revenueData);

        }
    }
}

您需要进行如下更改:

新建配置class:

@Configuration
@ComponentScan("com.mkyong.common")
public class AppConfig {
    @Autowired
    ServletContext servletContext;

    @Bean
    public ViewResolver getXmlViewResolver() {
        XmlViewResolver resolver = new XmlViewResolver();
        resolver.setLocation(new ServletContextResource(servletContext,
                    "/WEB-INF/spring-excel-views.xml"));
        return resolver;
    }    
}

@Controller

注释你的控制器class
@Controller
public class RevenueReportController extends AbstractController {

}

spring-excel-views.xml 与您使用的 XmlViewResolver 相同。它期望 bean 定义在 XML 文件中。

将我的评论扩展到原始 post,这是一个不需要任何 xml 配置的版本。使用此解决方案,您可以完全消除 xml 视图。您可以将 excel 视图定义为另一个 bean 并直接在您的控制器中使用它。

配置class:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@EnableWebMvc
@ComponentScan(basePackages = { "com.mkyong.common" })
@Configuration
public class AppConfig extends WebMvcConfigurerAdapter {

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Bean
    public ExcelRevenueReportView excelRevenueReportView() {
        return new ExcelRevenueReportView();
    }

    @Bean
    public InternalResourceViewResolver getInternalResourceViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/");
        resolver.setSuffix(".jsp");
        return resolver;
    }

}

控制器:

@Controller
public class RevenueReportController extends AbstractController{

    private Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    ExcelRevenueReportView excelRevenueReportView;

    @Override
    @RequestMapping(value = "/search/exportToExcel", method = RequestMethod.GET)
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {

        String output = ServletRequestUtils.getStringParameter(request, "output");

        //dummy data
        Map<String,String> revenueData = new HashMap<String,String>();
        revenueData.put("Jan-2010", "0,000,000");
        revenueData.put("Feb-2010", "0,000,000");
        revenueData.put("Mar-2010", "0,000,000");
        revenueData.put("Apr-2010", "0,000,000");
        revenueData.put("May-2010", "0,000,000");

        if(output ==null || "".equals(output)){
            //return normal view
            return new ModelAndView("RevenueSummary","revenueData",revenueData);

        }else if("EXCEL".equals(output.toUpperCase())){
            //return excel view
            new ModelAndView(excelRevenueReportView, "revenueData", revenueData);

        }else{
            //return normal view
            return new ModelAndView("RevenueSummary","revenueData",revenueData);

        }
    }
}