Range.PutValue with Merged Cells

I have a merged cell that is A18:J18, it is also a named range "details"

I've tried using the following code to add data, but the values added only fill what would be A18 instead of the entire merged cell:

Range range1 = wb1.Worksheets.GetRangeByName("details");

range1[0,0].PutValue(myReader["details"].ToString());

I've tried swamping the [0,0] with range1.FirstRow and range1.FirstColumn but it errors out with "Value does not fall within expected range." Checking the debug, FirstRow and FirstColumn return the right values of 17 and 0 (respectively)

Hi,

Thanks for considering Aspose.

Well, When you merged some cells whether it is a named range of cells, a single cell is generated or formulated and the merged cells / merged named range reference will be the upper-left cell in the selected range of merged cells. So, if your merged cells named range is A18:J18, then you can only insert into A18 cell which is the upper-left most cell in the range. You cannot enter into any other cell in the range of merged cells. Although you can apply some style attributes to this single long cell. To insert into any other cell, you have to unmerge those merged cells first.

This is same in MS Excel as you may check for confirmation.

The following code create a named range first (A18:J18) and then merge this named range. Finally using some alignment style attibutes, I place the String "Hello" in the center (horizontally) of the named range:

Workbook wb1 = new Workbook();

Worksheet worksheet1 = wb1.Worksheets[0];

Range mrange = worksheet1.Cells.CreateRange("A18", "J18");

mrange.Name = "details";

mrange.Merge();

Range range1 = wb1.Worksheets.GetRangeByName("details");

int i = wb1.Styles.Add();

Style style = wb1.StylesIdea [I];

style.HorizontalAlignment = TextAlignmentType.Center;

range1.Style = style;

range1[0,0].PutValue("Hello");

wb1.Save("d:\\tstrange.xls");

Best Regards,

FirstRow and FirstColumn property return index in the worksheet. To put value on the first cell of a range, you can use:

range1[0, 0].PutValue(...);

or

range1.Worksheet.Cells[range1.FirstRow, range1.FirstColumn].PutValue(...);