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

一、java

1、添加依赖:

复制代码
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.16</version>
</dependency>

2、封装方法

java 复制代码
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.poi.excel.BigExcelWriter;
import cn.hutool.poi.excel.ExcelUtil;

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

public class ExcelUtils {
    /**
     * 导出excel
     */
    public static void downloadExcel(List<Map<String, Object>> list, HttpServletResponse response) throws IOException {
        String tempPath =System.getProperty("java.io.tmpdir") + IdUtil.fastSimpleUUID() + ".xlsx";
        File file = 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")
public void exportListData(@RequestBody List<Map<String,Object>> list,HttpServletResponse response){
    try {
        ExcelUtils.downloadExcel(list, response);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

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

1、前端API写法:

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

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

html 复制代码
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",
	  });
	});
相关推荐
Nelson_hehe2 小时前
Java基础第四章、面向对象
java·语法基础·面向对象程序设计
Thomas_YXQ2 小时前
Unity3D Lua集成技术指南
java·开发语言·驱动开发·junit·全文检索·lua·unity3d
ShiinaMashirol3 小时前
代码随想录打卡|Day27(合并区间、单调递增的数字、监控二叉树)
java·算法
东阳马生架构5 小时前
Nacos简介—3.Nacos的配置简介
java
北极的企鹅885 小时前
XML内容解析成实体类
xml·java·开发语言
oioihoii5 小时前
C++23 中 static_assert 和 if constexpr 的窄化布尔转换
java·jvm·c++23
山海上的风5 小时前
Vue里面elementUi-aside 和el-main不垂直排列
前端·vue.js·elementui
聂 可 以5 小时前
调整IntelliJ IDEA当前文件所在目录(包路径)的显示位置
java·ide·intellij-idea
东阳马生架构5 小时前
Sentinel源码—7.参数限流和注解的实现一
java·sentinel