编程语言
首页 > 编程语言> > java – 在基于Spring的应用程序中不推荐使用AbstractExcelView

java – 在基于Spring的应用程序中不推荐使用AbstractExcelView

作者:互联网

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

当我打算使用AbstractExcelView时,但我知道不推荐使用AbstractExcelView.导出Excel文件的替代解决方案是什么?

解决方法:

从Spring 4.2开始,您可以尝试使用AbstractXlsView及其AbstractXlsxView和AbstractXlsxStreamingView变体来代替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,spring-mvc,spring,spring-boot-2,export-to-excel
来源: https://codeday.me/bug/20190611/1219441.html