springboot vue导出excel 使用easypoi

article/2025/9/14 2:20:23

springboot vue导出excel

处理后端返回的文件流,下载成excl文件

vue

<el-button class="el-icon-download" type="success" @click="exportExcel()">导出</el-button>

没封装axios

exportExcel() {axios({url: 请求地址, //URL,根据实际情况来method: "get", responseType: "blob" //这里必须设置 responseType: "blob"}).then(function (res) {const link = document.createElement("a");let blob = new Blob([res.data], { type: res.data.type });// let blob = new Blob([res.data], { type: "application/vnd.ms-excel" }); //知道type也可以直接填link.style.display = "none";//设置连接let url = URL.createObjectURL(blob);link.href = url;//导出文件名称link.download = "客户表格"; //模拟点击事件link.click();document.body.removeChild(link);});}

在这里插入图片描述

没封装axios的请求返回值res let blob = new Blob([res.data], { type: res.data.type }); 这里的res.data 或者 res.data.type 必须和这里对应

导出表格内容可能显示【Object object】或者 undefined 大概率这里填的有误

封装axios

export function exportUser(params) {return service({url: "/xxx", //自己后台请求地址method: "get",responseType: 'blob',   //这里必须设置 responseType: "blob"params: params});
}
   async exportExcel() {const res = await exportExcel();if (res) {const link = document.createElement("a");let blob = new Blob([res], { type: res.type });link.style.display = "none";//设置连接link.href = URL.createObjectURL(blob);link.download = "客户表格";document.body.appendChild(link);//模拟点击事件link.click();document.body.removeChild(link);}}

在这里插入图片描述

封装过的返回值可能不一样 let blob = new Blob([res], { type: res.type }); 这里就填写对应的 返回值

后端

用的easypoi

首先引入pom依赖

   <!--easypoi导入导出--><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-base</artifactId><version>4.1.3</version></dependency><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-web</artifactId><version>4.1.3</version></dependency><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-annotation</artifactId><version>4.1.3</version></dependency>

编写实体类

@ExcelTarget("user") 
@Data
public class User {private Integer id;/*** 邮箱*/@Excel(name = "邮箱")private String mailbox;/*** 用户名*/@Excel(name = "用户名")private String userName;@Excel(name = "ip")private String ip;@Excel(name = "备注")private String remarks;

这里使用了 @Excel 是关键注解,必不可少,name表示指定生成的excel的对应列明,更多用法请求官方文档查看或者百度使用。

控制层

    @GetMapping(value = "/getUser")public void getUser(HttpServletResponse response) {List<User> users  = userService.getUser();Workbook workbook = null;ServletOutputStream outputStream = null;try {workbook = ExcelExportUtil.exportExcel(new ExportParams("表格首行名称", "sheet名称"), User.class, users);response.setCharacterEncoding("utf-8");response.setContentType("application/vnd.ms-excel;charset=utf-8");response.setHeader("content-Disposition", "attachment;fileName="+ URLEncoder.encode("导出excel名称", "UTF-8"));//  出现跨域问题 可以加这俩行  //	response.setHeader("Access-Control-Allow-Origin", "前台地址");//  response.setHeader("Access-Control-Allow-Credentials", "true");outputStream = response.getOutputStream();workbook.write(outputStream);outputStream.flush();} catch (IOException e) {e.printStackTrace();} finally {try {outputStream.close();workbook.close();} catch (IOException e) {e.printStackTrace();}}// 或者直接用封装好的工具类 网上也有很多的// ExcelUtil.exportExcel(数据list, "表格首行名称", "sheet名称", User.class, "导出excel名称", response);}

excel能够正常导出 打开也没有乱码 但是控制台可能会报错

org.springframework.http.converter.HttpMessageNotWritableException: No converter for [xxx] with preset Content-Type 'application/vnd.ms-excel;charset=utf-8'

controller的方法用放回值 方法 改成void即可

工具类

public class ExcelUtil {/*** Map集合导出** @param list     需要导出的数据* @param fileName 导出的文件名* @param response HttpServletResponse对象*/public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response) throws Exception{defaultExport(list, fileName, response);}/*** 复杂导出Excel,包括文件名以及表名(不创建表头)** @param list      需要导出的数据* @param title     表格首行标题(不需要就传null)* @param sheetName 工作表名称* @param pojoClass 映射的实体类* @param fileName  导出的文件名(如果为null,则默认文件名为当前时间戳)* @param response  HttpServletResponse对象*/public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName,HttpServletResponse response) throws Exception{defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));}/*** 复杂导出Excel,包括文件名以及表名(创建表头)** @param list           需要导出的数据* @param title          表格首行标题(不需要就传null)* @param sheetName      工作表名称* @param pojoClass      映射的实体类* @param fileName       导出的文件名* @param isCreateHeader 是否创建表头* @param response       HttpServletResponse对象*/public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName,boolean isCreateHeader, HttpServletResponse response) throws Exception{ExportParams exportParams = new ExportParams(title, sheetName);exportParams.setCreateHeadRows(isCreateHeader);defaultExport(list, pojoClass, fileName, response, exportParams);}/*** 默认导出方法** @param list         需要导出的数据* @param pojoClass    对应的实体类* @param fileName     导出的文件名* @param response     HttpServletResponse对象* @param exportParams 导出参数实体*/private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response,ExportParams exportParams) throws Exception{Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);downloadExcel(fileName, workbook, response);}/*** 默认导出方法** @param list     Map集合* @param fileName 导出的文件名* @param response HttpServletResponse对象*/private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response)throws Exception {Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);if (null != workbook) {downloadExcel(fileName, workbook, response);}}/*** Excel导出** @param fileName Excel导出* @param workbook Excel对象* @param response HttpServletResponse对象*/public static void downloadExcel(String fileName, Workbook workbook, HttpServletResponse response) throws Exception{ServletOutputStream outputStream = null;try {if (StringUtils.isEmpty(fileName)) {throw new RuntimeException("导出文件名不能为空");}response.setCharacterEncoding("utf-8");response.setHeader("content-Type", "application/vnd.ms-excel; charset=utf-8");response.setHeader("content-disposition", "attachment;fileName="+ URLEncoder.encode(fileName+".xls", "UTF-8"));//          response.setHeader("Access-Control-Allow-Origin", "前台ip");
//          response.setHeader("Access-Control-Allow-Credentials", "true");outputStream = response.getOutputStream();workbook.write(outputStream);outputStream.flush();} catch (Exception e) {log.error(e.getMessage(), e);} finally {outputStream.close();workbook.close();}}/*** 根据文件路径来导入Excel** @param filePath   文件路径* @param titleRows  表标题的行数* @param headerRows 表头行数* @param pojoClass  映射的实体类* @return*/public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws Exception{//判断文件是否存在if (StringUtils.isBlank(filePath)) {return null;}ImportParams params = new ImportParams();params.setTitleRows(titleRows);params.setHeadRows(headerRows);List<T> list = null;try {list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);} catch (NoSuchElementException e) {log.error("模板不能为空", e);} catch (Exception e) {log.error(e.getMessage(), e);}return list;}/*** 根据接收的Excel文件来导入Excel,并封装成实体类** @param file       上传的文件* @param titleRows  表标题的行数* @param headerRows 表头行数* @param pojoClass  映射的实体类* @return*/public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws Exception{if (file == null) {return null;}ImportParams params = new ImportParams();params.setTitleRows(titleRows);params.setHeadRows(headerRows);List<T> list = null;try {list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);} catch (NoSuchElementException e) {log.error("excel文件不能为空", e);} catch (Exception e) {log.error(e.getMessage(), e);}return list;}/*** 文件转List** @param file* @param pojoClass* @param <T>* @return*/public static <T> List<T> fileToList(MultipartFile file, Class<T> pojoClass) throws Exception{if (file.isEmpty()) {throw new RuntimeException("文件为空");}List<T> list = ExcelUtil.importExcel(file, 1, 1, pojoClass);if (CollectionUtils.isEmpty(list)) {throw new RuntimeException("未解析到表格数据");}return list;}
}

http://chatgpt.dhexx.cn/article/aKpXkH88.shtml

相关文章

Vue导出Excel表格信息

一、安装两个依赖包 npm install -S file-saver xlsxnpm install -D script-loader二、项目中新建一个文件夹 里面放置两个文件Blob.js和 Export2Excel.js。 同时注意Export2Excel里引用Blob的路径是否正确。 三、使用案例 3.1、使用注意事项 表头对应的键要和传过来的数据…

vue导出excel表

方法一&#xff1a;vue2.0 element UI 中 el-table 数据导出Excel 。https://blog.csdn.net/u010427666/article/details/79208145 方法二&#xff1a; 1.安装2个依赖包&#xff08;其实是3个&#xff09; npm install -S file-saver xlsxnpm install -D script-loader 2.在…

Vue导出Excel的实现方法与原理

摘要&#xff1a;本文将详细介绍前端Vue中导出Excel的方法&#xff0c;包括使用第三方库和纯前端实现两种方式。同时解释其原理&#xff0c;帮助读者了解如何在Vue项目中高效地导出Excel文件。 第三方库&#xff1a;exceljs exceljs是一款功能强大的JavaScript库&#xff0c;它…

Vue实现excel文件的导出功能(后端直接返回文件流)

✍️ 作者简介: 一个每天中午去打篮球和锻炼的前端开发。 &#x1f408;‍⬛ 两只猫&#x1f431;和一只狗的铲屎官&#x1f436; &#x1f9e3; 微博: GuoJ阝阝&#xff08;fu&#xff09; 文章目录 前言一、实现效果二、实现步骤1、添加导出按钮2、添加点击事件函数3、获取当…

vue导出excel表格(详细教程)

在开发的时候&#xff0c;会经常用的导出excel表格功能,刚好自己开发有遇到&#xff0c;就记录一下 一、安装vue-json-excel npm install vue-json-excel -S二、main.js中引入 import JsonExcel from vue-json-excel Vue.component(downloadExcel, JsonExcel)三、在代码中使…

Vue2中导出Excel

目录 方式一 &#xff1a;vue-json-excel 1、引入vue-json-excel 2、 main.js中全局注册 3、使用 4、效果图 ​​ 方式二&#xff1a;file-saver、xlsx、script-loader 1、引入依赖 2、下载并引入Blob.js和Export2Excel.js 3、使用 4、效果图 导出指定的记录 1、引…

内网建站 NAT穿透 局域网穿透

背景&#xff1a; 一直想搭建个人的博客&#xff0c;但是买云服务器一年动则几千少则几百&#xff0c;想到家里有一台空闲的笔记本&#xff0c;于是乎想到了内网穿透&#xff5e; 准备工作&#xff1a; 个人电脑一台&#xff0c;小蝴蝶内网穿透 步骤一&#xff1a; 搭建好内网博…

内网穿透是什么?

文章目录 内网穿透的目的内网穿透的阻碍如何实现知识点正向代理反向代理 参考&#xff1a; 内网穿透 知识点 内网穿透是什么&#xff0c;如何利用花生壳实现内网穿透 内容从网络上收集而来 内网穿透的目的 使得外网能够访问内网应用。 或者&#xff0c;使得 两个内网能够相互通…

内网穿透-把自己的电脑部署为公网可访问的服务器

推荐一款工具&#xff0c;能够把自己电脑上的项目暴露到公网上、把自己的项目展示给别人看看。 使用很方便&#xff0c;可免费&#xff08;有收费项目&#xff09;使用。 下载 工具叫做 cpolar &#xff0c;其下载地址为&#xff1a;https://i.cpolar.com/m/4GSo &#xff0c…

内网穿透技术有哪些(经验分享)

内网穿透技术&#xff1a;说到内网穿透&#xff0c;相信很多人肯定一知半解&#xff0c;到底什么是内网穿透呢&#xff01;什么情况下需要内网穿透呢&#xff01;接下来给大家简单的述说一下原理&#xff0c;内网穿透&#xff0c;也即 NAT 穿透&#xff0c;进行 NAT 穿透是为了…

可以实现内网穿透的几款工具

https://blog.csdn.net/qq_36468810/article/details/109219639 me批注&#xff1a;现在流行使用cpolar&#xff0c;极客工具&#xff0c;HTTPS安全的隧道穿透&#xff0c;用它来调试微信公众号&#xff0c;远程控制树梅派&#xff0c;超级方便。而且还是免费的。https://cpol…

浅谈内网穿透

内网穿透&#xff0c;也叫NAT穿透&#xff0c;进行NAT穿透是为了使具有某一个特定源ip地址和源端口号的数据包不被NAT设备屏蔽而正确路由到内网主机。 什么是【内网穿透】&#xff1f; 在当前的互联网环境中&#xff0c;由于IPv4的公网地址数量是有限的&#xff0c;无法给每一台…

内网穿透什么意思?内网穿透基础知识原理内网穿透服务器搭建可以干嘛服务器端口映射无需公网IP教程方法

内网穿透什么意思&#xff1f;内网穿透基础知识原理内网穿透服务器搭建可以干嘛服务器端口映射无需公网IP教程方法 什么是内网&#xff08;今天说点大家都能听得懂的&#xff01;&#xff01;&#xff01;&#xff09; 通常情况下&#xff0c;内网 可以简单的理解为路由器创建…

内网穿透的作用 免费内网穿透有哪些 可以用来干什么

相信有很多人都会被一个问题所困惑&#xff0c;我们在日常办公和生活中&#xff1b;一些内网访问的应用&#xff0c;如何让实现在任意外网进行链接访问呢&#xff1f;有人说可以用内网穿透 内网穿透具体是什么&#xff0c;原理是什么&#xff1b;今天我们用网云穿来做演示。内…

重启路由器可以换IP吗

想换IP有哪些方法可以实现&#xff1f;有时候IP被限制了&#xff0c;怎么换IP访问&#xff0c;重启路由器可以换IP吗&#xff1f;一般家庭的基于PPPOE拨号方式上网的,使用的是动态IP&#xff0c;可以更换IP&#xff0c;下面一起去看看如何重启路由器&#xff1a; 1、断电源重启…

矩阵切换器有哪些控制方式,有什么好处

矩阵切换器有哪些控制方式呢&#xff1f;矩阵切换器&#xff0c;可以使用网络控制&#xff0c;app控制&#xff0c;web等方式控制。 一、网络控制是指设备加一个网络模块使得在一个局域网内电脑进行控制。 二、app控制是指设备加一个app控制模块&#xff0c;使得手机&#xf…

vmware 静态ip上网 防止切换网络换ip

问题&#xff1a;因为电脑有的时候用公司wifi、手机热点、或家里的wifi进行上网。但要保证vmware虚拟机固定ip且能访问网络。 一、安装好虚拟后在菜单栏选择编辑→ 虚拟网络编辑器&#xff0c;打开虚拟网络编辑器对话框&#xff0c;选择Vmnet1 Net网络连接方式&#xff0c;随意…

手机如何远程连接服务器

所有VPS均同时支持MSTSC、VNC和手机远程控制&#xff0c;本篇为手机连接教程 0、首先下载安装APP微软的RD Celient 1、运行RD Celient &#xff0c;点击右上角的加号 2、然后在谈出的窗口选择Desktop来添加远程服务器地址 3、Host name or ip address这里填远程服务器的连接地址…

SOME/IP与SOME/IP SD规范介绍

此文标准来源于AUTOSAR_PRS_SOMEIPProtocol.pdf(R21-11)和AUTOSAR_PRS_SOMEIPServiceDiscoveryProtocol.pdf(R21-11)&#xff1b; 1. 前言 AOTOSAR – AUTomotive Open System ARchitecture&#xff0c;汽车开发系统体系结构&#xff1b; SOME/IP – Scalable service-Orient…

什么是IP转换器?我们看看IP转换器的原理与应用及了解它的一些功能和用途

海外用户访问国内网站看视频/下东西/玩游戏 &#xff1b; 代理电脑或者手机IP地址&#xff0c;显示不同地区和城市IP。 国内用户玩游戏&#xff0c;电信 联通 网通 互转 降低延迟,加快速度。 完善的加密技术&#xff0c;全面保护信息安全 特性&#xff1a;安全 稳定 高速。 可以…