如何从Excel文件中选定的工作表复制到由程序创建一个新的excel文件 [英] How to copy selected sheets from excel file to a new excel file created by program

查看:160
本文介绍了如何从Excel文件中选定的工作表复制到由程序创建一个新的excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家对我喜有,我想作为源新的Excel files.I采用看到可以复制所有表的一些例子Excel文件,但我的问题是,我想唯一入选的张从源文件复制及建立新文件与所选择的片材。例如这将是一样,如果我的主表中有张X,Y,Z,C,F,G和用户选择Z,F,GI只会选择这三个创建一个新的.xlsx文件,并把它们there.Is这可能与C# ?谢谢你在前进

Hi everyone i have an excel file which i want to use as source for new excel files.I saw some examples that can copy all sheets,however my problem is that i want to copy only selected sheets from the source file and create new file with the selected sheets. e.g. this would be like if my main table has sheets x,y,z,c,f,g and user selects z,f,g i will only select these three create a new .xlsx file and put them there.Is this possible with c#? thank you in advance

推荐答案

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

You can copy the selected sheets for example in VBA, The below will create a new workbook with selected sheets

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

在C#中,你可以这么一回事

In C#, you can so the same thing as

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

    }

截图

这篇关于如何从Excel文件中选定的工作表复制到由程序创建一个新的excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆