简体   繁体   English

C#Excel - 将每个工作表保存到新工作簿

[英]C# Excel - save each worksheet to a new workbook

I have an excel with 25 or so worksheets I just want to save each worksheet as it's own new Workbook. 我有一个excel有25个左右的工作表我只想保存每个工作表,因为它是自己的新工作簿。 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();

Try this in place of your current for loop and below 尝试使用此代替当前的for循环及以下

    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. 请注意,我相信Workbooks.Add()放置在默认的空白页(通常是Sheet1)中,因此如果您只想复制的表单,则必须明确删除它。

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();

... 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: 我用VB编程,所以我猜,但你的foor循环中的代码应该是这样的:

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. 希望有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM