EasyExcel导出excel

EasyExcel导出excel

1.首先建立一个实体类

public class ExcelMode extends BaseRowModel {
    @ExcelIgnore   //excel中不导出字段
    private Integer id;
    @ExcelProperty(value = "姓名" ,index = 0)
    private String name;
    @ExcelProperty(value = "密码" ,index = 1)
    private String password;
    @ExcelProperty(value = "手机号码",index = 2)
    private String phone;
    @ExcelProperty(value = "性别",index = 2,converter = SettlementConverter.class )
     //SettlementConverter.class 将数据库枚举字段转换成字符串类型(例:1/男,2/女
    private Integer sex;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }
}
  1. SettlementConverter.class 实现 Converter 接口
public class SettlementConverter implements Converter<Integer> {

    @Override
    public Class supportJavaTypeKey() {
        return null;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return null;
    }

    @Override
    public Integer convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        return null;
    }

    @Override
    public CellData convertToExcelData(Integer s, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        if(s == 1){
            return new CellData("男");
        }else {
            return new CellData("女");
        }
    }
  1. 导出excel
    @RequestMapping(value = "/toExcel")
	@ResponseBody
	public Object toExcel(HttpServletRequest rep , HttpServletResponse reps) {
		try {
			ExcelMode u = new ExcelMode();
			
			List<ExcelMode> list = userservice.ExcelMode();
			String fileName = new String("文件名称.xlsx".getBytes(), "ISO-8859-1");
			reps.addHeader("Content-Disposition", "filename=" + fileName);
			ServletOutputStream out = reps.getOutputStream();
			EasyExcelFactory.write(out, ExcelMode.class).sheet("文件标签名称").doWrite(list);
		}catch (Exception e){
			e.printStackTrace();
		}
		return null;
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值