手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>程序设计>C/C++>列表

在excel中将连续多列相同数据项合并_c#应用

来源:互联网 作者:west263.com 时间:2008-02-23
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!

效果图如下:

代码如下:
/**//// <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)
{

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

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

for(int row=0;row<rowCount;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;
}

}
}
}
}


文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!