Bootstrap

vue中导出带有合并单元格的excel

安装blob和export2excel两个文件放到vendor文件夹下

<template>
  <div class="wrapper">
    <button @click='downloadOut'>下载</button>
  </div>
</template>

<script>
export default {
  components:{},
  props:{},
  data(){
    return {
      list:[{
		"id": "1",
		"cityId": "222401",
		"createUser": "延边分公司传输测试账号",
		"createTime": "2015-04-20 16:27:00",
		"updateTime": "2019-01-09 18:25:56",
		"cityName": "延吉市",
		"nodes": [{
			"targetName": "设备0",
			"cards": [{
				"targetName": "板卡1",
				"ports": [{
					"targetName": "端口1-1"
				}, {
					"targetName": "端口1-2"
				}, {
					"targetName": "端口1-3"
				}]
			}, {
				"targetName": "板卡2",
				"ports": [{
					"targetName": "端口2-1"
				}, {
					"targetName": "端口2-2"
				}, {
					"targetName": "端口2-3"
				}]
			}]
    }]
}],
list2:[{
  'name':'zjm',
  'list':[{
    type:'数学',
    score:[{
    'fenshu':'34'
  }]},{
    type:'语文',
    score:[{
    'fenshu':'30'
  }]}]
}]
    }
  },
  watch:{},
  computed:{},
  methods:{
    downloadOutline() {
                        require.ensure([], () => {
                            const { export_json_to_excel } = require('../vendor/Export2Excel')
                            const { export_get_title_time } = require('../vendor/Export2Excel')
                            const { format_json } = require('../vendor/Export2Excel')
                            const tHeader = ['姓名', '创建时间','设备','板卡','端口']
                            const filterVal = [ 'cityName',  'createTime','node','card','port']
                            const list = this.list
                            //1.拆分数据 存放在新建数据中 2.获取要合并的集合
                            var node_c = filterVal.indexOf("node")//node的列下标
                            var card_c = filterVal.indexOf("card")//card的列下标
                            var current_r = 1;//当前行从1开始(因为0是表头)
                            var current_s_r = 1//每条数据开始行
                            var node_s_r = 1;//node的开始行 从1开始
                            var card_s_r = 1;//card的开始行 从1开始
                            //要合并集合
                            var merges = [];
                            //     {
                            //     s: {//s为开始
                            //         c: 0,//开始列
                            //         r: 0//可以看成开始行,实际是取值范围
                            //     },
                            //     e: {//e结束
                            //         c: 0,//结束列
                            //         r: 0//结束行
                            //     }
                            // }
                            var nowList = [];//新建数据
                            for (let i = 0; i < list.length; i++) {
                                const element = list[i];
                                for (let j = 0; j < element.nodes.length; j++) {
                                    const node = element.nodes[j];
                                    for (let k = 0; k < node.cards.length; k++) {
                                        const card = node.cards[k];
                                        for (let l = 0; l < card.ports.length; l++) {
                                            const port = card.ports[l];
                                            current_r++//当前行 //最后是7
                                            //制造重复数据
                                            var obj = JSON.parse(JSON.stringify(element))
                                            obj.node = node.targetName
                                            obj.card = card.targetName
                                            obj.port = port.targetName
                                            nowList.push(obj)
                                        }
                                        if(card_s_r != (current_r-1)){//开始r != 结束行 则是合并
                                            var cardMerges = {
                                                s: {c: card_c,r: card_s_r},
                                                e: {c: card_c,r: current_r-1}
                                            };
                                            merges.push(cardMerges)
                                        }
                                        card_s_r = current_r
                                    }
                                    if(node_s_r != (current_r-1)){//开始r != 结束行 则是合并
                                        var nodeMerges = {
                                            s: {c: node_c,r: node_s_r},
                                            e: {c: node_c,r: current_r-1}
                                        };
                                        merges.push(nodeMerges)
                                    }
                                    node_s_r = current_r
                                }
                                //除node、card、port外其他列(下标1到10) 均合并 port总行数
                                //['归属城市'0, '归属专业'1, '抑制时间'2, '抑制原因'3,'规则状态'4,'启用状态'5,'审核意见'6,'审核人'7,'审核时间'8,
                                //'创建人'9,'创建时间'10,'node'11,'card'12,'port'13]
                                for (let c = 0; c < 2 ; c++) {
                                    if(current_s_r != (current_r-1)){//开始r != 结束行 则是合并
                                        var nodeMerges = {
                                            s: {c: c,r: current_s_r},
                                            e: {c: c,r: current_r-1}
                                        };
                                        merges.push(nodeMerges)
                                    }
                                }
                                current_s_r = current_r
                            }
                            const data = format_json(filterVal, nowList)
                            const currentdate = export_get_title_time()
                            export_json_to_excel(tHeader, data, '监控平台-规则-抑制派单-规则列表_'+'('+currentdate+')',merges)
                        })
            },
             downloadOut() {
console.log(111)
                        require.ensure([], () => {
                            const { export_json_to_excel } = require('../vendor/Export2Excel')
                            const { export_get_title_time } = require('../vendor/Export2Excel')
                            const { format_json } = require('../vendor/Export2Excel')
                            const tHeader = ['姓名',  '学科', '分数']
                            const filterVal = [ 'name',  'type',  'score']
                            const list2 = this.list2
                            //1.拆分数据 存放在新建数据中 2.获取要合并的集合
                            var node_c = filterVal.indexOf("list")//node的列下标
                            var card_c = filterVal.indexOf("type")//card的列下标
                            var current_r = 1;//当前行从1开始(因为0是表头)
                            var current_s_r = 1//每条数据开始行
                            var node_s_r = 1;//node的开始行 从1开始
                            var card_s_r=1
                            //要合并集合
                            var merges = [];
                            var nowList = [];//新建数据
                            for (let i = 0; i < list2.length; i++) {
                                const element = list2[i];
                                for (let j = 0; j < element.list.length; j++) {
                                    const node = element.list[j];
                                    for(let k = 0; k < node.score.length; k++){
                                      const score =node.score[k];
                                           current_r++//当前行
                                            //制造重复数据
                                            var obj = JSON.parse(JSON.stringify(element))
                                            obj.type = node.type
                                            obj.score=score.fenshu
                                            nowList.push(obj)
                                        if(card_s_r != (current_r-1)){//开始r != 结束行 则是合并
                                            var cardMerges = {
                                                s: {c: card_c,r: card_s_r},
                                                e: {c: card_c,r: current_r-1}
                                            };
                                            merges.push(cardMerges)
                                        }
                                        card_s_r = current_r
                                    if(node_s_r != (current_r-1)){//开始r != 结束行 则是合并
                                        var nodeMerges = {
                                            s: {c: node_c,r: node_s_r},
                                            e: {c: node_c,r: current_r-1}
                                        };
                                        merges.push(nodeMerges)
                                    }
                                    node_s_r = current_r
                                    }
                                          
                                }
                                //除node、card、port外其他列(下标1到10) 均合并 port总行数
                                //['归属城市'0, '归属专业'1, '抑制时间'2, '抑制原因'3,'规则状态'4,'启用状态'5,'审核意见'6,'审核人'7,'审核时间'8,
                                //'创建人'9,'创建时间'10,'node'11,'card'12,'port'13]
                                for (let c = 0; c < 1 ; c++) {
                                    if(current_s_r != (current_r-1)){//开始r != 结束行 则是合并
                                        var nodeMerges = {
                                            s: {c: c,r: current_s_r},
                                            e: {c: c,r: current_r-1}
                                        };
                                        merges.push(nodeMerges)
                                    }
                                }
                                current_s_r = current_r
                            }
                            console.log(nowList)
                            const data = format_json(filterVal, nowList)
                            const currentdate = export_get_title_time()
                            export_json_to_excel(tHeader, data, '监控平台-规则-抑制派单-规则列表_'+'('+currentdate+')',merges)
                        })
            }
  },
  created(){},
  mounted(){
    // this.downloadOutline()
  }
}
</script>


;