1

大家好,我有一个 excel 文件,我想将其用作新 excel 文件的源。我看到了一些可以复制所有工作表的示例,但是我的问题是我只想从源文件中复制选定的工作表并创建新文件选定的工作表。例如,如果我的主表有工作表 x、y、z、c、f、g 并且用户选择 z、f、gi 只会选择这三个创建一个新的 .xlsx 文件并将它们放在那里。这可能与C#?先感谢您

4

2 回答 2

1

例如,您可以在 VBA 中复制选定的工作表,下面将使用选定的工作表创建一个新工作簿

Sheets(Array("Sheet1", "Sheet3", "Sheet4")).Copy

在 C# 中,你可以做同样的事情

    private void button1_Click(object sender, EventArgs e)
    {
        Excel.Application xlexcel;
        Excel.Workbook xlWorkBook;
        Excel.Sheets worksheets;

        object misValue = System.Reflection.Missing.Value;

        xlexcel = new Excel.Application();

        xlexcel.Visible = true;

        //~~> Open a File
        xlWorkBook = xlexcel.Workbooks.Open("C:\\Sample.xlsx", 0, true, 5, "", "", true,
        Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

        //~~> Specify the sheets you want to copy
        String[] selectedSheets = { "a", "c", "d" };

        //~~> Set your worksheets
        worksheets = xlWorkBook.Worksheets;

        //~~>Copy it. This will create a new Excel file with selected sheets
        ((Excel.Sheets)worksheets.get_Item(selectedSheets)).Copy();

    }

截屏

在此处输入图像描述

于 2013-01-22T08:20:47.407 回答
-2

是的,可以在命令提示符下使用复制命令将文件从许多 Excel 工作表复制到单个工作表命令是 c:/path of the file copy sourcefile destinationfile

谢谢

于 2013-01-22T08:00:58.520 回答