C# 使用Microsoft.Office.Interop.Excel 操作Excel 调用Range.Merge()方法

2024-12-19 17:23:56
推荐回答(1个)
回答1:

之前系统做过的,把合并代码贴出来与大家分享
//假定要合并excel文件中第2行的1~3列,并且显示黑色边框
Microsoft.Office.Interop.Excel.Range rangesummary1 = excelSheet.get_Range(excelSheet.Cells[2, 1], excelSheet.Cells[2, 7]);
rangesummary1.Value2 = Type.Missing;
//必须的!由于excel本身的设计中考虑了与用户的交互,当此range中有值时,会弹出对话框。所以为了防止这种事情发生,应当清空值
rangesummary1.Merge(Type.Missing);
rangesummary1.Value2 = "单位名称:" + dt1.Rows[0]["Org_UpName"].ToString();
rangesummary1.Borders.Color = 0;
rangesummary1.Borders.Weight = 2;
rangesummary1.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
rangesummary1.VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;