5

我有一个包含 25 个左右工作表的 Excel,我只想将每个工作表保存为它自己的新工作簿。当我运行代码时,它会复制整个工作簿而不是单个工作表。任何帮助都是极好的。

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

3 回答 3

5

试试这个代替你当前的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();

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

于 2013-02-06T18:43:15.917 回答
1

我知道这行得通。

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();
于 2013-02-06T19:00:05.307 回答
0

...您需要在循环中创建一个新工作簿并将工作表移动到该工作簿。我在 VB 中编程,所以我猜,但你的 foo 循环内的代码应该是这样的:

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

然后您可以添加代码以删除其他工作表等。

希望对您有所帮助。

于 2013-02-06T18:30:46.843 回答