2020-05-07将json导出xls文件

        export function JSONToExcelConvertor(JSONData, FileName, title, filter, rowlength) {

            if (!JSONData) {

                return

            }

            // 转化json为object

            var arrData = typeof JSONData !== 'object' ? JSON.parse(JSONData) : JSONData

            var excel = '

'

            // 设置表头

            var row = '

'

            if (title) {

                // 使用标题项

                for (var i in title) {

                    row += "" + title[i] + ''

                }

            } else {

                // 不使用标题项

                for (var i in arrData[0]) {

                    row += "" + i + ''

                }

            }

            excel += row + '

'

                // 设置数据

            for (var i = 0; i < arrData.length; i++) {

                if (i === arrData.length - 1) {

                    var row = '

'

                } else {

                    var row = ''

                }

                for (var index in arrData[i]) {

                    // 判断是否有过滤行

                    if (filter) {

                        var value = ''

                        if (filter.indexOf(index) === -1) { // 过滤掉符合关键字的数据

                            for (var k = 0; k < (arrData.length / rowlength); k++) { // 循环到一个标题长度换一次行,否则数组会在一行

                                if (i === rowlength - 1) {

                                    for (var j = k * rowlength; j < ((k + 1) * rowlength); j++) {

                                        if (arrData[j].type === 'radio-group' || arrData[j].type === 'checkbox-group' || arrData[j].type === 'select') { // 如果为这三种格式,则他们的值储存在values中

                                            var groupLenght = arrData[j].values.length

                                            for (var q = 0; q < groupLenght; q++) {

                                                if (arrData[j].values[q].selected === true) { // 获取被选中的值

                                                    value = value + arrData[j].values[q].label

                                                }

                                            }

                                        } else {

                                            value = arrData[j].value == null ? '' : arrData[j].value

                                        }

                                        row += `` + value + ''

                                        value = ''

                                    }

                                }

                                excel += row + '

'

                                row = ''

                            }

                        }

                    } else {

                        var value = arrData[i][index] == null ? '' : arrData[i][index]

                        row += "" + value + ''

                    }

                }

                excel += row + "";

            }

            excel += '

'

            var excelFile = ""

            excelFile += ''

            excelFile += '

            excelFile += '; charset=UTF-8">'

            excelFile += ''

            excelFile += ''

            excelFile += ''

            excelFile += ''

            excelFile += excel

            excelFile += ''

            excelFile += ''

            var uri = 'data:application/vnd.ms-excel;charset=utf-8,' + encodeURIComponent(excelFile)

            var link = document.createElement('a')

            link.href = uri

            link.style = 'visibility:hidden'

            link.download = FileName + '.xls'

            document.body.appendChild(link)

            link.click()

            document.body.removeChild(link)

        }

你可能感兴趣的:(2020-05-07将json导出xls文件)