gpt4 book ai didi

java - AbstractExcelView 在基于 Spring 的应用程序中被弃用

转载 作者:搜寻专家 更新时间:2023-10-31 19:32:17 25 4
gpt4 key购买 nike

我想在基于 Spring-Boot 的应用程序中实现将数据导出到 Excel 文件。

当我打算使用 AbstractExcelView ,但我知道 AbstractExcelView 已被弃用。导出 Excel 文件的替代解决方案是什么?

最佳答案

从 Spring 4.2 开始,您可以尝试使用 AbstractXlsView 及其 AbstractXlsxViewAbstractXlsxStreamingView 变体来代替 AbstractExcelView

例子之一如下

    import com.example.Course;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.web.servlet.view.document.AbstractXlsView;
import org.apache.poi.ss.usermodel.Workbook;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.DateFormat;
import java.util.List;
import java.util.Map;

public class XlsView extends AbstractXlsView {

private static final DateFormat DATE_FORMAT = DateFormat.getDateInstance(DateFormat.SHORT);

@Override
protected void buildExcelDocument(Map<String, Object> model,
Workbook workbook,
HttpServletRequest request,
HttpServletResponse response) throws Exception {

// change the file name
response.setHeader("Content-Disposition", "attachment; filename=\"my-xls-file.xls\"");

@SuppressWarnings("unchecked")
List<Course> courses = (List<Course>) model.get("courses");

// create excel xls sheet
Sheet sheet = workbook.createSheet("Spring MVC AbstractXlsView");

// create header row
Row header = sheet.createRow(0);
header.createCell(0).setCellValue("ID");
header.createCell(1).setCellValue("Name");
header.createCell(2).setCellValue("Date");

// Create data cells
int rowCount = 1;
for (Course course : courses){
Row courseRow = sheet.createRow(rowCount++);
courseRow.createCell(0).setCellValue(course.getId());
courseRow.createCell(1).setCellValue(course.getName());
courseRow.createCell(2).setCellValue(DATE_FORMAT.format(course.getDate()));
}
}
}

另一个例子

import com.example.Course;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.servlet.view.document.AbstractXlsxStreamingView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.DateFormat;
import java.util.List;
import java.util.Map;

public class XlsxStreamingView extends AbstractXlsxStreamingView {

private static final DateFormat DATE_FORMAT = DateFormat.getDateInstance(DateFormat.SHORT);

@Override
protected void buildExcelDocument(Map<String, Object> model,
Workbook workbook, HttpServletRequest request,
HttpServletResponse response) throws Exception {
// change the file name
response.setHeader("Content-Disposition", "attachment; filename=\"my-xlsxStreaming-file.xlsx\"");

@SuppressWarnings("unchecked")
List<Course> courses = (List<Course>) model.get("courses");

// create excel xls sheet
Sheet sheet = workbook.createSheet("Spring MVC AbstractXlsxStreamingView");

// create header row
Row header = sheet.createRow(0);
header.createCell(0).setCellValue("ID");
header.createCell(1).setCellValue("Name");
header.createCell(2).setCellValue("Date");

// Create data cells
int rowCount = 1;
for (Course course : courses){
Row courseRow = sheet.createRow(rowCount++);
courseRow.createCell(0).setCellValue(course.getId());
courseRow.createCell(1).setCellValue(course.getName());
courseRow.createCell(2).setCellValue(DATE_FORMAT.format(course.getDate()));
}
}
}

关于java - AbstractExcelView 在基于 Spring 的应用程序中被弃用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37983946/

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