导出图片和数据到Excel

简介: 导出图片和数据到Excel

/* 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会出现中文乱码
       let 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>',
         base64 = function (s) {
           return window.btoa(unescape(encodeURIComponent(s)))
         },
         format = (s, c) => {
           return s.replace(/{(\w+)}/g, (m, p) => {
             return c[p]
           })
         }
       return (table, name) => {
         let ctx = {
           worksheet: name,
           table
         }
         //创建下载
         let link = document.createElement('a')
         link.setAttribute('href', uri + base64(format(template, ctx)))
         link.setAttribute('download', name)
         // window.location.href = uri + base64(format(template, ctx))
         link.click()
       }
     })()
   // tHeader和tbody的数据需要一一对应
   let tHeader = ["车名", "颜色", "照片"];
   let tbody = [
     {
       name: "林肯",
       color: "红色",
       pic: "https://img2.baidu.com/it/u=4020335929,583313623&fm=253&fmt=auto&app=138&f=JPEG?w=512&h=384",
     },
     {
       name: "林肯",
       color: "黄色",
       pic: "https://img1.baidu.com/it/u=1453715177,1769663341&fm=253&fmt=auto&app=120&f=JPEG?w=750&h=500",
     },
     {
       name: "林肯",
       color: "紫色",
       pic: "https://img1.baidu.com/it/u=1453715177,1769663341&fm=253&fmt=auto&app=120&f=JPEG?w=750&h=500",
     },
     {
       name: "林肯",
       color: "白色",
       pic: "https://img2.baidu.com/it/u=647676637,3137806543&fm=253&fmt=auto&app=120&f=JPEG?w=889&h=500",
     },
     {
       name: "林肯",
       color: "粉色",
       pic: "https://img0.baidu.com/it/u=2950100227,2008423144&fm=253&fmt=auto&app=120&f=JPEG?w=830&h=467",
     },
   ];
   const export2Excel = (theadData, tbodyData, dataname) => {
     let re = /http/; // 字符串中包含http,则默认为图片地址
     let th_len = theadData.length; // 表头的长度
     let tb_len = tbodyData.length; // 记录条数
     let width = 120; // 设置图片大小
     let height = 80;
     // 添加表头信息
     let thead = "<thead><tr>";
     for (let i = 0; i < th_len; i++) {
       thead += "<th>" + theadData[i] + "</th>";
     }
     thead += "</tr></thead>";
     // 添加每一行数据
     let tbody = "<tbody>";
     for (let i = 0; i < tb_len; i++) {
       tbody += "<tr>";
       let row = tbodyData[i]; // 获取每一行数据
       for (let key in row) {
         if (re.test(row[key])) {
           // 如果为图片,则需要加div包住图片
           //
           tbody +=
             '<div style="width:' +
             width +
             "px; height:" +
             height +
             'px; text-align: center; vertical-align: middle"><div style="display:inline"><img src=\'' +
             row[key] +
             "' " +
             " " +
             "width=" +
             '"' +
             width +
             '"' +
             " " +
             "height=" +
             '"' +
             height +
             '"' +
             "></div></div>";
         } else {
           tbody += '<div style="text-align:center">' + row[key] + "</div>";
         }
       }
       tbody += "</tr>";
     }
     tbody += "</tbody>";
     let table = thead + tbody;
     // 导出表格
     exportToExcel(table, dataname);
   };
   //
   const app = document.querySelector("#app");
   console.log(app, "app");
   app.onclick = function () {
     export2Excel(tHeader, tbody, '测试数据')
   };
相关文章
|
2月前
|
关系型数据库 MySQL Shell
不通过navicat工具怎么把查询数据导出到excel表中
不通过navicat工具怎么把查询数据导出到excel表中
32 0
|
6天前
|
SQL C# 数据库
EPPlus库的安装和使用 C# 中 Excel的导入和导出
本文介绍了如何使用EPPlus库在C#中实现Excel的导入和导出功能。首先,通过NuGet包管理器安装EPPlus库,然后提供了将DataGridView数据导出到Excel的步骤和代码示例,包括将DataGridView转换为DataTable和使用EPPlus将DataTable导出为Excel文件。接着,介绍了如何将Excel数据导入到数据库中,包括读取Excel文件、解析数据、执行SQL插入操作。
EPPlus库的安装和使用 C# 中 Excel的导入和导出
|
15天前
|
存储 Java
java的Excel导出,数组与业务字典匹配并去掉最后一个逗号
java的Excel导出,数组与业务字典匹配并去掉最后一个逗号
34 2
|
24天前
|
数据采集 存储 数据挖掘
使用Python读取Excel数据
本文介绍了如何使用Python的`pandas`库读取和操作Excel文件。首先,需要安装`pandas`和`openpyxl`库。接着,通过`read_excel`函数读取Excel数据,并展示了读取特定工作表、查看数据以及计算平均值等操作。此外,还介绍了选择特定列、筛选数据和数据清洗等常用操作。`pandas`是一个强大且易用的工具,适用于日常数据处理工作。
|
2月前
|
SQL JSON 关系型数据库
n种方式教你用python读写excel等数据文件
n种方式教你用python读写excel等数据文件
|
2月前
|
存储 Java Apache
|
2月前
|
索引 Python
Python基于Excel多列长度不定的数据怎么绘制折线图?
本文档详述了如何运用Python从CSV格式的Excel文件中读取特定范围的数据,并基于这些数据绘制多条折线图。文件的第一列代表循环增长的时间序列,后续各列包含不同属性的数据。通过指定起始与结束行数,可选取一个完整的时间循环周期内的数据进行绘图。每列数据以不同颜色和线型表示,并且图片长度会根据时间序列的长度动态调整,确保图表清晰易读。最终生成的图表将保存至指定文件夹。
|
2月前
|
关系型数据库 MySQL Windows
MySQL数据导入:MySQL 导入 Excel 文件.md
MySQL数据导入:MySQL 导入 Excel 文件.md
|
4月前
|
数据安全/隐私保护
杨老师课堂之Excel VBA 程序开发第七讲表格数据高亮显示
杨老师课堂之Excel VBA 程序开发第七讲表格数据高亮显示
39 1
|
2月前
|
数据可视化 Python
我是如何把python获取到的数据写入Excel的?
我是如何把python获取到的数据写入Excel的?
39 2