<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.2.8</version>
        </dependency>

JavaBean

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.*;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;

import java.util.Date;

@ColumnWidth(20)
@HeadStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND, fillForegroundColor = 9) // IndexedColors.WHITE
@HeadFontStyle(fontHeightInPoints = 10, fontName = "Arial")
@ContentStyle(dataFormat = 0x31, borderBottom = BorderStyle.THIN, borderTop = BorderStyle.THIN, borderRight = BorderStyle.THIN, borderLeft = BorderStyle.THIN)
@ContentFontStyle(fontHeightInPoints = 10, fontName = "Arial")
public class LaoUser {

    @ExcelProperty("ICCID")
    private Long iccid;

    @ExcelProperty("供应商")
    private String supplierName;

    @ExcelProperty("运营商")
    private String operatorName;

    @ExcelProperty("套餐")
    private String comboName;

    @ExcelProperty("使用量")
    private String usageAmount;

    @ExcelProperty("状态")
    private String status;

    @ExcelProperty("创建时间")
    private Date createTime;

    // set / get
}

Controller接口

import com.alibaba.excel.EasyExcel;
import com.google.common.collect.Lists;
import com.test.mapper.ReportInfoMapper;
import com.test.poi.LaoUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;

@RestController
public class FileController {

    @Autowired
    ReportInfoMapper reportInfoMapper;

    @GetMapping("/export")
    public void exportFile(int num, HttpServletResponse response) throws IOException {
        // 查询数据
        Map<String, Object> params = new HashMap<>();
        params.put("num", num);
        List<LaoUser> laoUserList = reportInfoMapper.findReportList(params);

        // 设响应头response信息
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("测试", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

        // 写入数据到Excel中
        EasyExcel.write(response.getOutputStream(), LaoUser.class).sheet("模板").doWrite(laoUserList);
    }


}

自定义单元格写入类型(如对象属性是 Long类型,写入表格后是数字类型的单元格内容,想要转换成字符串,使用如下方法):

@ExcelProperty(value = "订单编号", converter = LongStringConverter.class)
import java.text.ParseException;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.NumberUtils;

/**
 * Long and string converter
 */
public class LongStringConverter implements Converter<Long> {

    @Override
    public Class supportJavaTypeKey() {
        return Long.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public Long convertToJavaData(CellData cellData, ExcelContentProperty contentProperty,
        GlobalConfiguration globalConfiguration) throws ParseException {
        return NumberUtils.parseLong(cellData.getStringValue(), contentProperty);
    }

    @Override
    public CellData convertToExcelData(Long value, ExcelContentProperty contentProperty,
        GlobalConfiguration globalConfiguration) {
        return NumberUtils.formatToCellData(value, contentProperty);
    }
}

其他自行补脑(上诉案例够用了)

EasyExcel导出案例(只有你想不到)_spring