C# 单元格合并

 

ContractedBlock.gifExpandedBlockStart.gifCode
    /// <summary> 
/// 合并单元格,并赋值,对指定WorkSheet操作 
/// </summary> 
/// <param name="sheetIndex">WorkSheet索引</param> 
/// <param name="beginRowIndex">开始行索引</param> 
/// <param name="beginColumnIndex">开始列索引</param> 
/// <param name="endRowIndex">结束行索引</param> 
/// <param name="endColumnIndex">结束列索引</param> 
/// <param name="text">合并后Range的值</param> 
public void MergeCells(int sheetIndex,int beginRowIndex,int beginColumnIndex,int endRowIndex,int endColumnIndex,string text) 

if(sheetIndex > this.WorkSheetCount) 

this.KillExcelProcess(); 
throw new Exception("索引超出范围,WorkSheet索引不能大于WorkSheet数量!"); 


workSheet 
= (Excel.Worksheet)workBook.Worksheets.get_Item(sheetIndex); 
range 
= workSheet.get_Range(workSheet.Cells[beginRowIndex,beginColumnIndex],workSheet.Cells[endRowIndex,endColumnIndex]); 

range.ClearContents(); 
//先把Range内容清除,合并才不会出错 
range.MergeCells = true
range.Value 
= text; 
range.HorizontalAlignment 
= Excel.XlHAlign.xlHAlignCenter; 
range.VerticalAlignment 
= Excel.XlVAlign.xlVAlignCenter; 

 

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

        public void MergeWorkSheet(int sheetIndex,int beginRowIndex,int beginColumnIndex,int rowCount,int columnCount)
ExpandedBlockStart.gifContractedBlock.gif        
{

            
//检查参数
            if ( columnCount < 1 || rowCount < 1)
                
return ;

            
for(int col=0;col<columnCount;col++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
int mark = 0;            //标记比较数据中第一条记录位置
                int mergeCount = 1;        //相同记录数,即要合并的行数
                string text = "";
                
                
for(int row=0;row<rowCount;row++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
string prvName = "";
                    
string nextName = "";

                    
//最后一行不用比较
                    if( row + 1 < rowCount)        
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        
for(int n=0;n<=col;n++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
{
                            range 
= (Excel.Range)workSheet.Cells[row + beginRowIndex,n + beginColumnIndex];
                            range 
= (Excel.Range)range.MergeArea.get_Item(1,1);
                            text 
= range.Text.ToString();
                            prvName 
= prvName + text;

                            range 
= (Excel.Range)workSheet.Cells[row + 1 + beginRowIndex,n + beginColumnIndex];
                            range 
= (Excel.Range)range.MergeArea.get_Item(1,1);
                            nextName 
= nextName + range.Text.ToString();

                        }

                            
                        
if(prvName == nextName)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
{
                            mergeCount
++;

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

                        }

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

                            
                    }
        
                }

            }

        }

 

转载于:https://www.cnblogs.com/Jerrybook/archive/2009/08/26/1554431.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值