1. 程式人生 > >適用於iview的表格轉Excel插件

適用於iview的表格轉Excel插件

agen obj form base sch -- avi window new

在網上找的一個表格轉excel插件,經過修改後使其適用於iview中的table組件

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‘; } }; const method = (ref) => { //整個表格拷貝到EXCEL中
if (getExplorer() == ‘ie‘) { let curTbl = ref; 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); } } else { tableToExcel(ref) } }; const Cleanup = () => { window.clearInterval(idTmr); }; const tableToExcel = (function () { 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><!--[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 || ‘Worksheet‘, table }; window.location.href = uri + base64(format(template, ctx)) } })(); export default (theadData, tbodyData, name = ‘Worksheet‘) => { // 寫入key過濾不顯示的td let thArr = []; // 建立節點 let table = document.createElement(‘table‘); let thead = document.createElement(‘thead‘); let tbody = document.createElement(‘tbody‘); // 建立thead中的tr let thTr = document.createElement(‘tr‘); // 遍歷寫入th表頭 for (let i of theadData) { thArr.push(i.key); let th = document.createElement(‘th‘); let text = document.createTextNode(i.title); th.appendChild(text); thTr.appendChild(th); } thead.appendChild(thTr); // 遍歷寫入td for (let i of tbodyData) { let tr = document.createElement(‘tr‘); for (let j in i) { // 篩選寫入 for (let k of thArr) { if (k == j) { let td = document.createElement(‘td‘); let text = document.createTextNode(i[j]); td.appendChild(text); tr.appendChild(td); } } } tbody.appendChild(tr); } table.appendChild(thead); table.appendChild(tbody); method(table.innerHTML, name); }

適用於iview的表格轉Excel插件