Excel 分頁另存新檔功能

將Excel的分頁另存成獨立excel檔

using Excel = Microsoft.Office.Interop.Excel;
using System.Threading;
using System.Runtime.InteropServices;

Excel.Application App2 = new Excel.Application();
            Excel.Workbook WB1 = App2.Workbooks.Open("Excel路徑");
            Excel.Worksheet WS1 = new Excel.Worksheet();

            try
            {
                WS1 = WB1.Worksheets[0]; //inedx或分頁名稱
                WS1.Copy();
                App2.ActiveWorkbook.SaveAs(WB1.Path + "\\" + WS1.Name + ".xlsx");
                App2.ActiveWorkbook.Close();
            }
            catch (Exception ex)
            {
                WS1 = null;
            }

            finally
            {
                if (WS1 != null)
                    Marshal.FinalReleaseComObject(WS1);
                if (WB1 != null)
                {
                    WB1.Close(false);
                    Marshal.FinalReleaseComObject(WB1);
                }
                if (App2 != null)
                {
                    App2.Quit();
                    Marshal.FinalReleaseComObject(App2);
                }
                GC.Collect();
            }