使用easyexcel实现excel下载

个人记录所看
先导入pom标签

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

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.17</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>

然后要建下载一个实体类

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.BaseRowModel;
import lombok.Builder;
import lombok.Data;

/**
 * @author dou
 * @title: User
 * @projectName test
 * @description: TODO
 * @date 2021/7/16 16:45
 */
@Data
@Builder
public class User extends BaseRowModel {

    @ExcelProperty(value = "姓名",index = 0)
    private String name;

    @ExcelProperty(value = "密码",index = 1)
    private String password;

    @ExcelProperty(value = "年龄",index = 2)
    private Integer age;
}

然后写一个get 请求接口

import com.alibaba.excel.EasyExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @author dou
 * @title: UserController
 * @projectName test
 * @description: TODO
 * @date 2021/7/16 16:43
 */
@RequestMapping("/sm/excel")
@RestController
@Api(value = "/sm/excel", description = "测试")
public class UserController {
    /**
     * Author: dou
     * Description: 浏览器下载--excel
     */
    @GetMapping("/testRespExcel")
    @ApiOperation("excel下载")
    public void testRespExcel(HttpServletResponse response){
        response.addHeader("Content-Disposition", "attachment;filename=" + "huyuqiao.xlsx");
        response.setContentType("application/vnd.ms-excel;charset=gb2312");
        try {
//            从HttpServletResponse中获取OutputStream输出流
            ServletOutputStream outputStream = response.getOutputStream();
            /*
             * EasyExcel 有多个不同的read方法,适用于多种需求
             * 这里调用EasyExcel中通过OutputStream流方式输出Excel的write方法
             * 它会返回一个ExcelWriterBuilder类型的返回值
             * ExcelWriterBuilde中有一个doWrite方法,会输出数据到设置的Sheet中
             */
            EasyExcel.write(outputStream, User.class).sheet("测试数据").doWrite(getAllUser());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public List<User> getAllUser(){
        List<User> userList = new ArrayList<>();
        for (int i=0;i<100;i++){
            User user = User.builder().name("测试数据"+ i).password("ceshi").age(i).build();
            userList.add(user);
        }
        return userList;
    }
}
上一篇:使用Docker和Kubernetes将MongoDB作为微服务运行


下一篇:python编程300例之 001 反转一个3位数