UNPKG

@hzy1123581324/z-view-ui

Version:

z-view-ui是使用vue3开发的组件,开发中,有部分组件功能未实现,慎用

211 lines (188 loc) 6.51 kB
// 将对象数组保存为excel 表格 export function exportTable(name="test",list=[],headData="",bodyData=""){ if(list.length==0){ return uni.showToast({ title: "表格数据为空", icon: "none" }) } let str = list.map(m=>{ let str2 = Object.values(m).map(mp=>{ return `<td>${mp}</td>` }).join('') return `<tr>${str2}</tr>` }).join('') let str1 = Object.keys(list[0]).map(mp=>{ return `<td>${mp}</td>` }).join('') let str3 = `<tr>${headData||str1}</tr>${bodyData||str}` console.log(str3); let 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 encoding="UTF-8"><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet> <x:Name>sheet1</x:Name> <x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet> </x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--> </head><body><table>${str3}</table></body></html>`; // #ifdef H5 return // #endif plus.io.requestFileSystem(plus.io.PRIVATE_DOC, function(fs) { // 可通过fs进行文件操作 console.log("Request file system success!"); console.log(fs.name); fs.root.getFile( `${name}.xlsx`, { create: true, exclusive: false }, fileEntry => { console.log(1); fileEntry.createWriter( writer => { writer.onwrite = e => { // that.count = 100; // that.tap = `成功导出[${length}]条数据,文件路径为:${e.target.fileName}`; console.log(e.target.fileName); setTimeout(function() { // that.proshow = false; uni.openDocument({ filePath: `file://${e.target.fileName}`, //这里必须加file://否则会报错打不开文件 success: function(res) { console.log(res); }, fail(res) { console.log(res); } }); }, 2000); }; // `在这里插入代码片` writer.write(template); }, function(e) { uni.showToast({ title: '导出文件失败,请检查你的权限', icon: 'none' }); } ); }, e => { console.log(e); } ); }, function(e) { uni.showToast({ title: e.message, icon: "none", }) }); } /* var that=this // this.removeObjWithArr(this.shuju1,'source_id') // var for (var i = 0; i < that.shuju1.length; i++) { var newObject = {}; newObject.company = that.shuju1[i].company; newObject.mobile = that.shuju1[i].mobile; that.newArray2.push(newObject); } //console.log(this.newArray2) // console.log(this.shuju1) //要导出的json数据 // const jsonData = [{ // name: '测试数据', // phone: '123456', // email: '123@456.com' // }] const jsonData = this.newArray2 //列标题 let worksheet = "sheet1"; let str = '<tr><td>公司名称</td><td>电话</td></tr>'; //循环遍历,每行加入tr标签,每个单元格加td标签 for (let i = 0; i < jsonData.length; i++) { str += '<tr>'; for (let item in jsonData[i]) { //增加\t为了不让表格显示科学计数法或者其他格式 str += `<td>${ jsonData[i][item] + '\t'}</td>`; } str += '</tr>'; } //下载的表格模板数据 let 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 encoding="UTF-8"><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>${str}</table></body></html>`; //下载模板 this.exportFile(template); }, // exportFile(fileData, documentName = "项目Excel文件") { // var that=this //PRIVATE_DOC: 应用私有文档目录常量 //PUBLIC_DOCUMENTS: 程序公用文档目录常量 plus.io.requestFileSystem(plus.io.PUBLIC_DOCUMENTS, function(fs) { let rootObj = fs.root; let fullPath = rootObj.fullPath; // let reader = rootObj.createReader(); // console.log(reader); // reader.readEntries((res)=>{ // console.log(res); //这里拿到了该目录下所有直接文件和目录 // },(err)=>{console.log(err);}) console.log("开始导出数据********"); // 创建文件夹 rootObj.getDirectory(documentName, { create: true }, function(dirEntry) { //获取当前的年月继续创建文件夹 let date = new Date(); let year = date.getFullYear(); let month = date.getMonth() + 1; dirEntry.getDirectory(`${year}${month}月`, { create: true }, function(dirEntry2) { // 创建文件,防止重名 let fileName = "excel" + getUnixTime(formatDateThis(new Date())); console.log(fileName); dirEntry2.getFile(`${fileName}.xlsx`, { create: true }, function(fileEntry) { fileEntry.createWriter(function(writer) { writer.onwritestart = (e) => { uni.showLoading({ title: "正在导出", mask: true }) } // /storage/emulated/0指的就是系统路径 let pathStr = fullPath.replace("/storage/emulated/0", ""); writer.onwrite = (e) => { // 成功导出数据; uni.hideLoading(); setTimeout(() => { uni.showToast({ title: "成功导出", icon: "success" }) that.$refs.popup2.hide() that.duxuan = true that.successTip = `文件位置:${pathStr}/${documentName}/${year}${month}月`; }, 500) }; // 写入内容; writer.write(fileData); }, function(e) { console.log(e.message); }); }); }) }); }); }, */