Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active July 5, 2022 14:11
Show Gist options
  • Save aspose-com-gists/f549878ac2e005b7f99eb49082db94d0 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/f549878ac2e005b7f99eb49082db94d0 to your computer and use it in GitHub Desktop.
Merge or Unmerge Excel Cells in Worksheet in C# .NET
// Load a workbook
Workbook workbook = new Workbook(dataDir + "Merge_Range.xlsx");
// Access the first worksheet
Worksheet worksheet = workbook.Worksheets[0];
// Specify a range
Range range = worksheet.Cells.CreateRange("A1:D4");
range.Name = "Named_Range";
// Get the range.
Range range1 = workbook.Worksheets.GetRangeByName("Named_Range");
// Define a style object.
Style style = workbook.CreateStyle();
// Set the alignment.
style.HorizontalAlignment = TextAlignmentType.Center;
// Create a StyleFlag object.
StyleFlag flag = new StyleFlag();
// Make the relative style attribute ON.
flag.HorizontalAlignment = true;
// Apply the style to the range.
range1.ApplyStyle(style, flag);
// Input data into range.
range1[0, 0].PutValue("Aspose");
// Merge range
range.Merge();
// Save the workbook
workbook.Save(dataDir + "Merge_NamedRange.xlsx");
// Create a workbook
Workbook workbook = new Workbook();
// Access the first worksheet
Worksheet worksheet = workbook.Worksheets[0];
// Input data into C6 Cell.
worksheet.Cells[0, 0].PutValue("Merge Range");
// Create a range
Range range = worksheet.Cells.CreateRange("A1:D4");
// Merge range into a single cell
range.Merge();
// Save the workbook
workbook.Save(dataDir + "Merge_Range.xlsx");
// Create a Workbook.
Workbook wbk = new Workbook();
// Create a Worksheet and get the first sheet.
Worksheet worksheet = wbk.Worksheets[0];
// Create a Cells object ot fetch all the cells.
Cells cells = worksheet.Cells;
// Merge some Cells (C6:E7) into a single C6 Cell.
cells.Merge(5, 2, 2, 3);
// Input data into C6 Cell.
worksheet.Cells[5, 2].PutValue("This is my value");
// Create a Style object to fetch the Style of C6 Cell.
Style style = worksheet.Cells[5, 2].GetStyle();
// Create a Font object
Font font = style.Font;
// Set the name.
font.Name = "Times New Roman";
// Set the font size.
font.Size = 18;
// Set the font color
font.Color = System.Drawing.Color.Blue;
// Bold the text
font.IsBold = true;
// Make it italic
font.IsItalic = true;
// Set the backgrond color of C6 Cell to Red
style.ForegroundColor = System.Drawing.Color.Red;
style.Pattern = BackgroundType.Solid;
// Apply the Style to C6 Cell.
cells[5, 2].SetStyle(style);
// Save the Workbook.
wbk.Save(dataDir + "MergeCells.xlsx");
// Create a workbook
Workbook workbook = new Workbook(dataDir + "Merge_Range.xlsx");
// Access the first worksheet
Worksheet worksheet = workbook.Worksheets[0];
// Create a range
Range range = worksheet.Cells.CreateRange("A1:D4");
// UnMerge range
range.UnMerge();
// Save the workbook
workbook.Save(dataDir + "UnmergeRange.xlsx");
// Open the excel file.
Workbook wbk = new Workbook(dataDir + "MergeCells.xlsx");
// Create a Worksheet and get the first sheet.
Worksheet worksheet = wbk.Worksheets[0];
// Create a Cells object ot fetch all the cells.
Cells cells = worksheet.Cells;
// Unmerge the cells.
cells.UnMerge(5, 2, 2, 3);
// Save the file.
wbk.Save(dataDir + "UnmergeCells.xlsx");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment