C# - How to copy a single Excel worksheet from one workbook to another?

后端 未结 3 708
攒了一身酷
攒了一身酷 2020-11-29 12:06

I have a need to copy a worksheet from one workbook into another and I\'m a bit stuck. The premise is that I have a \"master\" workbook that stores the templates for a numb

3条回答
  •  甜味超标
    2020-11-29 12:53

    You could also use the Sheet.Copy() method.

    Basically, Sheet.copy copies the sheet, and will automatically create a new workbook at the same time.

    Try adding the following lines to your code, after your call to Worksheets.get_Item:

    //Copies sheet and puts the copy into a new workbook
    sheet.Copy(Type.Missing, Type.Missing);
    
    //sets the sheet variable to the copied sheet in the new workbook
    sheet = app.Workbooks[2].Sheets[1];
    

    Here's the reference for the Copy() function as well: MSDN Link

提交回复
热议问题