Spring Boot 配置EasyExcel实现Excel文件导出

1 EasyExcel

EasyExcel是一个基于Java的简单、省内存的读写Excel的开源项目。在尽可能节约内存的情况下支持读写百M的Excel。

2 pom.xml(Maven配置文件)

        
            com.alibaba
            easyexcel
            2.1.6
        

3 EasyExcelController

package com.controller;

import com.alibaba.excel.EasyExcel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
public class EasyExcelController {
    /**
     * 导出excel文件
     *
     * @param response
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response) {
        try {
            List> dataList = new ArrayList<>();
            List headList = new ArrayList<>(Arrays.asList(new Object[]{"表头1", "表头2", "表头3"}));
            dataList.add(headList);
            String fileName = URLEncoder.encode("文件名称.xlsx", "UTF-8");
            response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
            //设置类型,扩展名为.xls
            response.setContentType("application/vnd.ms-excel");
            ServletOutputStream out = response.getOutputStream();
            EasyExcel.write(out,null).sheet("sheet1").doWrite(dataList);
            response.flushBuffer();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
} 
  

4 调试结果 

Spring Boot 配置EasyExcel实现Excel文件导出_第1张图片

 

Spring Boot 配置EasyExcel实现Excel文件导出_第2张图片

你可能感兴趣的:(Spring,Boot,文档处理工具)