We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

rangy.CopyTo and formatting

I'm trying to get the same results as IWorksheets.AddCopy (in terms of copy "behavior") with the following code snippet but can't seem to get cell spacing to copy:

for (int i = 0; i < copyCells.Length; i++)
{

IRange range = copyCells[i];
int col =range.Column;
int row = range.Row;
int lastCol = range.LastColumn;
int lastRow = range.LastRow;
range.CopyTo(sheet.Range[row, col, lastRow, lastCol], ExcelCopyRangeOptions.All);

}

(just for the motivation - I want to use this approach to copy the same set of ranges over and over (moving down the appropriate number of rows) - to expand a template worksheet)



2 Replies

SH Steve Hookway April 21, 2009 02:59 PM UTC

adding the following lines seem to do the trick:

IRange newRange = sheet.Range[row, col, lastRow, lastCol];
range.CopyTo(newRange, ExcelCopyRangeOptions.All);
newRange.ColumnWidth = range.ColumnWidth;
newRange.RowHeight = range.RowHeight;

I thought ExcelCopyRangeOptions.All would also preserve the spacing. Are there any other values that I should explicitly add to my new range?



GM Geetha M Syncfusion Team April 22, 2009 12:22 PM UTC

Hi Steve,

Excel Range copy method does not copies row / column width / height. The above code copies all the data, formulas, styles and formatting (if any).

Please let me know if you have any questions.

Regards,
Geetha


Loader.
Live Chat Icon For mobile
Up arrow icon