在Excel中将连续多列相同数据项合并

本文介绍了一种用于Excel工作表的方法,能够实现指定区域内相同数据的单元格自动合并,通过简单的参数配置即可完成合并操作,提高了数据整理的工作效率。
效果图如下:
merge.jpg

代码如下:
ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
InBlock.gif        
/// 合并工作表中指定行数和列数数据相同的单元格
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="sheetIndex">工作表索引</param>
InBlock.gif        
/// <param name="beginRowIndex">开始行索引</param>
InBlock.gif        
/// <param name="beginColumnIndex">开始列索引</param>
InBlock.gif        
/// <param name="rowCount">要合并的行数</param>
ExpandedBlockEnd.gif        
/// <param name="columnCount">要合并的列数</param>

None.gif        public void MergeWorkSheet(int sheetIndex,int beginRowIndex,int beginColumnIndex,int rowCount,int columnCount)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            
//检查参数
InBlock.gif
            if ( columnCount < 1 || rowCount < 1)
InBlock.gif                
return ;
InBlock.gif
InBlock.gif            
for(int col=0;col<columnCount;col++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
int mark = 0;            //标记比较数据中第一条记录位置
InBlock.gif
                int mergeCount = 1;        //相同记录数,即要合并的行数
InBlock.gif
                string text = "";
InBlock.gif                
InBlock.gif                
for(int row=0;row<rowCount;row++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
string prvName = "";
InBlock.gif                    
string nextName = "";
InBlock.gif
InBlock.gif                    
//最后一行不用比较
InBlock.gif
                    if( row + 1 < rowCount)        
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
for(int n=0;n<=col;n++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            range 
= (Excel.Range)workSheet.Cells[row + beginRowIndex,n + beginColumnIndex];
InBlock.gif                            range 
= (Excel.Range)range.MergeArea.get_Item(1,1);
InBlock.gif                            text 
= range.Text.ToString();
InBlock.gif                            prvName 
= prvName + text;
InBlock.gif
InBlock.gif                            range 
= (Excel.Range)workSheet.Cells[row + 1 + beginRowIndex,n + beginColumnIndex];
InBlock.gif                            range 
= (Excel.Range)range.MergeArea.get_Item(1,1);
InBlock.gif                            nextName 
= nextName + range.Text.ToString();
InBlock.gif
ExpandedSubBlockEnd.gif                        }

InBlock.gif                            
InBlock.gif                        
if(prvName == nextName)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            mergeCount
++;
InBlock.gif
InBlock.gif                            
if(row == rowCount - 2)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                
this.MergeCells(sheetIndex,beginRowIndex + mark,beginColumnIndex + col,beginRowIndex + mark + mergeCount - 1,beginColumnIndex + col,text);
ExpandedSubBlockEnd.gif                            }

ExpandedSubBlockEnd.gif                        }

InBlock.gif                        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
this.MergeCells(sheetIndex,beginRowIndex + mark,beginColumnIndex + col,beginRowIndex + mark + mergeCount - 1,beginColumnIndex + col,text);
InBlock.gif                            mergeCount 
= 1;
InBlock.gif                            mark 
= row + 1;
ExpandedSubBlockEnd.gif                        }

InBlock.gif                            
ExpandedSubBlockEnd.gif                    }
        
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值