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

Examples of Programmatic Copy/Paste between two different sheets

I'm trying to cut a range say A1:B4 from spreadsheet1 and Paste this into Say L11:M14 on Spreadsheet2

Using similar code to

            Dim dRange = Spreadsheet1.Workbook.Worksheets(0).Range(1, 1, 4 ,2)
           Spreadsheet1.CopyPaste.Copy(dRange.ConvertExcelRangeToGridRange, False)
        
 
'//This doesnt give me the position to make the paste
            Spreadsheet2.CopyPaste.Paste(dRange, Syncfusion.Windows.Forms.Spreadsheet.PasteOptions.Paste)


Results in problems of throwing the exception on the paste.

{"Unable to cast object of type 'Syncfusion.XlsIO.Implementation.RangeImpl' to type 'Syncfusion.Windows.Forms.CellGrid.GridRangeInfoList'."}

3 Replies

AR Arulpriya Ramalingam Syncfusion Team February 7, 2020 01:22 PM UTC

Hi Spotty, 
 
Thank you for using Syncfusion products. 
 
In order to convert the IRange into GridRangeInfo, the ConvertExcelRangeToGridRange() method of the GridExcelHelper can be used. Please make use of below code to convert the range into GridRangeInfo. 
 
Example code 
 
rangeInfo = new GridRangeInfoList(); 
IRange range = spreadsheet.Workbook.Worksheets[0].Range[2, 2, 2, 7]; 
//To convert the IRange into GridRangeInfo. 
 rangeInfo.Add(GridExcelHelper.ConvertExcelRangeToGridRange(range)); 
//To convert the GridRangeInfo into IRange 
//string iRange = GridExcelHelper.ConvertGridRangeToExcelRange(rangeInfo, spreadsheet.ActiveGrid); 
spreadsheet.CopyPaste.Copy(rangeInfo[0], false); 
 
//To paste the copied content in another sheet. 
spreadsheet.CopyPaste.Paste(rangeInfo, Syncfusion.Windows.Forms.Spreadsheet.PasteOptions.Paste); 
 
Please get back to us, if you have any other queries. 
 
Regards, 
Arulpriya 



SP Spotty February 7, 2020 01:56 PM UTC

So how to specify the paste location.   In my example to location was at a different position.


AR Arulpriya Ramalingam Syncfusion Team February 10, 2020 08:55 AM UTC

Hi Spotty, 
 
Thank you for the update. 
 
As per the Spreadsheet current behavior, the paste operation will be done for the ranges in ActiveGrid. In order to paste values in other sheet programmatically, we would suggest that you to activate the specified sheet to paste the value. Please make use of below code. 
 
Example code 
 
GridRangeInfoList list = new GridRangeInfoList(); 
IRange range = spreadsheet.Workbook.Worksheets[1].Range[4, 5, 4, 11]; 
//To convert the IRange into GridRangeInfo. 
list.Add(GridExcelHelper.ConvertExcelRangeToGridRange(range)); 
string backUpSheet = spreadsheet.ActiveSheet.Name; 
//To change the active grid. 
spreadsheet.SetActiveSheet(range.Worksheet.Name); 
//To paste the copied content in another sheet. 
spreadsheet.CopyPaste.Paste(list, Syncfusion.Windows.Forms.Spreadsheet.PasteOptions.Paste); 
//Revert to the previous sheet, if needed. 
//spreadsheet.SetActiveSheet(backUpSheet); 
 
Please get back to us, if you have any other queries. 
 
Regards, 
Arulpriya 


Loader.
Live Chat Icon For mobile
Up arrow icon