p-export-excel
Version:
p-export-excel
8 lines (7 loc) • 9.84 kB
JavaScript
/*!
* p-export-excel v1.1.1
* Copyright 2024 Pbstar (https://github.com/pbstar)
* Licensed under MIT (https://github.com/pbstar/p-export-excel/blob/main/LICENSE)
* 2024-07-31 17:56:50
*/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).pExportExcel=t()}(this,(function(){"use strict";function e(e){var t="",n="",r=[],o="";return"xlsx"==e.fileType?(r=function(e){for(var t=[],n=0;n<e.sheets.length;n++){var r=e.sheets[n],o="";e.sheetStyle&&(o+=e.sheetStyle),r.style&&(o+=r.style);for(var l="<table style='border-spacing: 0;font-family:宋体;"+o+";vnd.ms-excel.numberformat:@'>",s=0;s<r.rows.length;s++){var i=r.rows[s],c="";e.rowStyle&&(c+=e.rowStyle),r.rowStyle&&(c+=r.rowStyle),i.style&&(c+=i.style),l+="<tr style='"+c+"'>";for(var a=0;a<i.cells.length;a++){var f=i.cells[a],d="";e.cellStyle&&(d+=e.cellStyle),r.cellStyle&&(d+=r.cellStyle),i.cellStyle&&(d+=i.cellStyle),f instanceof Object?(l+="<td",f.colspan&&(l+=" colspan="+f.colspan),f.rowspan&&(l+=" rowspan="+f.rowspan),f.style&&(d+=f.style),l+=" style='"+d+"'>"+f.text+"</td>"):l+="<td style='"+d+"'>"+f+"</td>"}l+="</tr>"}l+="</table>",t.push(l)}return t}(e),n=function(e,t){var n="<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'>",r='<x:ExcelWorksheet><x:Name>{SheetName}</x:Name><x:WorksheetSource HRef="sheet{SheetIndex}.htm"/><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>',o='<o:File HRef="sheet{SheetIndex}.htm"/>',l="\n------=_NextPart_dummy\nContent-Location: sheet{SheetIndex}.htm\nContent-Type: text/html; charset=windows-1252\n\n"+n+'\n<head>\n <meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">\n <link id="Main-File" rel="Main-File" href="../WorkBook.htm">\n <link rel="File-List" href="filelist.xml">\n</head>\n<body>{SheetContent}</body>\n</html>',s='MIME-Version: 1.0\nX-Document-Type: Workbook\nContent-Type: multipart/related; boundary="----=_NextPart_dummy"\n\n------=_NextPart_dummy\nContent-Location: WorkBook.htm\nContent-Type: text/html; charset=windows-1252\n\n'+n+'\n<head>\n<meta name="Excel Workbook Frameset">\n<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">\n<link rel="File-List" href="filelist.xml">\n\x3c!--[if gte mso 9]><xml>\n<x:ExcelWorkbook>\n <x:ExcelWorksheets>{ExcelWorksheets}</x:ExcelWorksheets>\n <x:ActiveSheet>0</x:ActiveSheet>\n</x:ExcelWorkbook>\n</xml><![endif]--\x3e\n</head>\n<frameset>\n <frame src="sheet0.htm" name="frSheet">\n <noframes><body><p>This page uses frames, but your browser does not support them.</p></body></noframes>\n</frameset>\n</html>\n{HTMLWorksheets}\nContent-Location: filelist.xml\nContent-Type: text/xml; charset="utf-8"\n\n<xml xmlns:o="urn:schemas-microsoft-com:office:office">\n <o:MainFile HRef="../WorkBook.htm"/>\n {ListWorksheets}\n <o:File HRef="filelist.xml"/>\n</xml>\n------=_NextPart_dummy--\n',i=function(e,t){return e.replace(/{(\w+)}/g,(function(e,n){return t[n]}))},c={ExcelWorksheets:"",HTMLWorksheets:"",ListWorksheets:""};return t.sheets.forEach((function(t,n){var s=t.sheetName||"Sheet"+n;c.ExcelWorksheets+=i(r,{SheetIndex:n,SheetName:s});var a=e[n];c.HTMLWorksheets+=i(l,{SheetIndex:n,SheetContent:a}),c.ListWorksheets+=i(o,{SheetIndex:n})})),i(s,c)}(r,e),t="data:application/vnd.ms-excel;charset=utf-8,"+encodeURIComponent(n)):"csv"==e.fileType&&(e.sheets.length>0&&(e.sheets.length>1&&console.warn("csv格式下多表导出仅会导出第一个表"),o=function(e){for(var t="",n=0;n<e.rows.length;n++){var r=e.rows[n];n>0&&(t+=" \n ");for(var o=0;o<r.cells.length;o++){var l=r.cells[o];o>0&&(t+=","),t+=l instanceof Object?l.text:l}}return t}(e.sheets[0])),t="data:text/csv;charset=utf-8,"+encodeURIComponent(o)),{base64:t,csv:o,xlsx:r}}function t(e,t){if("base64"==e.resType)return t;if("blob"==e.resType)return r=new Blob([t],{type:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});if("file"==e.resType)return new File([t],e.fileName+"."+e.fileType,{type:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});if("download"==e.resType){var n=document.createElement("a");return n.href=t,n.download=e.fileName+"."+e.fileType,n.click(),"success"}if("bloburl"==e.resType){var r=new Blob([t],{type:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});return URL.createObjectURL(r)}}function n(e,t){return new Promise((function(n,r){var o="";if("xlsx"==e.fileType){var l=t.xlsx;o='\n <div style="width: 100%;height: 470px;overflow: auto;">\n '.concat(l.map((function(e,t){return'<div class="pee_tab_content" style="display: '.concat(0===t?"block":"none",';">').concat(e,"</div>")})).join(""),'\n </div>\n <div style="width: 100%;height: 25px;display: flex;align-items: center;margin-top: 4px;box-sizing: border-box;border-left: 1px solid #ccc;border-bottom: 1px solid #ccc;">\n ').concat(e.sheets.map((function(e,t){return'<div class="pee_tab_bar" style="width:100px;height:25px;font-size:12px;cursor: pointer;border: 1px solid #ccc;border-bottom-color: '.concat(0===t?"#1890ff":"transparent",';border-left:0;box-sizing: border-box;display:flex;justify-content: center;align-items: center;">').concat(e.sheetName,"</div>")})).join(""),"\n </div>\n ")}else"csv"==e.fileType&&(o=t.csv).indexOf("\n")>=0&&(o=o.replace("\n","<br/>"));var s=document.querySelector("body"),i=document.createElement("div");i.style.width="100%",i.style.height="100%",i.style.position="fixed",i.style.top="0",i.style.left="0",i.style.backgroundColor="rgba(0,0,0,0.5)",i.style.zIndex="9999",i.style.display="flex",i.style.justifyContent="center",i.style.alignItems="center",i.innerHTML='\n <div style="width: 800px;height: 600px;background-color: white;border-radius: 3px;overflow: hidden;display: flex;flex-direction: column;align-items: center;justify-content: space-between;">\n <div style="width: 100%;height: 50px;padding:0 15px;display: flex;justify-content: space-between;align-items: center;box-sizing: border-box;">\n <div style="font-size: 18px;">预览</div>\n <div id="pee_close" style="cursor: pointer;">X</div>\n </div>\n <div style="width: 100%;height: 500px;overflow: auto;box-sizing: border-box;padding: 0 10px;">\n '.concat(o,'\n </div>\n <div style="width: 100%;height: 50px;display: flex;justify-content: center;align-items: center;">\n <button style="width: 100px;height: 30px;lin-height:28px;border: 1px solid #eee;background-color: #fff;color: #333;border-radius: 5px;cursor: pointer;" id="pee_close">取消</button>\n <button style="width: 100px;height: 30px;lin-height:30px;border: none;background-color: #1890ff;color: white;border-radius: 5px;cursor: pointer;margin-left: 15px;" id="pee_ok">确定</button>\n </div>\n </div>\n '),null==s||s.appendChild(i);var c=document.querySelector("#pee_close");null==c||c.addEventListener("click",(function(){f(!1)}));var a=document.querySelector("#pee_ok");function f(e){null==s||s.removeChild(i),n(e)}null==a||a.addEventListener("click",(function(){f(!0)}));var d=document.querySelectorAll(".pee_tab_bar");d&&d.forEach((function(e,t){e.addEventListener("click",(function(){var e;e=t,document.querySelectorAll(".pee_tab_content").forEach((function(t,n){t.style.display=n===e?"block":"none"})),document.querySelectorAll(".pee_tab_bar").forEach((function(t,n){t.style.borderBottomColor=n===e?"#1890ff":"#fff"}))}))}))}))}return function(r){return new Promise((function(o,l){var s=function(e){var t={fileName:"文件",fileType:"xlsx",sheets:[],sheetStyle:"",rowStyle:"",cellStyle:"",resType:"download",isPreview:!1};if(!e)return console.error("请传入配置参数"),!1;if(e.fileName){if("string"!=typeof e.fileName)return console.error("fileName参数类型错误,请传入字符串"),!1;if(e.fileName.length>50)return console.error("fileName参数长度不能超过50"),!1;if(!/^[a-zA-Z0-9\u4e00-\u9fa5_-]+$/.test(e.fileName))return console.error("fileName参数不合法,请传入合法的文件名"),!1;t.fileName=e.fileName}if(e.fileType){if("string"!=typeof e.fileType)return console.error("fileType参数类型错误,请传入字符串"),!1;if(!["xlsx","csv"].includes(e.fileType))return console.error("fileType参数不合法,请传入xlsx或csv"),!1;t.fileType=e.fileType}if(e.sheets){if(!Array.isArray(e.sheets))return console.error("sheets参数类型错误,请传入数组"),!1;t.sheets=e.sheets}if(e.sheetStyle){if("string"!=typeof e.sheetStyle)return console.error("sheetStyle参数类型错误,请传入字符串"),!1;t.sheetStyle=e.sheetStyle}if(e.rowStyle){if("string"!=typeof e.rowStyle)return console.error("rowStyle参数类型错误,请传入字符串"),!1;t.rowStyle=e.rowStyle}if(e.cellStyle){if("string"!=typeof e.cellStyle)return console.error("cellStyle参数类型错误,请传入字符串"),!1;t.cellStyle=e.cellStyle}if(e.resType){if("string"!=typeof e.resType)return console.error("resType参数类型错误,请传入字符串"),!1;if(!["download","blob","bloburl","file","base64"].includes(e.resType))return console.error("resType参数不合法,请传入download、blob、bloburl、file或base64"),!1;t.resType=e.resType}if(e.isPreview||!1===e.isPreview){if("boolean"!=typeof e.isPreview)return console.error("isPreview参数类型错误,请传入布尔值"),!1;t.isPreview=e.isPreview}return t}(r);if("boolean"==typeof s)return l("error:config error");var i=s,c=e(i);if(!c.base64)return l("error: create file error");if(i.isPreview)n(i,c).then((function(e){if(e){var n=t(i,c.base64);o(n)}else l("error: preview error")}));else{var a=t(i,c.base64);o(a)}}))}}));