Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all

I did the below code which is coping the data from one excel to other for a given range

but i have to copy the data to another worksheet's specific place. like it should copy after 5th row of the sheet and the range should be B1 to D1

Please tell me how to do that.

What I have tried:

C#
Excel.Application excelApplication = new Excel.Application();

            srcPath = "C:\\Users\\sn314708\\Desktop\\Excel\\1.xlsx";
            Excel.Workbook srcworkBook = excelApplication.Workbooks.Open(srcPath);
            Excel.Worksheet srcworkSheet = (Microsoft.Office.Interop.Excel.Worksheet)srcworkBook.Sheets.get_Item(1);

            destPath = "C:\\Users\\sn314708\\Desktop\\Excel\\2.xlsx";
            Excel.Workbook destworkBook = excelApplication.Workbooks.Open(destPath, 0, false);
            Excel.Worksheet destworkSheet = (Microsoft.Office.Interop.Excel.Worksheet)destworkBook.Sheets.get_Item(1);

            Excel.Range from = srcworkSheet.Range["A1:C100"];
            Excel.Range to = destworkSheet.Range["A1:C100"];

           
            from.Copy(to);

            destworkBook.SaveAs("C:\\Users\\sn314708\\Desktop\\Excel\\2 " + DateTime.Now.ToString("MM_dd_yyyy") + ".xlsx");
Posted
Updated 17-Feb-16 2:28am
v2
Comments
Maciej Los 17-Feb-16 11:39am    
Black_Rose, please see my comment to the Richard's answer. I'd suggest to change "from" and "to" variable names to "fromrange" and "torange" respectively.

That should be as simple as changing the "to" range:
C#
Excel.Range to = destworkSheet.Range["B6:D105"];
 
Share this answer
 
Comments
Maciej Los 17-Feb-16 11:35am    
Richard, if you wanted to say that "to" is reserved word, i have to admit that i do not see it on the list of reserved words ;(
But "from" is one of the reserved words ;)
https://msdn.microsoft.com/en-us/library/x53a06bb.aspx
Richard Deeming 17-Feb-16 11:37am    
No. Not sure where you've got that idea from?

All I'm saying is that to change the location where the data is pasted, you need to change the destination range stored in the to variable.
Maciej Los 17-Feb-16 11:45am    
Richard, i'm not an inquirer ;)
You're wrong. "from" & "to" refers to different ranges, because they are in the different worksheets in the different workbooks.
Richard Deeming 17-Feb-16 11:48am    
I'm well aware of that!

The OP's question is: how do I paste the values from one worksheet into another worksheet at a different address.
My answer is: change the destination address for the paste operation.

Not sure where this confusion is coming from?
Maciej Los 17-Feb-16 11:50am    
Mea culpa, i missed that!
A 5!
BTW: Have you seen my answer?
Please, read my comment to the question. A from variable name is wrong. This is a reserved word. Please, see: C# Keywords[^]

I'd suggest to change the name of variable to fromrange - for example.

A "to" range doesn't need to be defined the same way as "fromrange" (doesn't have to be same size). It is sufficient to define it as a sigle cell:
C#
Excel.Range torange = destworkSheet.Range["B1"];
 
Share this answer
 
v3
Comments
Richard Deeming 17-Feb-16 11:52am    
Not quite: "from" is a contextual keyword. It's only a reserved word in particular contexts, such as a LINQ query. There's nothing wrong with using it as a variable name outside of LINQ.
Maciej Los 17-Feb-16 12:02pm    
If contextual keyword is used outside of Linq, agree.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900