近期需求:将一棵树导出到excel中,树是ztree,通过插件Table2excel导出table到excel中。
var list = treeObj.getNodes();
//将ztree导出到excel中
var list = treeObj.getNodes();
//递归循环list,(children)
var arr = [];
var maxRows = 0;
function getDeepData(children,level){
for(var i=0;i<children.length;i++){
var obj = children[i];
var name = obj.name;
var children2 = obj.children;
if(!children2 || children2.length == 0){//最下一层级
arr[maxRows] = arr[maxRows]||[];
arr[maxRows][level] = {name:name,rowspan:1};
maxRows ++;
}else{
arr[maxRows] = arr[maxRows]||[];
arr[maxRows][level] = {name:name};
var last = maxRows;
getDeepData(children2,level+1);
arr[last][level].rowspan = maxRows - last;
}
}
}
getDeepData(list,0);
var allHtml = '';
for(var i=0;i<arr.length;i++){
var row = arr[i];
var html = '<tr>';
for(var j=0;j<row.length;j++){
html += '<td rowspan="'+(null == row[j]||undefined == row[j] ? 1 :(row[j].rowspan||1))+'" style="'+(null == row[j]||undefined == row[j]? 'display:none;' : '')+'">'+(null == row[j]||undefined == row[j] ? '' : row[j].name)+'</td>';
}
html += '</tr>';
allHtml += html;
}
$('#table').append(allHtml);
new Table2Excel().export($('#table'),"文件名");
table2excel : https://github.com/rusty1s/table2excel
转载请注明出处: https://chrunlee.cn/article/export-ztree-to-excel.html