使用vue3框架vue-next-admin导出表格excel(带图片)

打印 上一主题 下一主题

主题 574|帖子 574|积分 1722

想要使用vue3导出表格内容并且图片显示在表格中(如图): 

步骤如下:
下载安装插件:
    安装命令:npm install js-table2excel
引入插件:
  1.         import table2excel from 'js-table2excel'
复制代码
使用插件     
        直接上代码:
        onBatchExport方法中数据的key值要与data中保持一致,否则数据无法获取到,打印出的效果就回为undefined。
我写了两种导出:
一种是全部导出;另一种是选中导出。
选中导出需要联合列表中的复选框,给复选框绑定onchange事件,声明变量,把选中的数据存到到变量中,然后给导出的数据换成变量,就可以只导出选中的列表。

下面是全部导出: 
  1. <template>
  2. <div>
  3.      <el-button size="big" type="primary" @click="open">导出</el-button>
  4. </div>
  5. </template>
  6. <script>
  7. import table2excel from "js-table2excel";
  8. // 账号列表总数据
  9. const datum = ref([]);
  10. // 导出框显示
  11. // 导出全部数据的函数
  12. const exportBtn = () => {
  13.   const column = [
  14.     {
  15.       title: "序号",
  16.       key: "id",
  17.       type: "text",
  18.     },
  19.     {
  20.       title: "店长名称",
  21.       key: "name",
  22.       type: "text",
  23.     },
  24.     {
  25.       title: "商家门头照",
  26.       key: "img",
  27.       type: "image",
  28.       width: 120,
  29.       height: 150,
  30.     },
  31.     {
  32.       title: "公司名称",
  33.       key: "title",
  34.       type: "text",
  35.     },
  36.     {
  37.       title: "订单号",
  38.       key: "order_num",
  39.       type: "text",
  40.     },
  41.     {
  42.       title: "应付",
  43.       key: "money",
  44.       type: "text",
  45.     },
  46.     {
  47.       title: "实际支付",
  48.       key: "fact_money",
  49.       type: "text",
  50.     },
  51.     {
  52.       title: "类型",
  53.       key: "type",
  54.       type: "text",
  55.       formatter: (row) => {
  56.         const typeMap = {
  57.           1: "微信",
  58.           2: "支付宝",
  59.           3: "抖音",
  60.         };
  61.         return typeMap[row.type] || "未知";
  62.       },
  63.     },
  64.     {
  65.       title: "平台订单号",
  66.       key: "terrace_order_num",
  67.       type: "text",
  68.       width: 500, // 自动调整宽度
  69.       numberFormat: "text",
  70.     },
  71.     {
  72.       title: "创建时间",
  73.       key: "create_time",
  74.       type: "text",
  75.     },
  76.     {
  77.       title: "下单时间",
  78.       key: "pay_time",
  79.       type: "text",
  80.     },
  81.   ];
  82.   // 深拷贝数据
  83.   const tableDatas = JSON.parse(JSON.stringify(datum.value));
  84.   // 处理数据,将 type 字段的值转换为对应的文字
  85.   const processedDatas = tableDatas.map((item) => {
  86.     const typeMap = {
  87.       1: "微信",
  88.       2: "支付宝",
  89.       3: "抖音",
  90.     };
  91.     return {
  92.       ...item,
  93.       type: typeMap[item.type] || "未知", // 转换 type 字段
  94.       terrace_order_num: item.terrace_order_num + "&nbsp;",
  95.     };
  96.   });
  97.   // 获取当前日期并格式化
  98.   const currentDate = new Date();
  99.   const year = currentDate.getFullYear();
  100.   const month = String(currentDate.getMonth() + 1).padStart(2, "0"); // 月份从0开始,需要加1
  101.   const day = String(currentDate.getDate()).padStart(2, "0");
  102.   const formattedDate = `${year}-${month}-${day}`;
  103.   // 将日期添加到文件名中
  104.   const fileName = `订单列表导出_${formattedDate}`;
  105.   // 导出处理后的数据
  106.   table2excel(column, processedDatas, fileName);
  107. };
  108. </script>
复制代码
全部导出:我还获取了当前的日期,导出时是名字+导出的日期。
并且:当列表中的某一行内容只有数字时,导出的表格会默认转成缩写格式。
可以把数据处理一下然后给后面加上一个HTML的空格,也就是&nbsb,这样就不会给数字默认转化为科学计数法。

选中导出:
  1. // 声明选中的行
  2. const selectedRows = ref([]);
  3. // 处理选中项变化
  4. const handleSelectionChange = (selection) => {
  5.   selectedRows.value = selection;
  6.   console.log("当前选中的数据:", selectedRows.value); // 打印选中的数据
  7. };
  8. // 导出选中数据的函数
  9. const exportPick = () => {
  10.   console.log("导出选中的数据:", selectedRows.value);
  11.   if (selectedRows.value.length === 0) {
  12.     ElMessage.warning("请选择要导出的数据!");
  13.     return;
  14.   }
  15.   // 获取导出数据
  16.   const column = [
  17.     {
  18.       title: "序号",
  19.       key: "id",
  20.       type: "text",
  21.     },
  22.     {
  23.       title: "店长名称",
  24.       key: "name",
  25.       type: "text",
  26.     },
  27.     {
  28.       title: "商家门头照",
  29.       key: "img",
  30.       type: "image",
  31.       width: 150,
  32.       height: 100,
  33.     },
  34.     {
  35.       title: "公司名称",
  36.       key: "title",
  37.       type: "text",
  38.     },
  39.     {
  40.       title: "订单号",
  41.       key: "order_num",
  42.       type: "text",
  43.     },
  44.     {
  45.       title: "价格",
  46.       key: "money",
  47.       type: "text",
  48.     },
  49.     {
  50.       title: "类型",
  51.       key: "type",
  52.       type: "text",
  53.       formatter: (row) => {
  54.         const typeMap = {
  55.           1: "微信",
  56.           2: "支付宝",
  57.           3: "抖音",
  58.         };
  59.         return typeMap[row.type] || "未知";
  60.       },
  61.     },
  62.     {
  63.       title: "平台订单号",
  64.       key: "terrace_order_num",
  65.       type: "text",
  66.     },
  67.     {
  68.       title: "创建时间",
  69.       key: "create_time",
  70.       type: "text",
  71.     },
  72.     {
  73.       title: "下单时间",
  74.       key: "pay_time",
  75.       type: "text",
  76.     },
  77.   ];
  78.   const tableDatas = JSON.parse(JSON.stringify(selectedRows.value)); //这里面填写你接口的数据\
  79.   // 处理数据,将 type 字段的值转换为对应的文字
  80.   const processedDatas = tableDatas.map((item) => {
  81.     const typeMap = {
  82.       1: "微信",
  83.       2: "支付宝",
  84.       3: "抖音",
  85.     };
  86.     return {
  87.       ...item,
  88.       type: typeMap[item.type] || "未知", // 转换 type 字段
  89.     };
  90.   });
  91.   // 获取当前日期并格式化
  92.   const currentDate = new Date();
  93.   const year = currentDate.getFullYear();
  94.   const month = String(currentDate.getMonth() + 1).padStart(2, "0"); // 月份从0开始,需要加1
  95.   const day = String(currentDate.getDate()).padStart(2, "0");
  96.   const formattedDate = `${year}-${month}-${day}`;
  97.   // 将日期添加到文件名中
  98.   const fileName = `订单列表选中导出_${formattedDate}`;
  99.   table2excel(column, processedDatas, fileName);
  100. };
  101. // 打开导出确认对话框
  102. const open = () => {
  103.   ElMessageBox.confirm("请选择要导出的数据", "导出", {
  104.     confirmButtonText: "导出全部",
  105.     cancelButtonText: "导出选中",
  106.     center: true,
  107.   })
  108.     .then(() => {
  109.       // 点击“导出全部”按钮
  110.       exportBtn();
  111.     })
  112.     .catch(() => {
  113.       // 点击“导出选中”按钮
  114.       exportPick();
  115.     });
  116. };
复制代码
 


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

梦应逍遥

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表