欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

SpringBoot+Mybatis-plus实现excel文件导出下载

时间:2023-08-09

目录

整体结构

加入依赖

实体类

Controller

DictTypeMapper

DictTypeMapper.xml

application.yml(配置文件)

运行


整体结构

加入依赖

在pom.xml中加入此依赖

com.alibaba easyexcel 2.1.7

实体类

在类上面加入一下注解,可以根据自己需求修改列宽等数据。

@ColumnWidth(20) //列宽@ContentRowHeight(20)//数据行高@HeadRowHeight(30)//表头高@Data@ExcelIgnoreUnannotated //解决不加ExcelProperty注解的,也会出现在excel中

在想要展现在Excel表中属性加上注解 

@ExcelProperty(value = "字典名称",index = 1) //value:在excel中列名,index:次序

 具体表现为下图:

package com.example.ideaworkplace.pojo;import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;import com.alibaba.excel.annotation.ExcelProperty;import com.alibaba.excel.annotation.write.style.ColumnWidth;import com.alibaba.excel.annotation.write.style.ContentRowHeight;import com.alibaba.excel.annotation.write.style.HeadRowHeight;import com.baomidou.mybatisplus.annotation.FieldFill;import com.baomidou.mybatisplus.annotation.IdType;import com.baomidou.mybatisplus.annotation.TableField;import com.baomidou.mybatisplus.annotation.TableId;import lombok.Data;import java.util.Date;@ColumnWidth(20) //列宽@ContentRowHeight(20)//数据行高@HeadRowHeight(30)//表头高@Data@ExcelIgnoreUnannotated //解决不加ExcelProperty注解的,也会出现在excel中public class DictType { @ExcelProperty(value = "主键id",index = 0) @TableId(type = IdType.AUTO) private Long id; @ExcelProperty(value = "字典名称",index = 1) private String dictName; @ExcelProperty(value = "字典类型",index = 2) private String dictType; @ExcelProperty(value = "字典状态",index = 3) private String status; private String createBy; @TableField(fill = FieldFill.INSERT) //插入时自动填充 private Date createTime; private String updateBy; @TableField(fill = FieldFill.UPDATE) //更新时自动填充 private Date updateTime; @ExcelProperty(value = "备注",index = 4) private String remark;}

Controller

package com.example.ideaworkplace.controller;import java.io.IOException;import java.net.URLEncoder;import java.util.List;import javax.annotation.Resource;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.alibaba.excel.EasyExcel;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;import com.example.ideaworkplace.mapper.DictTypeMapper;import com.example.ideaworkplace.pojo.DictType;import io.swagger.v3.oas.annotations.Operation;import io.swagger.v3.oas.annotations.tags.Tag;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@Tag(name = "字典类型操作")@RestController@RequestMapping("/dictType/page")public class DictTypeController { @Resource private DictTypeMapper dictTypeMapper; @RequestMapping("/typeExport") @Operation(summary = "导出字典类型",description = "export dict type") public void typeExport(HttpServletRequest request, HttpServletResponse response){ try { String filename = "字典类型"; String userAgent = request.getHeader("User-Agent"); if(userAgent.contains("MSIE")||userAgent.contains("Trident")){ filename = URLEncoder.encode(filename,"UTF-8"); }else { filename = new String(filename.getBytes("UTF-8"),"ISO-8859-1"); } response.setContentType("application/json.ms-exce"); response.setCharacterEncoding("utf-8"); response.addHeader("Content-Disposition","filename = " + filename + ".xlsx"); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.select("*"); List dictTypeList = dictTypeMapper.selectList(queryWrapper); EasyExcel.write(response.getOutputStream(),DictType.class).sheet("sheet").doWrite(dictTypeList); } catch (IOException e) { e.printStackTrace(); } } }

DictTypeMapper

package com.example.ideaworkplace.mapper;import com.baomidou.mybatisplus.core.mapper.baseMapper;import com.example.ideaworkplace.pojo.DictType;public interface DictTypeMapper extends baseMapper {}

DictTypeMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>

application.yml(配置文件)

spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/mysql?serverTimezone=Asia/Shanghai username: root password: rootmybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #开启SQL语句打印 mapper-locations: /mapper/*Mapper.xml global-config: db-config: id-type: auto #全局自增主键

运行

运行之后在网页根据controller中的@RequestMapping注解输入网址,例如我的是:

loclahost:8080/dictType/page/typeExport

运行之后如下

 下载之后打开excel如下

 如果本篇文章对您有所帮助,来个一键三连吧好兄弟,笔芯!

注:本文可能会涉及一些其他依赖包引入,如有问题可以私信或者评论,已读必回。

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。