合并单元格

java中excel表格相同列的单元格合并

 1 ///合并表格相同行的内容  
 2 ///tableId:表格ID(最好是tbody,避免把表尾给合并了)  
 3 ///startRow:起始行,没有标题就从0开始  
 4 ///endRow:终止行,此参数是递归时检查的范围,一开始时会自动赋值为最后一行  
 5 ///col:当前处理的列
 6 function mergeCell(tableId, startRow, endRow, col) {  
 7     var tb = document.getElementById(tableId);  
 8     if (col >= tb.rows[0].cells.length) {  
 9         return;  
10     }  
11     //当检查第0列时检查所有行  
12     if (col == 0) {  
13         endRow = tb.rows.length - 1;  
14     }  
15     for (var i = startRow; i < endRow; i++) {  
16         //subCol:已经合并了多少列  
17         var subCol = tb.rows[0].cells.length - tb.rows[startRow].cells.length;  
18         //程序是自左向右合并,所以下一行一直取第0列  
19         if (tb.rows[startRow].cells[col - subCol].innerHTML == tb.rows[i + 1].cells[0].innerHTML) {  
20             //如果相同则删除下一行的第0列单元格  
21             tb.rows[i + 1].removeChild(tb.rows[i + 1].cells[0]);  
22             //更新rowSpan属性  
23             tb.rows[startRow].cells[col - subCol].rowSpan = (tb.rows[startRow].cells[col - subCol].rowSpan | 0) + 1;  
24             //当循环到终止行前一行并且起始行和终止行不相同时递归(因为上面的代码已经检查了i+1行,所以此处只到endRow-1)  
25             if (i == endRow - 1 && startRow != endRow) {  
26                 mergeCell(tableId, startRow, endRow, col + 1);  
27             }  
28         } else {  
29             //起始行,终止行不变,检查下一列  
30             mergeCell(tableId, startRow, i, col + 1);  
31             //增加起始行  
32             startRow = i + 1;  
33         }  
34     }  
35 }  

 

转载于:https://www.cnblogs.com/cathyqq/p/5177104.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值