【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",
	  });
	});
相关推荐
清灵xmf15 分钟前
揭开 Vue 3 中大量使用 ref 的隐藏危机
前端·javascript·vue.js·ref
学习路上的小刘29 分钟前
vue h5 蓝牙连接 webBluetooth API
前端·javascript·vue.js
&白帝&29 分钟前
vue3常用的组件间通信
前端·javascript·vue.js
陈大爷(有低保)44 分钟前
UDP Socket聊天室(Java)
java·网络协议·udp
kinlon.liu1 小时前
零信任安全架构--持续验证
java·安全·安全架构·mfa·持续验证
王哲晓1 小时前
Linux通过yum安装Docker
java·linux·docker
java6666688881 小时前
如何在Java中实现高效的对象映射:Dozer与MapStruct的比较与优化
java·开发语言
Violet永存1 小时前
源码分析:LinkedList
java·开发语言
执键行天涯1 小时前
【经验帖】JAVA中同方法,两次调用Mybatis,一次更新,一次查询,同一事务,第一次修改对第二次的可见性如何
java·数据库·mybatis
Jarlen2 小时前
将本地离线Jar包上传到Maven远程私库上,供项目编译使用
java·maven·jar