首页 > 其他分享 >js前端excel导出带图片(亲测可用)

js前端excel导出带图片(亲测可用)

时间:2023-08-29 21:11:07浏览次数:33  
标签:resolve return image excel js let const data 亲测

1,js-table2excel npm包有问题,导出后一片空白

 

2,改写一下js-table2excel

/* eslint-disable */
let idTmr;
const getExplorer = () => {
  let explorer = window.navigator.userAgent;
  //ie
  if (explorer.indexOf("MSIE") >= 0) {
    return 'ie';
  }
  //firefox
 
  else if (explorer.indexOf("Firefox") >= 0) {
    return 'Firefox';
  }
  //Chrome
  else if (explorer.indexOf("Chrome") >= 0) {
    return 'Chrome';
  }
  //Opera
  else if (explorer.indexOf("Opera") >= 0) {
    return 'Opera';
  }
  //Safari
  else if (explorer.indexOf("Safari") >= 0) {
    return 'Safari';
  }
}
// 判断浏览器是否为IE
const exportToExcel = (data, name) => {
 
  // 判断是否为IE
  if (getExplorer() == 'ie') {
    tableToIE(data, name)
  } else {
    tableToNotIE(data, name)
  }
}
 
const Cleanup = () => {
  window.clearInterval(idTmr);
}
 
// ie浏览器下执行
const tableToIE = (data, name) => {
  let curTbl = data;
  let oXL = new ActiveXObject("Excel.Application");
 
  //创建AX对象excel
  let oWB = oXL.Workbooks.Add();
  //获取workbook对象
  let xlsheet = oWB.Worksheets(1);
  //激活当前sheet
  let sel = document.body.createTextRange();
  sel.moveToElementText(curTbl);
  //把表格中的内容移到TextRange中
  sel.select;
  //全选TextRange中内容
  sel.execCommand("Copy");
  //复制TextRange中内容
  xlsheet.Paste();
  //粘贴到活动的EXCEL中
 
  oXL.Visible = true;
  //设置excel可见属性
 
  try {
    let fname = oXL.Application.GetSaveAsFilename("Excel.xls", "Excel Spreadsheets (*.xls), *.xls");
  } catch (e) {
    print("Nested catch caught " + e);
  } finally {
    oWB.SaveAs(fname);
 
    oWB.Close(savechanges = false);
    //xls.visible = false;
    oXL.Quit();
    oXL = null;
    // 结束excel进程,退出完成
    window.setInterval("Cleanup();", 1);
    idTmr = window.setInterval("Cleanup();", 1);
  }
}
 
// 非ie浏览器下执行
const tableToNotIE = (function () {
  // 编码要用utf-8不然默认gbk会出现中文乱码
  const uri = 'data:application/vnd.ms-excel;base64,',
    template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><meta charset="UTF-8"><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>';
 
  const base64 = function (s) {
    return window.btoa(unescape(encodeURIComponent(s)));
  }
 
  const format = (s, c) => {
    return s.replace(/{(\w+)}/g,
      (m, p) => {
        return c[p];
      })
  }
 
  return (table, name) => {
    const ctx = {
      worksheet: name,
      table
    }
 
    const url = uri + base64(format(template, ctx));
 
    if (navigator.userAgent.indexOf("Firefox") > -1) {
      window.location.href = url
    } else {
      const aLink = document.createElement('a');
      aLink.href = url;
      aLink.download = name || '';
      let event;
      if (window.MouseEvent) {
        event = new MouseEvent('click');
      } else {
        event = document.createEvent('MouseEvents');
        event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
      }
      aLink.dispatchEvent(event);
    }
  }
})()
 
// 导出函数
const table2excel = async (column, data, excelName) => {
  const typeMap = {
    image: getImageHtml,
    text: getTextHtml
  }
 
  let thead = column.reduce((result, item) => {
    result += `<th>${item.title}</th>`
    return result
  }, '')
 
  thead = `<thead><tr>${thead}</tr></thead>`
  var tbody = ``
  for (var item of data) {
    var temp = ``
    for (var item1 of column) {
      temp += await typeMap[item1.type || 'text'](item[item1.key], item1)
    }
    tbody += `<tr>${temp}</tr>`
  }
 
  tbody = `<tbody>${tbody}</tbody>`
 
  const table = thead + tbody
  // console.log(table)
  // 导出表格
  exportToExcel(table, excelName)
 
  function getTextHtml (val) {
    return new Promise((resolve, reject) => {
      resolve(`<td style="text-align: center;vnd.ms-excel.numberformat:@">${val}</td>`)
    })
    // return `<td style="text-align: center;vnd.ms-excel.numberformat:@">${val}</td>`
  }
 
  function imgUrlToBase64 (url) {
    return new Promise((resolve, reject) => {
      if (!url) {
        resolve('')
      }
      if (/\.(png|jpe?g|gif|svg)(\?.*)?$/.test(url)) {
        // 图片地址
        const image = new Image()
        // 设置跨域问题
        image.setAttribute('crossOrigin', 'anonymous')
        // 图片地址
        image.src = url
        image.onload = () => {
          const canvas = document.createElement('canvas')
          const ctx = canvas.getContext('2d')
          canvas.width = image.width
          canvas.height = image.height
          ctx.drawImage(image, 0, 0, image.width, image.height)
          // 获取图片后缀
          const ext = url.substring(url.lastIndexOf('.') + 1).toLowerCase()
          // 转base64
          const dataUrl = canvas.toDataURL(`image/${ext}`)
          resolve(dataUrl || '')
        }
      } else {
        // 非图片地址
        resolve('非(png/jpe?g/gif/svg等)图片地址');
      }
    })
 
  }
  function getImageHtml (val, options) {
    return new Promise((resolve, reject) => {
      imgUrlToBase64(val).then(res => {
        options = Object.assign({ width: 40, height: 60 }, options)
        resolve(`<td style="width: ${options.width}px; height: ${options.height}px; text-align: center; vertical-align: middle"><img  src="${res}" width=${options.width} height=${options.height}></td>`)
      }).catch(e => {
        resolve(e)
      })
    })
  }
}
 
export default table2excel

  

3, 使用

handleExport() {
        if (this.tableData.length == 0) return;
        const loading = this.$loading({
            target: ".facility-query",
            lock: true,
            text: '导出数据准备中...',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          });
        const data = this.tableData.map((v: any) => {
            return {
                ...v,
                picaddress: "/mediadl/media/getdata//LP/01/0101/52958960532.jpg"
            }
           }  
        );
        const column = [
            {
              title: '名称', 
              key: 'note_',
              type: 'text'
            },
            {
              title: 'photo',
              key: 'picaddress',
              type: 'image',
              width: 100,
              height: 100
            }
          ];
          const tableData = JSON.parse(JSON.stringify(data));
        table2excel(column, tableData, "导出数据_" + moment().format("YYYY-MM-DD_HHmmss")).then(() => {
                loading.close();
        }).catch(() => {
            loading.close();
        });
    }

  

 

标签:resolve,return,image,excel,js,let,const,data,亲测
From: https://www.cnblogs.com/cuijinlin/p/17665861.html

相关文章

  • js 常用链接
    nodeguide:TheNode.jsEventLoop,Timers,andprocess.nextTick()vuexvue3vitevitest......
  • 【Azure App Service for Linux】NodeJS镜像应用启动失败,遇见 RangeError: Incorrect
    问题描述在AppServiceForLinux中,部署NodeJS应用,应用启动失败。报错信息为:2023-08-29T11:21:36.329731566ZRangeError:Incorrectlocaleinformationprovided2023-08-29T11:21:36.329776866ZatIntl.getCanonicalLocales(<anonymous>)2023-08-29T11:21:36.329783066ZatC......
  • jsonpath用法记录
    {"flag":1,"code":0,"msg":"成功","detail":[{"name":"重疾险","value":"1","children":[......
  • js事件移除
    1.AbortController()addEventListener()时,可以配置一个信号,用于命令式地中止/删除监听器。当相应的控制器调用.abort()时,监听器会被移除:const button = document.getElementById('button');const controller = new AbortController();const { signal } = contro......
  • 【Azure App Service for Linux】NodeJS镜像应用启动失败,遇见 RangeError: Incorrect
    问题描述在AppServiceForLinux中,部署NodeJS应用,应用启动失败。报错信息为:2023-08-29T11:21:36.329731566ZRangeError:Incorrectlocaleinformationprovided2023-08-29T11:21:36.329776866ZatIntl.getCanonicalLocales(<anonymous>)2023-08-29T11:21:36.3297830......
  • 【NestJS系列】连接数据库及优雅地处理响应
    前言Node作为一门后端语言,当然也可以连接数据库,为前端提供CURD接口我们以mysql为例,自行安装mysqlTypeORMTypeORM是一个ORM框架,它可以运行在NodeJS、Browser、Cordova、PhoneGap、Ionic、ReactNative、Expo和Electron平台上,可以与TypeScript和JavaScript一起使用。......
  • ajax跨域jsonp
    java端代码:/** *AJAX跨域检证用户状态 *@paramrequest *@paramresponse *@throwsIOException */ @RequestMapping("ajaxCheckCross.html") publicvoiddoAjaxCheckCross(HttpServletRequestrequest,HttpServletResponseresponse)throwsIOException......
  • nodejs一些学习笔记记录
    模块一个文件就是一个模块引入模块Node.js提供了exports和require两个对象,其中exports是模块公开的接口,require用于从外部获取一个模块的接口,即所获取模块的exports对象。varhello=require('./hello');模块编写的形式常规写法exports.world=function(){......
  • js 禁止复制打印
    /*NoPrint.jsV1.0CreatedbyPDFAntiCopy.com*/constnoPrint=true;constnoCopy=true;constnoScreenshot=true;constautoBlur=false;if(noCopy){document.body.oncopy=function(){returnfalse};document.body.oncontextmenu=function......
  • 关于Newtonsoft.Json的随笔
    在工作中一些陈旧项目,难免引用了一些很老版本,在一次升级中,项目引用的Newtonsoft.Json.dll突然少了一些method和class1,原来的:(咱也不懂为啥没有版本号嘞 2,升级后的 问题:1,原来使用的函数不存在,但是其实还在,换地方了而已,以下是修改:Newtonsoft.Json.JavaScriptConvert.Seria......