8

I would like to simply copy one sheet within my workbook and give it a different name.

var pointName1 = workbook.Worksheets["PointName1"] as Worksheet;
pointName1.Copy(); // How do I access this newly created sheet?

Ideally I would like be able to write a method like this

pointName1.CopyTo("New Sheet");

where 'New Sheet' is a renamed copy of 'PointName1'.

Sometimes PointName1 will be the only sheet in the workbook, other times there will be others.

Joe Bauer
  • 572
  • 1
  • 9
  • 22
  • possible duplicate of [C# - How to copy a single Excel worksheet from one workbook to another?](http://stackoverflow.com/questions/3808368/c-sharp-how-to-copy-a-single-excel-worksheet-from-one-workbook-to-another) – MethodMan Nov 18 '14 at 21:58
  • @DJKRAZE I need to copy within the same workbook. The link you provided (along with most others on SO) deals with copying into a new workbook. – Joe Bauer Nov 18 '14 at 22:06

4 Answers4

12

You can achieve this in multiple ways - probably the easiest way is to copy after the last sheet and then rename it using the index:

Excel.Application xlApp = Marshal.GetActiveObject("Excel.Application") as Excel.Application;
Excel.Workbook xlWb = xlApp.ActiveWorkbook as Excel.Workbook;
Excel.Worksheet xlSht = xlWb.Sheets[1];
xlSht.Copy(Type.Missing, xlWb.Sheets[xlWb.Sheets.Count]); // copy
xlWb.Sheets[xlWb.Sheets.Count].Name = "NEW SHEET";        // rename

I believe this MSDN guide also answers your question.

If you want to get the index of a sheet, look up Worksheet.Index property.

3

You should be able to use the Copy function, but you won't be able to rename the sheet in the same step. The MSDN documentation shows the additional parameters:

pointName1.Copy(pointName1, Type.Missing); //Place a copy before the existing sheet

From the documentation: If you do not specify either Before or After, Microsoft Office Excel creates a new workbook that contains the copied sheet.

To rename the sheet you'll need to get a reference to the new sheet (by index or name) and use the Name property of worksheet to change the name.

EDIT:

If you use the code above you can use the index of the original sheet (since you're placing the copy before the original):

int index = pointName1.Index;
pointName1.Copy(pointName1, Type.Missing);
Worksheet newWS = (Worksheet)xlApp.Worksheets[index];
MikeH
  • 4,242
  • 1
  • 17
  • 32
  • I would like to copy into the same sheet, not create a new workbook. How can I reliably get a reference to the newly created sheet in the same workbook? – Joe Bauer Nov 19 '14 at 14:13
  • @JoeBauer This code copies it into the same notebook. The line from the documentation is there to show you why the. As to the index, if you use the code above the index will be the same as the original sheet since that code inserts before. – MikeH Nov 19 '14 at 16:32
2

well, other solution mentioned here did not work for me. I got this solution.

Step 1: Create a copy of the source file (i.e. TempFile )

Step 2: Copy desired sheet of source file to TempFile

Step 3: Delete the source file

Step 4: Rename TempFile to Source File.

Note: You will need the "Microsoft.Office.Interop.Excel" package from Nuget for this solution. Also, add using Excel = Microsoft.Office.Interop.Excel;

static void Main(string[] args)
{
    Excel.Application excelApp;

    string sourceFileName = "Original.xlsx"; //Source excel file
    string tempFileName = "temp.xlsx";

    string folderPath = @"C:\FodlerPath\";

    string sourceFilePath = System.IO.Path.Combine(folderPath, sourceFileName);
    string destinationFilePath = System.IO.Path.Combine(folderPath, tempFileName);

    System.IO.File.Copy(sourceFilePath,destinationFilePath,true);

    /************************************************************************************/

    excelApp = new Excel.Application();
    Excel.Workbook wbSource, wbTarget;
    Excel.Worksheet currentSheet;

    wbSource = excelApp.Workbooks.Open(sourceFilePath);
    wbTarget = excelApp.Workbooks.Open(destinationFilePath); 

    currentSheet = wbSource.Worksheets["Sheet1"]; //Sheet which you want to copy
    currentSheet.Name = "TempSheet"; //Give a name to destination sheet

    currentSheet.Copy(wbTarget.Worksheets[1]); //Actual copy
    wbSource.Close(false);
    wbTarget.Close(true);
    excelApp.Quit();

    System.IO.File.Delete(sourceFilePath);
    System.IO.File.Move(destinationFilePath, sourceFilePath);
}
Amnesh Goel
  • 2,617
  • 3
  • 28
  • 47
0

The easiest way is to copy it after the last sheet as presented in the accepted answer.

Please note that if the excel file contains hidden sheets, the copy will be place between the visible sheets and the hidden ones, therefore the hidden sheets will be pushed to the right.

If you then try to set the name with xlWb.Sheets[xlWb.Sheets.Count].Name = "NEW SHEET" you will end up renaming your last hidden sheet instead of your new copy.

I have managed to get round this by accessing the new copy by it's name: xlWb.Sheets["_oldName_ (2)"].Name = "NEW SHEET".

Other fix that will enable you to use xlWb.Sheets[xlWb.Sheets.Count].Name = "NEW SHEET" is to set all sheets visible before copying the desired sheet.

DG.
  • 74
  • 1
  • 6