locked
Interop Excel : Copy a sheet from a workbook and put it in another workbook RRS feed

  • Question

  • Hello,
     
    With Interop and C# .NET, I would like to copy a sheet from a workbook to put it in another workbook at the end.  
    I have already done a search on the internet, but the examples given are often partial or don't work. 
    Would you have a simple example in C# that can copy a sheet from a workbook to put it in another workbook at the end please?
     
    Thanks. 
    Monday, June 8, 2020 10:31 PM

All replies

  • Hi speed780,

    Thank you for posting here.

    Worksheet has a Copy method, you can use to achieve your goals.

                Application application = new Application();
                string surceFile = @"D:\test\excel\111.xlsx";
                string targetFile = @"D:\test\excel\222.xlsx";
                Workbook workbook = application.Workbooks.Open(surceFile);
                Workbook workbook1 = application.Workbooks.Open(targetFile);
                Worksheet worksheet = (Worksheet)workbook.Worksheets["Sheet1"];
              
                worksheet.Copy(workbook1.Worksheets[1]);
                
                workbook1.Save();
                workbook.Close();
                workbook1.Close();
                application.Quit();

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, June 9, 2020 2:42 AM
  • Hi, alternatively you can use Free Spire.XLS to coyp worksheet from one workbook to another this way.

    //Load source Excel file
    Workbook workbook1 = new Workbook();
    workbook1.LoadFromFile("Vendor.xlsx");
    //Get the specific worksheet
    Worksheet sheet1 = workbook1.Worksheets[0];
    //Load target Excel file
    Workbook workbook2 = new Workbook();
    workbook2.LoadFromFile("Report.xlsx");
    //Coyp worhsheet from worbook 1 to workbook 2
    Worksheet sheet2 = workbook2.Worksheets.AddCopy(sheet1);
    sheet2.Name = "Copy";
    //Save workbook 2
    workbook2.SaveToFile("Report.xlsx");


    Tuesday, June 9, 2020 7:33 AM
  • Hi,

    Has your issue been resolved?

    If so, please click "Mark as Answer" for the answer that solved your problem, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, June 16, 2020 7:30 AM