springboot+vue使用easyExcel实现导出功能

vue部分

javascript 复制代码
// 导出计算数据
      exportDataHandle(id) {
        this.$http({
          url: this.$http.adornUrl('/xxx/xxx/exportCalDataExcel'),
          method: 'post',
          data: this.$http.adornData({'id': id}),
          responseType: 'blob', // 重要:告诉axios我们希望接收二进制数据
        }).then(({data}) => {
          const blob = new Blob([data], {type: 'data:application/vnd.ms-excel;base64;charset=utf-8'});
          const downloadElement = document.createElement('a');
          const href = window.URL.createObjectURL(blob); // 创建下载的链接
          downloadElement.href = href;
          downloadElement.download = 'abc' + '.xlsx'; // 下载后文件名
          document.body.appendChild(downloadElement);
          downloadElement.click(); // 点击下载
          document.body.removeChild(downloadElement); // 下载完成移除元素
          window.URL.revokeObjectURL(href); // 释放掉blob对象

          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
          })
        })
      },

Java部分

1导入maven依赖

这里要注意,由于easyexcel的包中包含了poi的依赖,所以在引入easyexcel包之前,需要删掉mavne中poi的包,否则会出现依赖异常:com.alibaba.excel.exception.ExcelGenerateException: java.lang.NoSuchFieldError: Factory

javascript 复制代码
        <dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>easyexcel</artifactId>
			<version>3.0.5</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.79</version>
		</dependency>
		<dependency>
			<groupId>cn.hutool</groupId>
			<artifactId>hutool-all</artifactId>
			<version>5.8.10</version>
		</dependency>

2 编写代码

java 复制代码
import com.alibaba.excel.EasyExcel;   
 /**
     * 导出 计算数据
     */
    @RequestMapping("/exportCalDataExcel")
    public void exportCalDataExcel(@RequestBody ExamEntity exam, HttpServletResponse response) throws IOException {
        //1 数据获取
        List<CalDataDto> dataList = answerScoreService.getAllCalData(exam);

        //2 设置响应头
        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");

        // 3 将数据写入excel中
        EasyExcel.write(response.getOutputStream(), CalDataDto.class)
                .sheet("Sheet1")
                .doWrite(dataList);
    }
java 复制代码
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;

@Data
public class CalDataDto {
    @ExcelProperty("变量名")
//    @ColumnWidth(10)
    private String name;

    @ExcelProperty("数据")
    private Double num;
}
相关推荐
Q_970956395 分钟前
java+vue+SpringBoo校园失物招领网站(程序+数据库+报告+部署教程+答辩指导)
java·数据库·vue.js
仰望星空@脚踏实地9 分钟前
Spring Boot Web 服务单元测试设计指南
spring boot·后端·单元测试
羊小猪~~21 分钟前
数据库学习笔记(十七)--触发器的使用
数据库·人工智能·后端·sql·深度学习·mysql·考研
翻滚吧键盘24 分钟前
vue 条件渲染(v-if v-else-if v-else v-show)
前端·javascript·vue.js
用户83249514173228 分钟前
JAVA 版本多版本切换 - 傻瓜式操作工具
后端
estarlee31 分钟前
随机昵称网名API接口教程:轻松获取百万创意昵称库
后端
明天好,会的35 分钟前
跨平台ZeroMQ:在Rust中使用zmq库的完整指南
开发语言·后端·rust
追逐时光者42 分钟前
C#/.NET/.NET Core优秀项目和框架2025年6月简报
后端·.net
叹一曲当时只道是寻常1 小时前
vue中添加原生右键菜单
javascript·vue.js
一勺菠萝丶2 小时前
Spring Boot + MyBatis/MyBatis Plus:XML中循环处理List参数的终极指南
xml·spring boot·mybatis