三分钟搞懂alibaba的excel导出——EasyExcel

注意:学习本文章一定要打开自己的开发工具,代码中有详细的解释。电脑不在身边建议先收藏,方便日后观看。最后祝大家技术突飞猛进,早日拿到心仪的offer。

引入依赖

<!--easyexcel依赖,导出excel-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>2.1.6</version>
</dependency>

<!--google提供的对数组进行操作的依赖-->
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>21.0</version>
</dependency>

<!--实体类@Data所需依赖-->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>

第一步创建实体类

import java.util.Date;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.alibaba.excel.annotation.format.NumberFormat;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import lombok.Data;
import lombok.experimental.Accessors;

@Data//自动生成get、set、tostring
@Accessors(chain = true)//支持set方法返回一个对象,可以连点
@ColumnWidth(20)// 表示列宽
public class Dept {
     
    
    //@ExcelProperty中value代表列名,index代表列号
    @ExcelProperty(value = "部门编号", index = 0)
    private int deptno;

    @ExcelProperty(value = "部门地址", index = 1)
    private String loc;

    @ExcelProperty(value = "部门名称", index = 2)
    private String dname;

    //@DateTimeFormat格式化时间
    @DateTimeFormat("yyyy-MM-dd")
    @ExcelProperty(value = "创办时间", index = 3)
    private Date birthday;

    //@NumberFormat格式化数字
    @NumberFormat("#.##")
    @ExcelProperty(value = "平均薪资", index = 4)
    private double money;

}

第二步编写Controller类

import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.example.demo.po.Dept;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.excel.EasyExcel;
import com.google.common.collect.Lists;

@RestController
public class ExcelWriteController {
     

    /**
     * 下载excel文件
     */
    @GetMapping("/excel")
    public void doDownLoad(HttpServletResponse response) throws IOException {
     
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("部门信息文件", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        //以下三条语句可以转化为一条语句
        //ExcelWriterBuilder write = EasyExcel.write(response.getOutputStream(), Dept.class);
        //ExcelWriterSheetBuilder excelWriterSheetBuilder = write.sheet("部门信息");
        //excelWriterSheetBuilder.doWrite(getList());//参数为Dept的对象数组
        //可以转化为
        EasyExcel.write(response.getOutputStream(), Dept.class).sheet("部门信息").doWrite(getList());
    }

    /**
     * 构造假数据
     * @return List
     */
    private List<Dept> getList() {
     
        List<Dept> depts = Lists.newArrayList();
        for (int i = 1;i <= 20; i++ ){
     
            if(i%2==0){
     
                Dept dept = new Dept().setLoc("微信公众号:搜索" + i + "次").setDname("程序员小哲").setBirthday(new Date()).setDeptno(i * 10).setMoney(8888 + i * 1000);
                depts.add(dept);
            }else {
     
                Dept dept = new Dept().setLoc("百度:搜索"+i+"次").setDname("程序员小哲").setBirthday(new Date()).setDeptno(i*10).setMoney(8888+i*1000);
                depts.add(dept);
            }
        }
        return depts;
    }

}

三分钟搞懂alibaba的excel导出——EasyExcel_第1张图片

你可能感兴趣的:(java,java,python,excel,数据库,spring)