gpt4 book ai didi

java - 无法使用 AbstractExcelView 导出 Excel 工作表

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:58:00 25 4
gpt4 key购买 nike

这是我的 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>

我的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表格。
谁能指出错误?
或者还有其他可能的建议吗?

最佳答案

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

@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()));
}
}
};
}

您代码中的问题是,在您的 Controller 中,您使用名为“CreateExcel”的 View ,但在您的配置中,您将 View 命名为“ExcelRevenueSummary”。

如果您需要一个完整的工作示例 - 您可以找到它 here .

关于java - 无法使用 AbstractExcelView 导出 Excel 工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13934465/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com