Using the concatenate formula to combine cells Clicking on cell C2 (or the row where the information you want to combine is) Typing =concatenate( Clicking on cell A2 and then adding a comma (,) Clicking on cell B2 and closing the formula with a closing bracket.
Merge across cells: To merge across cells in a single row, highlight the cells and use this keyboard shortcut: ALT H+M+C. Unmerge cells: If you need to unmerge a group of cells, you can highlight the merged cell and press the following keys at the same time: ALT H+M+U.
Using the Interop you get a range of cells and call the .Merge()
method on that range.
eWSheet.Range[eWSheet.Cells[1, 1], eWSheet.Cells[4, 1]].Merge();
oSheet.get_Range("A1", "AS1").Merge();
Excel.Application xl = new Excel.ApplicationClass();
Excel.Workbook wb = xl.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorkshe et);
Excel.Worksheet ws = (Excel.Worksheet)wb.ActiveSheet;
ws.Cells[1,1] = "Testing";
Excel.Range range = ws.get_Range(ws.Cells[1,1],ws.Cells[1,2]);
range.Merge(true);
range.Interior.ColorIndex =36;
xl.Visible =true;
Code Snippet
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Excel.Application excelApp = null;
private void button1_Click(object sender, EventArgs e)
{
excelApp.get_Range("A1:A360,B1:E1", Type.Missing).Merge(Type.Missing);
}
private void Form1_Load(object sender, EventArgs e)
{
excelApp = Marshal.GetActiveObject("Excel.Application") as Excel.Application ;
}
}
Thanks
This solves the issue in the appropriate way
// Merge a row
ws.Cell("B2").Value = "Merged Row(1) of Range (B2:D3)";
ws.Range("B2:D3").Row(1).Merge();
You can use Microsoft.Office.Interop.Excel:
worksheet.Range[worksheet.Cells[rowNum, columnNum], worksheet.Cells[rowNum, columnNum]].Merge();
You can also use NPOI:
var cellsTomerge = new NPOI.SS.Util.CellRangeAddress(firstrow, lastrow, firstcol, lastcol);
_sheet.AddMergedRegion(cellsTomerge);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With