ITPub博客

首页 > Linux操作系统 > Linux操作系统 > C#实现在Excel中将连续多列相同数据项合并

C#实现在Excel中将连续多列相同数据项合并

原创 Linux操作系统 作者:fangqm 时间:2011-03-09 17:19:27 0 删除 编辑

 

/**////


        /// 合并工作表中指定行数和列数数据相同的单元格
        ///

        /// 工作表索引
        /// 开始行索引
        /// 开始列索引
        /// 要合并的行数
        /// 要合并的列数
        public void MergeWorkSheet(int sheetIndex,int beginRowIndex,int beginColumnIndex,int rowCount,int columnCount)
        {

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

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

                    //最后一行不用比较
                    if( row + 1 < rowCount)       
                    {
                        for(int n=0;n<=col;n++)
                        {
                            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)
                        {
                            mergeCount++;

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

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/23109131/viewspace-688850/,如需转载,请注明出处,否则将追究法律责任。

下一篇: DevExpress WEB换肤
请登录后发表评论 登录
全部评论

注册时间:2010-03-24

  • 博文量
    298
  • 访问量
    729531