【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",
	  });
	});
相关推荐
华仔啊1 分钟前
乐观锁、悲观锁和分布式锁,你用对了吗?
java·分布式
自由的疯2 分钟前
Java下载图片并导出压缩包
java·后端·trae
tanxiaomi22 分钟前
Spring面试宝典:Spring IOC的执行流程解析
java·spring·面试
郭少26 分钟前
🔥 我封装了一个会“思考”的指令!Element-Plus Tooltip 自动检测文本溢出,优雅展示
前端·vue.js·性能优化
郭少29 分钟前
🔥 放弃 vw!我在官网大屏适配中踩了天坑,用 postcss-px-to-viewport-8-plugin 实现了 Rem 终极方案
vue.js·性能优化·nuxt.js
咸虾米31 分钟前
微信小程序通过uni.chooseLocation打开地图选择位置,相关设置及可能出现的问题
vue.js·微信小程序
NightDW38 分钟前
连续周更任务模块的设计与实现
java·后端·mysql
华仔啊40 分钟前
什么情况下用线程池,怎么用?看完就会
java·后端
鹏多多41 分钟前
深入解析vue的transition过渡动画使用和优化
前端·javascript·vue.js
灵魂猎手43 分钟前
8. Mybatis插件体系
java·后端·源码