关于vue实现导出excel表,以及导出的excel后的图片超过单元格的问题

实现导出带图标片的excel的方法,

首先:

复制代码
import table2excel from 'js-table2excel

// 导出表格  按钮点击后触发事件
const onBatchExport = () => {
  
    const column = [
    //数据表单
    {
      title: "ID", //表头名称title
      key: "id", //数据
      type: "text", //类型
    },
    {
      title: "景区ID",
      key: "scienceid",
      type: "text",
    },
    {
      title: "景区名称",
      key: "sciencename",
      type: "text",
    },
    {
      title: "二维码",
      key: "code",
      type: "image",
      width: 80,
      height: 80,
    },  
    {
      title: "二维码状态",
      key: "state",
      type: "text",
    },
    {
      title: "创建时间",
      key: "time",
      type: "text",
      width: 130,
      height: 80,
    },
  
  ];
 //将数据转化为字符串(list_data数据是接口数据,把名称换成自己的数据即可)
  let tableDatas = JSON.parse(JSON.stringify(datalists.value)); 
  let datas = tableDatas;
  table2excel(column, datas, "数据"); //表单数据名称
};

但是这种可能会出现在excel打开表格时,图片尺寸超过单元格的问题,这个时候就要去更改table2excel文件源码,在node_modules/js-table2excel/src/index.js中更改

在img标签外面加一个div然后设置div和img的宽高,

复制代码
function getImageHtml(val, options) {
			options = Object.assign({ width: 40, height: 60, scale: 0.64 }, options);
			const imgWidth = options.width * options.scale;
			const imgHeight = options.height * options.scale;
			return `<td style=" width:${options.width}px;height:${options.height}px; text-align: center; vertical-align: middle">
			<div style="display: flex;
    justify-content: center;
    align-items: center; width:${options.width}px;height:${options.height}px; text-align: center;  margin:auto auto; vertical-align: middle;" ><img width="${imgWidth}" height="${imgHeight}" src="${val}" /></div>
				</td>`;
		}

我又给div和img的宽高加了一个缩小,div和img的宽高等于单元格宽高乘以0.64。

这样的话图片就不会超过单元格了ψ(`∇´)ψ,但是会有一个问题如果在wps上打开的话图片又会显得很小T_T。希望路过的各位大佬一起探讨一下该怎么办。

相关推荐
拉里呱唧32 分钟前
一个像在使用PPT的在线 HTML 编辑器:HeyHTML
javascript·交互·html5
ayqy贾杰1 小时前
Cursor SDK发布!开发者可直接搬走其内核
前端·vue.js·面试
changshuaihua0011 小时前
扣子开发指南
javascript·人工智能
光影少年2 小时前
对typescript开发框架的理解?
前端·javascript·typescript
a1117762 小时前
“像风之翼“无人机巡检平台仪表盘
前端·javascript·开源·html·无人机
李白的天不白2 小时前
vue 数据格式问题
前端·vue.js·windows
小白蒋博客2 小时前
【ai开发段永平投资理财的知识图谱网站】第一天:搭 Vite + Vue 项目,跑通 Hello World
vue.js·人工智能·trae
We་ct7 小时前
LeetCode 5. 最长回文子串:DP + 中心扩展
前端·javascript·算法·leetcode·typescript
@yanyu66612 小时前
登录注册功能-明文
vue.js·springboot
cn_mengbei15 小时前
用React Native开发OpenHarmony应用:Reanimated共享元素过渡
javascript·react native·react.js