美文网首页
UNI本地导出excel

UNI本地导出excel

作者: 扶不起的蝌蚪 | 来源:发表于2020-04-16 14:50 被阅读0次

引入xlsx.js

import XLSX from '@/common/js/xlsx.full.min.js';
//单元格标题
arr[0] = [
                'guid',
                '任务名',
                '账号',
                '姓名',
                '任务下发时间',
                '任务类型',
                '调查位置',
                '经度',
                '纬度',
                '遥感解译得到的地质灾害类型',
                '遥感图像基本信息',
                '遥感影像特征',
                '遥感解译结果',
                '现场描述',
                '任务时间',
                '任务下发人',
                '调查时间',
                '是否调查',
                '是否为隐患点',
                '隐患类型',
                '图层名称',
                '图层地址',
                '行政区划代码'
            ];
//arr[1]对应arr[0]单元格内容
//导出功能
leadOut(arr){
            //采用数组导出方法,其他导出方法参考官方文档
            var ws = XLSX.utils.aoa_to_sheet(arr);
            var csvws = XLSX.utils.sheet_to_csv(ws);
            plus.io.requestFileSystem(
                plus.io.PRIVATE_DOC,
                fs => {
                    fs.root.getFile(
                        `${arr[1][1]}_${this.$moment().format('YYYY-MM-DD HH:mm:ss')}.csv`,
                        {
                            create: true
                        },
                        fileEntry => {
                            fileEntry.createWriter(
                                writer => {
                                    writer.onwrite = e => {
                                        uni.showModal({
                                            title: '提示',
                                            content: `导出成功,文件路径为${e.target.fileName}`,
                                            showCancel: false
                                        });
                                          uni.openDocument({
                                                    filePath: `file://${e.target.fileName}`,  //这里必须加file://否则会报错打不开文件
                                                    success: function(res) {
                                                        console.log(res);
                                                    },
                                                    fail(res) {
                                                        console.log(res);
                                                    }
                                                });
                                    };
                                    writer.write(csvws);
                                },
                                function(e) {
                                    uni.showToast({
                                        title: '导出文件失败,请检查你的权限',
                                        icon: 'none'
                                    });
                                }
                            );
                        }
                    );
                },
                e => {
                    uni.showToast({
                        title: '导出文件失败,请检查你的权限',
                        icon: 'none'
                    });
                }
            );
        }

相关文章

网友评论

      本文标题:UNI本地导出excel

      本文链接:https://www.haomeiwen.com/subject/rmbrvhtx.html