首页 > 其他分享 >vue + springboot 实现Excel模板文件下载

vue + springboot 实现Excel模板文件下载

时间:2024-06-07 19:01:57浏览次数:6  
标签:downTemplate vue springboot Excel 下载 message response 模板 String

1、后端实现

1.创建用于映射模板的实体类

@Data
public class SysUserTo{
    @Pattern(regexp = "^(\\w+([-.][A-Za-z0-9]+)*){3,18}@\\w+([-.][A-Za-z0-9]+)*\\.\\w+([-.][A-Za-z0-9]+)*$", message = "邮箱格式有误")
    @Size(max = 50, message = "邮箱超出50长度")
    @NotNull(message = "邮箱")
    @ExcelProperty(value = "email", index = 0)
    private String email;

//    @Size(max = 5, message = "姓名超出5长度")
//    @NotNull(message = "姓名")
//    @ExcelProperty(value = "name", index = 1)
//    private String name;
//
//    @Size(max = 2, message = "昵称超出2长度")
//    @NotNull(message = "昵称")
//    @ExcelProperty(value = "nickname", index = 2)
//    private String nickname;
}

2.引入easyexcel依赖

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

3.控制器类编写下载接口

    @ApiOperation("Excel模板下载")
    @GetMapping("/downTemplate")
    public void downTemplate(HttpServletResponse response) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman
        response.setContentType("application/vnd.ms-excel");
//        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("用户导入模板", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xlsx");
        List<SysUserTo> list = new ArrayList<SysUserTo>();
        SysUserTo downloadData = new SysUserTo();
        downloadData.setEmail("[email protected]");
        list.add(downloadData);
        EasyExcel.write(response.getOutputStream(), SysUserTo.class).sheet("Sheet1")
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) //添加自适应列宽度
                .doWrite(list);
    }

}

2、前端实现

1.添加下载按钮

<el-button type="success" size="mini" icon="el-icon-download" @click="downTemplate">Excel模版下载</el-button>

2.发送下载请求

  methods: {
    downTemplate() {
      return request({
        url: `${api_name}/downTemplate`,
        method: 'get',
        responseType: 'blob'
      }).then(response => {
        this.$message.success('模板下载成功')
      })
    }
 }

3.测试

在这里插入图片描述
在这里插入图片描述

标签:downTemplate,vue,springboot,Excel,下载,message,response,模板,String
From: https://blog.csdn.net/weixin_44071461/article/details/139533321

相关文章

  • vue 连接mqtt
    下载mqtt服务:npminstallmqttconstmqttConfig={//你的MQTT服务器配置protocolId:'MQTT',protocolVersion:4,clean:true,clientId:'xxxx',reconnectPeriod:1000,connectTimeout:60*1000,//will:{//topic:�......
  • Vue Router 4与路由管理实战
    title:VueRouter4与路由管理实战date:2024/6/7updated:2024/6/7excerpt:这篇文章介绍了如何在Vue.js应用中利用VueRouter实现单页面应用的路由管理,包括配置路由、导航守卫的使用、路由懒加载以优化性能以及动态路由的实现方法,旨在提升用户体验和应用加载效率categorie......
  • Redis-12-SpringBoot集成Redis哨兵模式
    Redis哨兵的配置,参考我这篇文章:Redis-5-高可用1.背景网上搜半天没搜到份好用的,自己整理了下方便以后复制,基于springboot2.6.13。lettucecommons-pool22.集成2.1导入pom<!--spring-redis--><dependency><groupId>org.springframewor......
  • 基于SpringBoot+Vue的网上花店系统设计与实现(源码+lw+部署文档+讲解等)
    文章目录前言详细视频演示项目运行截图技术框架后端采用SpringBoot框架前端框架Vue可行性分析系统测试系统测试的目的系统功能测试数据库表设计代码参考数据库脚本为什么选择我?获取源码前言......
  • Vue父子组件生命周期执行顺序
    顺序执行顺序:父组件先创建,然后子组件创建;子组件先挂载,然后父组件挂载,即“父beforeCreate->父create->子beforeCreate->子created->子mounted->父mounted”。在单一组件中,钩子的执行顺序是beforeCreate->created->mounted->…->destroyed,但当父子组件嵌套时,父组件和......
  • 基于SpringBoot+Vue的校园驿站管理系统设计与实现(源码+lw+部署文档+讲解等)
    文章目录前言详细视频演示项目运行截图技术框架后端采用SpringBoot框架前端框架Vue可行性分析系统测试系统测试的目的系统功能测试数据库表设计代码参考数据库脚本为什么选择我?获取源码前言......
  • 基于SpringBoot+Vue的二手手机交易平台的详细设计和实现(源码+lw+部署文档+讲解等)
    文章目录前言详细视频演示项目运行截图技术框架后端采用SpringBoot框架前端框架Vue可行性分析系统测试系统测试的目的系统功能测试数据库表设计代码参考数据库脚本为什么选择我?获取源码前言......
  • Vue3等比例缩放图片组件
    本文由ChatMoney团队出品有些情况我们需要在各种刁钻的情况下都要保持图片比例不变,比如用户缩放窗口等改变布局的情况。实现原理就是通过容器的宽度和内边距在保持你想要的比例。以下是基础功能的组件示例:<template><divstyle="position:relative":style="ratioStr"......
  • vue+cesium+heatmap.js 实现热力图
    效果如下图:1.安装heatmap.jsnpmi heatmap.js官网:heatmap.js:DynamicHeatmapsfortheWeb(patrick-wied.at)2.把这两个文件放到项目里heatmap.jsimportutilfrom"./util"importh337from'heatmap.js'/***@description二维热力图类,基于h337类扩展*@......
  • vue3+TypeScript
    1.Vue3简介2020年9月18日,Vue.js发布版3.0版本,代号:OnePiece(n经历了:4800+次提交、40+个RFC、600+次PR、300+贡献者官方发版地址:Releasev3.0.0OnePiece·vuejs/core截止2023年10月,最新的公开版本为:3.3.41.1.【性能的提升】打包大小减少41%。初次渲染快......