We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Cell Value not visible if we write in AsposeRange last column

Hi,MergeCellsInNamedRange.zip (7.6 KB)

Cell Value not visible if we write in AsposeRange last column.

Attached sample code and generated file.

@pankajgrwl123,
I have tested the scenario and observed that as all the cells are merged, hence you can put value at index 0,0 only. If you want to save data in individual cell, then you may un-merge the cells in the range. Please give it a try and share the feedback.

@pankajgrwl123,

Moreover, you need to insert the value before merge operation. See the updated code segment that works fine.
e.g.
Sample code:

            // Instantiate a new Workbook.
            Workbook wb1 = new Workbook();

            // Get the first worksheet in the workbook.
            Worksheet worksheet1 = wb1.Worksheets[0];

            // Create a range.
            Range mrange = worksheet1.Cells.CreateRange("D6", "I12");

            // Name the range.
            mrange.Name = "TestRange";

            // Get the range.
            Range range1 = wb1.Worksheets.GetRangeByName("TestRange");

            // Input data into last column's first cell of your range.
            range1[0, 5].PutValue("Welcome to Aspose APIs.");

            // Merge the cells of the range.
            mrange.Merge();

            // Define a style object.
            Style style = wb1.CreateStyle();

            // Set the alignment.
            style.HorizontalAlignment = TextAlignmentType.Center;
            style.VerticalAlignment = TextAlignmentType.Center;
            style.Pattern = BackgroundType.Solid;
            style.ForegroundColor = System.Drawing.Color.Aqua;

            // Create a StyleFlag object.
            StyleFlag flag = new StyleFlag();
            // Make the relative style attribute ON.
            flag.HorizontalAlignment = true;
            flag.VerticalAlignment = true;
            flag.CellShading = true;

            // Apply the style to the range.
            range1.ApplyStyle(style, flag);

            // Save the excel file.
            wb1.Save("e:\\test2\\outputMergeCellsInNamedRange.xlsx");

            Console.WriteLine("MergeCellsInNamedRange executed successfully."); 

Hope, this helps a bit.

Hi @Amjad_Sahi

If i access the value it is in D6 not I6.

Also if i write to I6 and then merge (“D6”, “I12”) how can I make it work so that if I access it should be in I6

@pankajgrwl123,

Please note, in MS Excel when you merge a range of cells, the merged cell’s name will be top left cell. For example, when you merge “D6”, “I12”, it will become one big cell (D6). Now you got to insert into D6 cell if you want to insert some data into the merged area. This is the behavior of MS Excel regarding merging cell(s) feature and Aspose.Cells just follows the same specifications and standards of MS Excel.

To better understand this behavior, please try and practice the merging cells feature in MS Excel manually and you will notice Aspose.Cells works the same way as per MS Excel.

Should you have any further queries or issue, let us know with details.