Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法使用AbstractExcelView导出excel工作表_Java_Spring_Spring Mvc - Fatal编程技术网

Java 无法使用AbstractExcelView导出excel工作表

Java 无法使用AbstractExcelView导出excel工作表,java,spring,spring-mvc,Java,Spring,Spring Mvc,这是我的ExcelController.java public ModelAndView generateExcel(HttpServletRequest request, HttpServletResponse response) { // Created List called 'employeeList' model.put("employeeList", employeeList); return new ModelAndView("Creat

这是我的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);
            }
}
受保护的文档(地图模型,
HSSF工作簿、HttpServletRequest请求、HttpServletResponse响应)
抛出异常{
@抑制警告(“未选中”)
List employeeList=(List)model.get(“employeeList”);
HSSFSheet sheet=workBook.createSheet(“员工列表”);
HSSFRow表头=sheet.createRow(0);
header.createCell((短)0.setCellValue(“雇员”);
int rowNum=1;
for(对象员工:employeeList){
//创建行数据
HSSFRow行=sheet.createRow(rowNum++);
系统输出打印项次(行);
row.createCell((短)0.setCellValue(1);
}
}
myservlet.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>

/WEB-INF/spring-excel-views.xml
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>

我跟着

在上面的项目中,我知道我没有调用
buildExcelDocument()
,这可能是excel工作表没有生成的原因,我不确定是否应该调用它。
最终,我无法通过这种方式生成excel工作表。
谁能指出错误吗?

或者任何其他可能的建议?

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

@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()));
            }
        }
    };
}
@RequestMapping(“列表/excell”)
公共视图ListXcell(){
返回新的AbstractExcelView(){
@凌驾
受保护的无效文件(地图模型、HSSF工作簿、,
HttpServletRequest请求、HttpServletResponse响应)引发异常{
HSSFSheet sheet=workbook.createSheet(“员工列表”);
setText(getCell(第0页,第0页),“Id”);
setText(getCell(第0页,第1页),“名字”);
setText(getCell(第0页,第2页),“姓氏”);
setText(getCell(第0页,第3页),“活动”);
setText(getCell(第0页,第4页),“工资”);
List employees=dao.listEmployees();
对于(int i=0;i
代码中的问题是,在控制器中使用名为“CreateExcel”的视图,但在配置中,将视图命名为“ExcelRevenueSummary”


如果您需要完整的工作示例,您可以找到它。

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

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

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

但是现在您已经混合了视图和控制器。MVC,关注点分离。您可以轻松地进行单独的视图-只需使用命名类而不是匿名类,并使用employees列表而不是DAO创建它-这样,获取此列表的逻辑将与视图解耦。但是,通过隔离层,可以提高通用级别。。。您可以通过以下链接找到方法