【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",
	  });
	});
相关推荐
keke1023 分钟前
Java【14_2】接口(Comparable和Comparator)、内部类
java·开发语言
多云的夏天24 分钟前
前端:VUE-(0)-环境搭建和helloworld
前端·javascript·vue.js
CN.LG33 分钟前
Java 乘号来重复字符串的功能
java·开发语言
萌新下岸多多关照38 分钟前
Java中synchronized 关键字
java·开发语言
中国lanwp40 分钟前
使用Maven部署WebLogic应用
java·maven
开开心心就好1 小时前
Word图片格式调整与转换工具
java·javascript·spring·eclipse·pdf·word·excel
GanGuaGua1 小时前
Vue3:脚手架
前端·javascript·css·vue.js·vue
CGG921 小时前
【单例模式】
android·java·单例模式
苦学编程的谢1 小时前
多线程代码案例-1 单例模式
java·开发语言·单例模式
yaoxin5211231 小时前
80. Java 枚举类 - 使用枚举实现单例模式
java·开发语言·单例模式