C# Excel合并单元格,并赋值 - 一米阳光的专栏 - 博客频道 - CSDN.NET

baacloud免费翻墙vpn注册使用

 

来源: 合并单元格,并赋值 – 一米阳光的专栏 – 博客频道 – CSDN.NET

/// 合并单元格,并赋值,对每个WorkSheet操作
///

///开始行索引 ///开始列索引 ///结束行索引 ///结束列索引 ///合并后Range的值 public void MergeCells(int beginRowIndex,int beginColumnIndex,int endRowIndex,int endColumnIndex,string text)
{
for(int i=1;i<=this.WorkSheetCount;i++)
{
workSheet = (Excel.Worksheet)workBook.Worksheets.get_Item(i);
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;
}
}

///

/// 合并单元格,并赋值,对指定WorkSheet操作
///

///WorkSheet索引 ///开始行索引 ///开始列索引 ///结束行索引 ///结束列索引 ///合并后Range的值 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;
}

赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏