首页 > 其他分享 >springboot导出数据到Excel表格,使用EasyExcel

springboot导出数据到Excel表格,使用EasyExcel

时间:2022-10-15 15:45:40浏览次数:53  
标签:20 springboot Excel EasyExcel 导出 private Student response String

1.导入依赖

导出方法需要使用到fastJson的依赖,这里也直接导入

点击查看代码
<!--阿里的easyexcel-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.2.0-beta1</version>
            <scope>compile</scope>
        </dependency>
        <!-- 阿里fastjson包JSON转换-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>

2.准备导出数据

这里简单创建一个Student的实体类

在导出的数据中,要是不想有某个属性,可以在该属性上面添加@ExcelIgnore注解,

同样,若在导出的数据中想要某个属性,可以在该属性上面添加@ExcelProperty("姓名")注解

若在属性上面啥属性也没有,默认也是会导出改属性的数据,但是表头就是这个属性名。

点击查看实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student implements Serializable {
    private static final long serialVersionUID = 1L;

    @ExcelProperty("Id")
    private String id;

    @ExcelProperty("姓名")
    private String name;

    @ExcelProperty("学号")
    private String studentNo;

    @ExcelProperty("电话")
    private String phone;

    @ExcelProperty("性别")
    private Integer sex;

    @ExcelIgnore
    private Integer age;

    private String h;
}

这里再简单编写一个service方法,模拟查询到的数据;

点击查看Service方法
import java.util.ArrayList;
import java.util.List;

@Service
public class ExportExcelService {

    public List<Student> getData(){
        Student student = new Student("001","张三","191026","13299998888",1,20,"1");
        Student student1 = new Student("002","李四","191027","13299993333",0,20,"1");
        Student student2 = new Student("003","王五","191028","13293338888",1,20,"1");
        return new ArrayList<Student>(){{
            add(student);
            add(student1);
            add(student2);
        }};
    }
}

3.编写controller方法

这里导出的方法就直接写在controller里了,从service查数据。

这里其实需要改动的就只有两个地方,一个就是导出数据集合的泛型,二是导出的数据集合。

EasyExcel.write(response.getOutputStream(), Student.class) .autoCloseStream(Boolean.FALSE) .sheet("导出详情") .doWrite(studentList);

点击查看代码
@RestController
@RequestMapping("/export")
public class ExportExcelController {
    @Autowired
    private ExportExcelService exportExcelService;

    @GetMapping("/")
    public void export(HttpServletResponse response) throws IOException {
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码
            String fileName = URLEncoder.encode("导出", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            List<Student> studentList = exportExcelService.getData();
            // 这里需要设置不关闭流
            EasyExcel.write(response.getOutputStream(), Student.class)
                    .autoCloseStream(Boolean.FALSE)
                    .sheet("导出详情")
                    .doWrite(studentList);
        } catch (Exception e) {
            // 重置response
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            Map<String, String> map = new HashMap();
            map.put("status", "failure");
            map.put("message", "下载文件失败" + e.getMessage());
            response.getWriter().println(JSON.toJSONString(map));
        }
    }
}

4.最后附上效果图

当然,默认的文件名,还有表头的宽高,数据一行的宽高都是可以设置的。都有相应的注解,加在实体类上就可以了,这里就简单逻辑几个:

设置表头的高度:@HeadRowHeight(20)

设置数据的高度:@ContentRowHeight(20)

设置数据的宽度:@ColumnWidth(25)

标签:20,springboot,Excel,EasyExcel,导出,private,Student,response,String
From: https://www.cnblogs.com/huoyl/p/16794297.html

相关文章

  • Excel导入数据异常Cannot get a text value from a numeric cell解决办法
    POI操作Excel时偶尔会出现Cannotgetatextvaluefromanumericcell的异常错误。异常原因:Excel数据Cell有不同的类型,当我们试图从一个数字类型的Cell读取出一个字符串......
  • 微信直接登录(springboot+wx)
    方法1源码地址快速开始配置文件编写weixin:appid:wx5bbcac13dbec9fe0secret:a1ed9ae66689bbaf50b0e04630ec1472业务的编写,前端传来一个code,发送到......
  • asp.net core +vue 导出excel
      定义Excel专用特性名(应用导出表格列名) 引入Nuget包1.Microsoft.AspNetCore.Hosting2.DotNetCore.NPOI   引入包usingNPOI.SS.UserModel;usingN......
  • SpringBoot之Logback 日志文件配置
    Logback概述Logback是由log4j的创始人设计的另一个开源的日志组件,官方网站是:http://logback.qos.ch。它当前分为下面下个模块:logback-core:其他两个模块的基础模块l......
  • springboot MP代码生成器
    1、需要的依赖和版本号(我这个是项目完成后的全部依赖,只参照需要的依赖即可)<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0......
  • springboot整合easyExcel实现不固定列导入
    1、pom.xml文件引入easyExcel<!--阿里开源easyExcel依赖--><dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><versio......
  • SpringBoot整合
    1、JedisPoolConfig(这个是配置连接池)2、RedisConnectionFactory这个是配置连接信息,这里的RedisConnectionFactory是一个接口,我们需要使用它的实现类,在SpringData......
  • Excel数据筛选后,编号数字自动从1重新开始
    如下,A列是手动输入的编号,B列需要进行筛选筛选 “大猫03”、“大猫05”、“大猫09”,结果如下:要求:无论是筛选前,还是筛选后。编号均为1、2、3……​,请问如何实现?大猫支招1.取......
  • DEMO:Excel 下载
    效果:......
  • Excel批量提取每行最后一个出现的数据,最全解析!
    Excel情报局职场联盟Excel生产挖掘分享Excel基础技能Excel爱好者大本营用1%的Excel基础搞定99%的职场问题做一个超级实用的Excel公众号Excel是门手艺玩转需要勇气数万Excel......