C# Excel - save each worksheet to a new workbook

2020-02-11 07:55发布

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 individual sheet. Any help would be awesome.

string FileDropLocation = @"C:\ExcelFiles";
        string file_FullFileName = @"C:\ts\Conversion\v2.xlsx";
        Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
        Microsoft.Office.Interop.Excel.Workbook workBook = app.Workbooks.Open(file_FullFileName, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
for (int i = 0; i < workBook.Worksheets.Count; i++)
        {
            Microsoft.Office.Interop.Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)workBook.Sheets[i+1];
            workSheet.SaveAs(FileDropLocation + "\\" + workSheet.Name);
        }
        workBook.Close();

标签: c# excel
3条回答
该账号已被封号
2楼-- · 2020-02-11 08:30

I know this works.

Microsoft.Office.Interop.Excel.Application xl = null;
            Microsoft.Office.Interop.Excel._Workbook wb = null;
            xl = new Microsoft.Office.Interop.Excel.Application();
            xl.SheetsInNewWorkbook = 1;
            xl.Visible = true;
            wb = (Microsoft.Office.Interop.Excel._Workbook)(xl.Workbooks.Add(Type.Missing));

            wb.SaveAs(FileDropLocation + "\\" + workBook.Sheets[i + 1].Name, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel8, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            wb.Close(true, Type.Missing, Type.Missing);
            xl.Quit();
            Microsoft.Office.Interop.Excel.Workbook destWorkbook = null;
            Microsoft.Office.Interop.Excel.Worksheet workSheet = null;
            Microsoft.Office.Interop.Excel.Worksheet newWorksheet = null;

            destWorkbook = app.Workbooks.Open(FileDropLocation + "\\" + workBook.Sheets[i + 1].Name + ".xls", false, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            workSheet = (Microsoft.Office.Interop.Excel.Worksheet)workBook.Sheets[i + 1];
            newWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)destWorkbook.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            app.DisplayAlerts = false;
            workSheet.Copy(Type.Missing,newWorksheet);
            destWorkbook.Save();
查看更多
迷人小祖宗
3楼-- · 2020-02-11 08:30

... 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.

查看更多
走好不送
4楼-- · 2020-02-11 08:41

Try this in place of your current for loop and below

    foreach(Worksheet sheet in workBook.Worksheets)
    {
        var newbook = app.Workbooks.Add(1);
        sheet.Copy(newbook.Sheets[1]);

        newbook.SaveAs(FileDropLocation + "\\" + sheet.Name);
        newbook.Close();
    }

    workBook.Close();

Just to note, I believe Workbooks.Add() places in a default blank sheet (typically Sheet1), so if you want just the copied sheet you'll have to explicitly remove it.

查看更多
登录 后发表回答