Click here to Skip to main content
15,896,153 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using excel interop with c# I want to merge all adjacent cells that contain a specific value using c# code.

How to merge all the cells that contains the value (Merged cells) using c# code.

What I have tried:

I am trying this,

String startRange = "C2";
String endRange = "C7";
String repetitiveValue = "Merged Cells";
Microsoft.Office.Interop.Excel.Range xlrange = (Microsoft.Office.Interop.Excel.Range)objexcelapp.Cells[startRange, endRange];
xlrange.Value2 = repetitiveValue;
xlrange.Merge(System.Reflection.Missing.Value);
Posted
Updated 27-May-19 0:25am

1 solution

You can use this package it can do every thing for excel , it's very easy for use.

NuGet Gallery | EPPlus 4.5.3.1[^]

e.g. if you want to export DataTable to Excel

using (ExcelPackage pck = new ExcelPackage(newFile))
{
  ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Accounts");
  ws.Cells["A1"].LoadFromDataTable(dataTable, true);
  pck.Save();
}
 
Share this answer
 
Comments
Member 14376506 27-May-19 6:32am    
I'll installed (EPPlus) this library and using below code:

using (var excel = new ExcelPackage(new System.IO.FileInfo(@"physical_address_of_your_xslx_file")))
{
var sheet1 = excel.Workbook.Worksheets["Sheet1"];
sheet1.Cells["C2:C7"].Merge = true;
sheet1.Cells["C2:C7"].Style.VerticalAlignment = OfficeOpenXml.Style.ExcelVerticalAlignment.Bottom;
sheet1.Cells["C2"].Value = "The value";

excel.Save();
}

But Its showing exceptional error: An unhandled exception of type 'System.NullReferenceException' occurred in ExportExcel.exe Additional information: Object reference not set to an instance of an object.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900