无法使用AbstractExcelView导出Excel工作表

时间:2012-12-18 13:51:31

标签: java spring spring-mvc

这是我的ExcelController.java

public ModelAndView generateExcel(HttpServletRequest request,
        HttpServletResponse response) {

    // Created List called 'employeeList'
    model.put("employeeList", employeeList);

    return new ModelAndView("CreateExcel","employeeList",employeeList);
}

这是我的ExcelRevenueReportView.java

protected void buildExcelDocument(Map<String, Object> model,
        HSSFWorkbook workBook, HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    @SuppressWarnings("unchecked")
    List<Employee> employeeList = (List<Employee>) model.get("employeeList");
    HSSFSheet sheet = workBook.createSheet("Employee List");

    HSSFRow header = sheet.createRow(0);
    header.createCell((short) 0).setCellValue("Employee");

    int rowNum = 1;
    for (Object employee : employeeList) {
        //create the row data
        HSSFRow row = sheet.createRow(rowNum++);
        System.out.println(row);
        row.createCell((short) 0).setCellValue(1);
            }
}

我的servlet.xml

<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>
<bean
    class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />

<bean class="com.sort.process.ExcelController" />

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

my spring-excel-views.xml

<context:component-scan base-package="com.sort.process" />
<context:property-placeholder location="conf/log4j.properties" />

<bean id="ExcelRevenueSummary"
    class="com.sort.process.ExcelRevenueReportView">
</bean>

我跟着this tutorial

在上面的项目中我知道我没有调用buildExcelDocument(),这可能是excel表没有生成的原因,我不确定是否应该在某个地方调用它。
最终,我无法通过这种方式生成excel表 任何人都可以发现错误吗? 还是其他任何可能的建议?

2 个答案:

答案 0 :(得分:7)

我建议您使用基于Annotation的控制器配置的更轻量级方法。在这种情况下,只需在控制器中放置以下代码:

@RequestMapping("list/excell")
public View listExcell() {
    return new AbstractExcelView() {
        @Override
        protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook,
                HttpServletRequest request, HttpServletResponse response) throws Exception {
            HSSFSheet sheet = workbook.createSheet("List of employees");

            setText(getCell(sheet, 0, 0), "Id");
            setText(getCell(sheet, 0, 1), "First name");
            setText(getCell(sheet, 0, 2), "Last name");
            setText(getCell(sheet, 0, 3), "Active");
            setText(getCell(sheet, 0, 4), "Salary");

            List<Employee> employees = dao.listEmployees();
            for (int i = 0; i < employees.size(); i++) {
                Employee employee = employees.get(i);
                setText(getCell(sheet, i + 1, 0), String.valueOf(employee.getId()));
                setText(getCell(sheet, i + 1, 1), employee.getFirstName());
                setText(getCell(sheet, i + 1, 2), employee.getLastName());
                setText(getCell(sheet, i + 1, 3), String.valueOf(employee.isActive()));
                setText(getCell(sheet, i + 1, 4), String.valueOf(employee.getSalary()));
            }
        }
    };
}

代码中的问题是在控制器中使用名为“CreateExcel”的视图,但在配置中,您将视图命名为“ExcelRevenueSummary”。

如果您需要一个完整的工作示例 - 您可能会发现它here

答案 1 :(得分:2)

以下是工作示例Excel Export Spring MVC。 如果您特定于扩展,那么您可以在代码中添加以下行。

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment; filename=BusinessSegment.xls");

现在,这将使用.xls扩展名在Excel中导出数据。