koa2+vue3通过exceljs实现数据导出excel文档

  1. 服务端安装exceljs依赖
npm i exceljs
  1. 服务端实现代码
    1. 实现导出excel文件工具方法
const ExcelJS = require('exceljs');
/**
 * @description: 导出excel文件
 * @param {*} fileHeader  导出的表头
 * @param {*} data 导出的数据
 * @param {*} ctx 上下文对象
 * @return {*}
 */
async function exportExcel(fileHeader, data, ctx){
  const workbook = new ExcelJS.Workbook();
  const sheet = workbook.addWorksheet();
  sheet.columns = fileHeader.map((item,index)=>{
    return {
      header: item.header,
      key: item.key,
      width: item.width||25,
    }
  })
  data.forEach((item,index)=>{
    sheet.addRow(item);
  })
  
  // 生成Excel文件
  const buffer = await workbook.xlsx.writeBuffer();

  ctx.status = 200;
  ctx.set('Content-Type', 'application/vnd.openxmlformats');
  ctx.attachment('example.xlsx');
  ctx.body = buffer;

}

module.exports = exportExcel;
b. 使用案例
await exportExcel(
      [
        { header: "Name", key: "name", width: 20 },
        { header: "Age", key: "age", width: 10 },
        { header: "Email", key: "email" },
      ],
      [
        { name: "测试数据1", age: 320, email: "alice@example.com" },
        { name: "测试数据2", age: 330, email: "alice@example.com" },
        { name: "测试数据3", age: 340, email: "alice@example.com" },
      ],
      ctx
    );
  1. 客户端代码实现
    1. 创建下载excel工具方法
/**
 * @description: 下载excel方法
 * @param {String} filename
 * @param {Blob} blob
 */
async function exportExcel(filename = "导出文件", blob: Blob) {
  const url = window.URL.createObjectURL(blob);
  const link = document.createElement("a");
  link.href = url;
  link.download = filename; // 设置下载的文件名
  document.body.appendChild(link); // 需要添加到DOM中才能生效
  link.click();
  document.body.removeChild(link);
  window.URL.revokeObjectURL(url); // 释放内存
}

export default exportExcel;
b. 接口请求方法封装
export const exportTableApi = (url: string, params: any = {}) => {
  return http.get<any>(url + "/export-to-excel", params, {
    headers: {
      Accept:
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
    },
    **responseType: "blob",**
  });
};
c. 使用案例
/**
   * @description 导出表格数据
   * @return void
   * */
  const exportData = async (apiUrl, params) => {
    const data = await exportTableApi(apiUrl, params);
    const blob = new Blob([data as any], {
      type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
    });
    exportExcel(tableName, blob);
  };

相关推荐

  1. koa2+vue3通过exceljs实现数据导出excel文档

    2024-03-17 20:28:05       22 阅读
  2. vue3 导入excel数据

    2024-03-17 20:28:05       15 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-17 20:28:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-17 20:28:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-17 20:28:05       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-17 20:28:05       20 阅读

热门阅读

  1. IOS面试题object-c 146-150

    2024-03-17 20:28:05       20 阅读
  2. 循序渐进理解数据库基本概念

    2024-03-17 20:28:05       25 阅读
  3. 分布式微服务 - 总概

    2024-03-17 20:28:05       21 阅读
  4. Golang 泛型定义类型的时候前面 ~ 代表什么意思

    2024-03-17 20:28:05       21 阅读
  5. 音视频实战---音视频频解码

    2024-03-17 20:28:05       23 阅读
  6. 我的创作纪念日

    2024-03-17 20:28:05       20 阅读
  7. 静态绑定和动态绑定的介绍?

    2024-03-17 20:28:05       21 阅读
  8. 树莓派 ubuntu 23.10 mantic 换阿里云源

    2024-03-17 20:28:05       20 阅读
  9. Microsoft VBA Excel 提取相同名称的整列数据

    2024-03-17 20:28:05       18 阅读
  10. ./experiment.sh: line 64: python3.6: command not found

    2024-03-17 20:28:05       21 阅读
  11. 435. 无重叠区间

    2024-03-17 20:28:05       22 阅读