【java导出xlsx\hutool】java导出List<Map<String, Object>>类型数据到xlsx

news/2024/7/24 12:30:25 标签: java, list, vue.js, word

java_0">一、java

1、添加依赖:

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.16</version>
</dependency>

2、封装方法

java">word">import cn.hutool.core.io.IoUtil;
word">import cn.hutool.core.util.IdUtil;
word">import cn.hutool.poi.excel.BigExcelWriter;
word">import cn.hutool.poi.excel.ExcelUtil;

word">import javax.servlet.ServletOutputStream;
word">import javax.servlet.http.HttpServletResponse;
word">import java.io.File;
word">import java.io.IOException;
word">import java.util.List;
word">import java.util.Map;

word">public word">class ExcelUtils {
    /**
     * 导出excel
     */
    word">public word">static word">void downloadExcel(List<Map<String, Object>> list, HttpServletResponse response) word">throws IOException {
        String tempPath =System.getProperty("java.io.tmpdir") + IdUtil.fastSimpleUUID() + ".xlsx";
        File file = word">new File(tempPath);
        BigExcelWriter writer= ExcelUtil.getBigWriter(file);
        writer.write(list, true);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        //test.xls是弹出下载对话框的文件名,不能为中文,中文请自行编码
        response.setHeader("Content-Disposition","attachment;filename=file.xlsx");
        ServletOutputStream out=response.getOutputStream();
        // 终止后删除临时文件
        file.deleteOnExit();
        writer.flush(out, true);
        //关闭输出Servlet流
        IoUtil.close(out);
    }
}

2、接口

java">@PostMapping("/exportList")
word">public word">void exportListData(@RequestBody List<Map<String,Object>> list,HttpServletResponse response){
    word">try {
        ExcelUtils.downloadExcel(list, response);
    } word">catch (IOException e) {
        word">throw word">new RuntimeException(e);
    }
}

二、vue(代码块,仅参考)

1、前端API写法:

export function exportList(data) {
  return request({
    url: '/xxx/xxx/exportList/',
    method: 'post',
    responseType: "blob",
    data: data
  })
}

2、页面调用方法和导出文件

exportList(this.data).then(rsp => {
	  let blob = new Blob([rsp], {
	    type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
	  });
	  let url = window.URL.createObjectURL(blob);
	  window.location.href = url;
	  this.$message({
	    showClose: true,
	    message: "文件下载成功",
	    type: "success",
	  });
	});

http://www.niftyadmin.cn/n/5459950.html

相关文章

只出现一次的数字 II

题目链接 只出现一次的数字 II 题目描述 注意点 nums中&#xff0c;除某个元素仅出现一次外&#xff0c;其余每个元素都恰出现三次设计并实现线性时间复杂度的算法且使用常数级空间来解决此问题 解答思路 本题与只出现一次的数字的数字类似&#xff0c;区别是重复的数字会…

Linux 开发环境以及编译链接

再谈编译链接 C函数重载与编译链接-CSDN博客 之前我已经写过文章简单介绍了编译链接要做的一些操作。现在为了能更好的理解我们平时的开发环境&#xff0c;我会在Linux系统上完整地走一遍流程。 环境描述 我们使用普通用户在Linux上进行操作&#xff0c;先写一段测试代码。 …

数字永生和数字孪生

随着科技的飞速发展&#xff0c;人类对于生命延续的探索从未停歇。数字永生&#xff0c;作为一种新兴的概念&#xff0c;承诺将人类的意识和记忆永久保存于数字世界之中&#xff0c;引发了广泛的讨论和关注。然而&#xff0c;当我们深入思考这一命题时&#xff0c;不难发现&…

鸿蒙原生应用开发-网络管理HTTP数据请求

一、场景介绍 应用通过HTTP发起一个数据请求&#xff0c;支持常见的GET、POST、OPTIONS、HEAD、PUT、DELETE、TRACE、CONNECT方法。 二、接口说明 HTTP数据请求功能主要由http模块提供。 使用该功能需要申请ohos.permission.INTERNET权限。 涉及的接口如下表&#xff0c;具体的…

Redis入门到实战-第十九弹

Redis入门到实战 Redis中Count-min-sketch数据类型常见操作官网地址Redis概述Count-min-sketch常见操作更新计划 Redis中Count-min-sketch数据类型常见操作 完整命令参考官网 官网地址 声明: 由于操作系统, 版本更新等原因, 文章所列内容不一定100%复现, 还要以官方信息为准…

【Go】Context

https://www.fengfengzhidao.com/article/WdlGxI0BEG4v2tWkq3bD#go%E8%AF%AD%E8%A8%80%E7%9A%84context https://blog.csdn.net/weixin_52690231/article/details/124518402 https://blog.csdn.net/m0_57960197/article/details/132529334 基于源码 学一点&#xff0c;整一点&a…

HarmonyOS实战开发-如何实现一个支持加减乘除混合运算的计算器。

介绍 本篇Codelab基于基础组件、容器组件&#xff0c;实现一个支持加减乘除混合运算的计算器。 说明&#xff1a; 由于数字都是双精度浮点数&#xff0c;在计算机中是二进制存储数据的&#xff0c;因此小数和非安全整数&#xff08;超过整数的安全范围[-Math.pow(2, 53)&#…

富格林:摸清诱导受害原因正规防范

富格林悉知&#xff0c;在交易中&#xff0c;亏损和盈利是并存的&#xff0c;不存在永远的盈利&#xff0c;也没有长久的亏损&#xff0c;想要有效的减少亏损增加盈利&#xff0c;投资者需要正规的做单投资&#xff0c;增加盈利的机会。但有一些投资者对于技术的掌握是好的&…