合并单元格

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