C# Excel - save each worksheet to a new workbook

前端 未结 3 1736
南方客
南方客 2020-12-18 06:30

I have an excel with 25 or so worksheets I just want to save each worksheet as it\'s own new Workbook. When I run the code it copys the entire workbook just not the individ

3条回答
  •  猫巷女王i
    2020-12-18 07:18

    ... You need to create a new workbook in your loop and move the sheet to that workbook. I program in VB, so i'm guessing, but the code inside your foor loop should look something like this:

    Microsoft.Office.Interop.Excel.Workbook workBook2 = app.Workbooks.Add(Missing.Value)
    Microsoft.Office.Interop.Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)workBook.Sheets[i+1];
    workSheet.Copy(workBook2.Sheets(1))
    

    Then you can add code to delete the other sheets, etc.

    Hope tihs helps.

提交回复
热议问题