自动合并单元格

一:使用jQuery合并table单元格

<script type="text/javascript" src="./js/jquery-1.7.2.js"></script>
<script type="text/javascript">
	jQuery.noConflict();
</script>
<style type="text/css">
td{
	text-align: center;
}
table.gridtable {
	font-family: verdana,arial,sans-serif;
	font-size:11px;
	color:#333333;
	border-width: 1px;
	border-color: #666666;
	border-collapse: collapse;
}
table.gridtable th {
	border-width: 1px;
	padding: 8px;
	border-style: solid;
	border-color: #666666;
	background-color: #dedede;
}
table.gridtable td {
	border-width: 1px;
	padding: 8px;
	border-style: solid;
	border-color: #666666;
	background-color: #ffffff;
}
</style>
</style>
</head>
<body>
	<table class="gridtable">
		<thead>
			<th>第一列</th>
			<th>第二列</th>
			<th>第三列</th>
			<th>第四列</th>
		</thead>
		<tbody class="sub">
			<tr>
				<td>a</td>
				<td>b</td>
				<td>e</td>
				<td>k</td>
			</tr>
			<tr>
				<td>a</td>
				<td>b</td>
				<td>e</td>
				<td>k</td>
			</tr>
			<tr>
				<td>a</td>
				<td>b</td>
				<td>e</td>
				<td>k</td>
			</tr>
			<tr>
				<td>a</td>
				<td>c</td>
				<td>e</td>
				<td>k</td>
			</tr>
			<tr>
				<td>a</td>
				<td>c</td>
				<td>f</td>
				<td>k</td>
			</tr>
			<tr>
				<td>a</td>
				<td>d</td>
				<td>g</td>
				<td>k</td>
			</tr>
			<tr>
				<td>a</td>
				<td>d</td>
				<td>h</td>
				<td>k</td>
			</tr>
		</tbody>
	</table>
	<div>
		<input type="button" value="合并单元格" οnclick="automerge()"/>
	</div>
	<script type="text/javascript">
	function automerge(){
		var size = jQuery("tbody.sub tr").length;
		merge(1,0,size);
	}
	function merge(col,first,size){
		if(col>3) return;//合并前三列
		var tdArr ;
		if(first == 0)
			tdArr = jQuery("tbody.sub tr:lt("+size+") td:nth-child("+col+")");
		else
			tdArr = jQuery("tbody.sub tr:gt("+first+"):lt("+size+") td:nth-child("+col+")");
		var start = 0;
		var str = jQuery(tdArr[0]).text();
		for(var i = 1 ; i < tdArr.length ; i++){
				if(jQuery(tdArr[i]).text() != str){
					jQuery(tdArr[start]).attr("rowspan",i-start);
					if(i-start > 1){
						if(start == 0)
							merge(col+1,first+start,i-start);
						else
							merge(col+1,first+start-1,i-start);
					}
					start = i;
					str = jQuery(tdArr[i]).text();
				}else if(i == size-1){
					jQuery(tdArr[start]).attr("rowspan",i-start+1);
					jQuery(tdArr[i]).css("display","none");
					if(i-start+1 > 1){
						if(start == 0)
							merge(col+1,first+start,i-start+1);
						else
							merge(col+1,first+start-1,i-start+1);
					}
				}else
					jQuery(tdArr[i]).css("display","none");
			}
		}
	</script>
</body>

效果图:

变为


大笑
二:自动合并excel单元格,需要用到jxl.jar包

public static void main(String[] args) {
		File file = new File("F:/my.xls");
		try {
			//修改excel文件
			Workbook workbook = Workbook.getWorkbook(file);
			WritableWorkbook wb = Workbook.createWorkbook(file, workbook);
			WritableSheet sheet = wb.getSheet(0);
			int rowCount = sheet.getRows();
			merge(sheet, 0, 1, rowCount-1);
			wb.write();
			wb.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**合并单元格*/
	private static void merge(WritableSheet sheet,int col,int startRow,int endRow) 
			throws RowsExceededException, WriteException{
		if(startRow >= endRow) return;
		if(col>2) return;//只合并前三列
		String tagStr = sheet.getCell(col, startRow).getContents();
		for(int i=startRow+1;i<=endRow;i++){
			String str = sheet.getCell(col, i).getContents();
			if(!tagStr.equals(str)){
				sheet.mergeCells(col, startRow, col, i-1);
				merge(sheet, col+1, startRow, i-1);
				startRow = i;
				tagStr = str;
			}else if(i==endRow){
				sheet.mergeCells(col, startRow, col, i);
				merge(sheet, col+1, startRow, i);
			}
		}
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值