useful-exportDEMO.vue 5.17 KB
Newer Older
xinzhedeai's avatar
xinzhedeai committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
<template>
    <view class="content">
        <view class="top_box">{{title}}</view>
 
        <view class="btn_cube" @click="tableToExcel">导出一个表来看</view>
 
        <view class="tip">tips:合并什么的可以直接用table标签相关的行内属性合并,如colspan、rowspan</view>
        <view class="tip">tips:创建目录时,一个大目录,下面再有一级年月的目录,方便到时候读取目录</view>
        <view class="tip">{{successTip}}</view>
 
    </view>
</template>
 
<script>
	// 导出文件到手机 fileData:要写入到文件的数据,返回参数为文档路径
	function exportFile (fileData,documentName="项目Excel文件") {
	    /*
	    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.successTip = `文件位置:${pathStr}/${documentName}/${year}${month}月`;
	                            },500)
	 
	                        };
	                        // 写入内容;
	                        writer.write(fileData);
	 
	                    }, function(e) {
	                        console.log(e.message);
	                    });
	                });
	            })
	 
	        });
	 
	    });
	 
	}
	
    import {formatNumber,formatDateThis,getUnixTime} from "./dateUtil.js"
    var that;
    export default {
        components:{
 
        },
        data() {
            return {
                title:"app端导出excel",
                successTip:""
            }
        },
        onLoad() {
            that = this;
        },
        methods: {
            tableToExcel() {
                //要导出的json数据
                const jsonData = [{
                        name: '测试数据',
                        phone: '123456',
                        email: '123@456.com'
                    }
                ]
                //列标题
                let worksheet = "sheet1";
                let str = '<tr><td>姓名</td><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>`;
                //下载模板
                exportFile(template);
            }
 
        }
    }
 
   
 </script>