Only the top-left cell keeps its value.
dotnet add package \ RecursiveAI.OneDocument.Spreadsheets
Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneDocument.Spreadsheets; using var book = Workbook.Create(); var sheet = book.Sheets[0]; sheet["A1"].SetString("Sales report"); sheet.Range("A1:C1").Merge(); book.SaveAs("report.xlsx"); Console.WriteLine("Wrote report.xlsx");
Wrote report.xlsxThe title spans A1 through C1 in the XLSX.
Unzip the example, then dotnet run.
The full program
The download puts “Sales report”, “Draft” and “November” in A1, B1 and C1, respectively. It merges those cells, saves the workbook, then reopens it and reads the three positions.
using RecursiveAI.OneDocument.Spreadsheets; using var book = Workbook.Create(); var sheet = book.Sheets[0]; sheet["A1"].SetString("Sales report"); sheet["B1"].SetString("Draft"); sheet["C1"].SetString("November"); sheet.Range("A1:C1").Merge(); book.SaveAs("report.xlsx"); using var saved = Workbook.Open("report.xlsx"); var values = saved.Sheets[0].Range("A1:C1").Values; Console.WriteLine($"A1: {values[0, 0]}"); Console.WriteLine($"B1: {values[0, 1] ?? "(empty)"}"); Console.WriteLine($"C1: {values[0, 2] ?? "(empty)"}");
dotnet run prints:
A1: Sales report B1: (empty) C1: (empty)
Merge throws InvalidOperationException for a single cell or a range that overlaps an existing merge.
Variations
Unmerge the range. Insert this after Merge and before SaveAs in the full program:
sheet.Range("A1:C1").Unmerge();A1, B1 and C1 become separate cells again. B1 and C1 stay empty; unmerging does not restore their previous values.
Related recipes
The short answer
Call sheet.Range("A1:C1").Merge() to make the first three cells of row 1 one merged region, then save the workbook as XLSX. Put the text you want to keep in A1. In an A1-style range, the two references are the opposite corners of a rectangle: A1:C1 is one row across three columns, while A1:C2 also spans a second row.
How do I merge a range of cells in Excel?
OneDocument’s Merge keeps the value at the top-left of the rectangle and clears values and formulas in the covered cells. Those cells keep their styles. If your title is in B1 and A1 is empty, merging A1:C1 will not move the title to A1. Write or move the title to A1 before merging. The download deliberately starts with a value in every cell so you can see the difference between the surviving title and the empty cells underneath the rest of the merged region.
Merging changes the sheet layout, not just the text. An XLSX stores the rectangle as a merged range as well as storing the contents of A1. When the workbook reopens, the range reads as one nonempty value and two empty positions; it does not hold copies of the title in the three cells.
How do I merge multiple cells and keep all text?
Not by calling Merge on a range that already has multiple values you want to keep. Decide whether you need one visible label or separate data. For one label, put the combined text in the top-left cell before merging. If the values still need to be read independently, leave those cells separate and merge a different, otherwise empty row for the title. This is useful when a heading should span a report but the column labels immediately below it should remain separate.
If a sheet already has a merged region and the new one would overlap it, unmerge the old rectangle first, using its exact range. That frees the cells for the new layout but does not recover any content cleared by the old merge. If you need that content again, keep a copy of the original workbook or keep the values outside the merged region before changing it.