Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy cells in excel using C#

How to copy to specific row in target sheet?

I need to copy A1 to J10 from a sheet in one excel to location starting from A15 in second excel sheet. How can I achieve this in c#? In the below Copy method there seems to be no option to specify the location in target excel sheet.

ObjWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)ObjWorkBookTemp.Sheets[1];
ObjWorkSheet.Copy(Type.Missing, ObjWorkBookGeneral.Sheets[1]);
like image 397
Anand Avatar asked Jul 29 '11 17:07

Anand


People also ask

How do I automatically copy data from one cell to another in Excel?

You can use formula to copy and paste cell automatically. Please do as follows. 1. For copying and pasting cell in current sheet such as copy cell A1 to D5, you can just select the destination cell D5, then enter =A1 and press the Enter key to get the A1 value.


1 Answers

I think you are using the wrong method here... you want to use a Paste method not a copy method.

Try the Range.PasteSpecial method... should do the trick for you.

Something like this...

Excel.Range sourceRange = firstWorksheet.get_Range("A1", "J10");
Excel.Range destinationRange = secondWorksheet.get_Range("A15", "J25");

sourceRange.Copy(Type.Missing);
destinationRange.PasteSpecial(Microsoft.Office.Interop.Excel.XlPasteType.xlPasteFormulas, Microsoft.Office.Interop.Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false);
like image 103
c0deNinja Avatar answered Oct 17 '22 20:10

c0deNinja