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

Free Support Forum - aspose.com

Why is my value being quadruplicated when I apply a value to a Range?

Using this code:

Range unitNameRange = locationWorksheet.Cells.CreateRange("A1", "D1");
unitNameRange.PutValue(_unit, false, true); // val, isConverted, SetStyle.
CellsFactory cf = new CellsFactory();
Style style = cf.CreateStyle();
style.HorizontalAlignment = TextAlignmentType.Left;
style.Font.Name = fontForSheets;
style.Font.IsBold = true;
style.Font.Size = 14;
unitNameRange.SetStyle(style);

...I am trying to get a value to span across four columns (A, B, C, D). Instead, the value is placed into each column, as can be seen in the attached screenshot.

If I do this:

Range unitNameRange = locationWorksheet.Cells.CreateRange("A1");
. . . // rest of the code the same as above

...the value only appears once, but it would cause column A's width to grow wider than I want it to be (and would differ each time, depending on the width of the value assigned).

How can I spread the value across an array of columns, appearing only once, and allowing the width of column A to be determined irrespective of the value assigned on the first row?

For more context, here is all the pertinent code (I think) for what is shown above:

Cell rptNameAndRangeCell = locationWorksheet.Cells["A2"];
string rptNameAndRangeCellVal = String.Format("Fill Rate Reporting - {0} to {1}", _begDate.ToShortDateString(), _endDate.ToShortDateString());
rptNameAndRangeCell.PutValue(rptNameAndRangeCellVal);
style = cf.CreateStyle();
style.HorizontalAlignment = TextAlignmentType.Left;
style.Font.Name = fontForSheets;
style.Font.IsBold = true;
style.Font.Size = 12;
rptNameAndRangeCell.SetStyle(style);
var ms = new MemoryStream();
Image _logo = RoboReporterConstsAndUtils.GetURLImage("http://www.packerswinsuperbowl2017.com/wp-content/themes/greenbay/images/pa_logo_notag.png");
_logo.Save(ms, ImageFormat.Png);
ms.Position = 0;
locationWorksheet.Pictures.Add(0, 4, ms);
AutoFitterOptions options = new AutoFitterOptions { OnlyAuto = true };
locationWorksheet.AutoFitRows(options);
Cell shortNameHeaderCell = locationWorksheet.Cells[BYDCBYLOC_HEADING_ROW, SHORTNAME_BYDCBYLOC_COL];
shortNameHeaderCell.PutValue("Short Name");
style = cf.CreateStyle();
style.HorizontalAlignment = TextAlignmentType.Left;
style.Font.Name = fontForSheets;
style.Font.IsBold = true;
style.Font.Size = 12;
shortNameHeaderCell.SetStyle(style);
Cell companyNameHeaderCell = locationWorksheet.Cells[BYDCBYLOC_HEADING_ROW, COMPANYNAME_BYDCBYLOC_COL];
companyNameHeaderCell.PutValue("Company Name");
companyNameHeaderCell.SetStyle(style);
Cell reasonDescHeaderCell = locationWorksheet.Cells[BYDCBYLOC_HEADING_ROW, REASONDESC_BYDCBYLOC_COL];
reasonDescHeaderCell.PutValue("Reason Description");
reasonDescHeaderCell.SetStyle(style);
Cell transTypeHeaderCell = locationWorksheet.Cells[BYDCBYLOC_HEADING_ROW, TRANSTYPE_BYDCBYLOC_COL];
transTypeHeaderCell.PutValue("Transaction Type");
transTypeHeaderCell.SetStyle(style);

Hi Clay,


Thank you for contacting Aspose support.

Please note, when you set a value to a range using the Range.PutValue method, it will be set for all the cells in the range. As per my understanding of this scenario, you probably wish to merge the cells in the range and set one value for the merged range. Could you please confirm this by sharing your desired results? Please create a sample spreadsheet manually in Excel and place the values as per your requirement. Upon reviewing it we will help you achieve the same goal with Aspose.Cells APIs.