效果图如下:

代码如下:
/**////
/// 合并工作表中指定行数和列数数据相同的单元格
///
/// 工作表索引
/// 开始行索引
/// 开始列索引
/// 要合并的行数
/// 要合并的列数
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();
