前端vue方法导出为xsml

首先引用xlsx在这里插入图片描述
首先是导出方法

// 导出
        exportExcel() {
            // 格式化数据为二维数组
            this.exportData = this.formatTableData()
            // 设置表格数据
            const ws = XLSX.utils.aoa_to_sheet(this.exportData)
            // 新建一个工作簿
            const wb = XLSX.utils.book_new()
            // 设置表格样式,!cols为列宽
            const options = { '!cols': [] }
            for (const key in this.tableLabel) {
                options['!cols'].push({ wpx: this.tableLabel[key] })
            }
            ws['!cols'] = options['!cols']
            // 将工作表添加到工作簿
            XLSX.utils.book_append_sheet(wb, ws, 'Sheet1')
            // 输出工作表,由文件名决定的输出格式
            const fileNamePrefix = this.unitTestCoverObj.applicationName
            const time = getFullTime(null, true).replace(/-/g, '')
            const fileName = `${fileNamePrefix}${time}.xlsx`
            XLSX.writeFile(wb, fileName)
            this.$message({
                type: 'success',
                message: '导出成功'
            })
        }

格式化数据为二维数组

// 数据格式为二维数组
        formatTableData() {
            const data = []
            let tableRef = null
            tableRef = this.$refs.unitTestCoverTable
            data.push(...this.unitTestCoverData)
            if (!tableRef) {
                console.error('找不到表格组件ref')
                return []
            }
            const array = []
            array[0] = []
            this.tableLabel = {}
            for (const obj of tableRef.columns) {
                if (obj.type === 'index') {
                    array[0].push('序号')
                    this.tableLabel['index'] = obj.width
                } else {
                    array[0].push(obj.label)
                    this.tableLabel[obj.property] = obj.width || obj.minWidth || obj.realWidth || 100
                }
            }
            data.forEach((item, index) => {
                const subArray = []
                for (const head of Object.keys(this.tableLabel)) {
                    if (head === 'index') {
                        subArray.push(index + 1)
                        continue
                    }
                    if (item[head] !== undefined) {
                        if (head === 'coverage' || head === 'branchCoverage') {
                            subArray.push(`${item[head]}%`)
                        } else {
                            subArray.push(`${item[head]}`)
                        }
                    } else {
                        subArray.push('')
                    }
                }
                array.push(subArray)
            })
            console.log(array)
            return array
        }

console到控制台看一眼是这样的:
第一组对象为xsml表格第一排
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值