简体   繁体   English

如何从excel复制特定的工作表名称并将其保存到新位置? 使用 C#

[英]How to copy specific sheet name from excel and save it to a new location? Using C#

I am using C# Windows Forms.我正在使用 C# Windows 窗体。

Goal:目标:

If I have multiple excel sheets.如果我有多个excel表。

For example, "Sheet1, Sheet2, Sheet3, TestSheet1, TestSheet2"例如, "Sheet1, Sheet2, Sheet3, TestSheet1, TestSheet2"

How can I grab specific sheet name eg ["Sheet2"] and save it as a new excel workbook?如何获取特定的工作表名称,例如["Sheet2"]并将其另存为新的 Excel 工作簿?

This is what I have so far:这是我到目前为止:

Button Click:按钮点击:

private void button1_Click(object sender, EventArgs e)
{
    Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
    Microsoft.Office.Interop.Excel.Workbook excelBook = excelApp.Workbooks.Open("C:\Users\LV98\Desktop\Test C#\excel_file.xlsx");

    excelBook.Worksheets.Copy("Sheet2");
}

Update:更新:

Here is where I got to.这是我要去的地方。

private void button1_Click(object sender, EventArgs e)
{
    Excel.Application excelApp;

    string fileTarget = "C:\\Users\\LV98\\Desktop\\Test C#\\template.xlsx";
    string fileTemplate = "C:\\Users\\LV98\\Desktop\\Test C#\\excel_file.xlsx";
    excelApp = new Excel.Application();
    Excel.Workbook wbTarget;
    Excel.Worksheet sh;

    //Create target workbook
    wbTarget = excelApp.Workbooks.Open(fileTemplate);

    //Fill target workbook
    //Open the template sheet
    sh = wbTarget.Worksheets["Sheet2"];
    sh.Copy(wbTarget.Worksheets[1]);


    //Save file
    wbTarget.SaveAs(fileTarget);
    wbTarget.Close(true);
    excelApp.Quit();

}

When I open the new excel file, it opens "Sheet2", just what I was after!当我打开新的 excel 文件时,它会打开“Sheet2”,这正是我所追求的! But only problem is, there is other sheets saved too.. I will be looking into renaming the new sheet - and delete the rest.但唯一的问题是,还保存了其他工作表......我将考虑重命名新工作表 - 并删除其余工作表。

just try this in your code it will be works只需在您的代码中尝试此操作即可

foreach (Excel.Worksheet sheet in wbTarget.Worksheets)
            {
                //Save Particular sheet as file
                if (sheet.Name == "Sheet2")
                {
                    var newbook = excelApp.Workbooks.Add(1);
                    sheet.Copy(newbook.Sheets[1]);

                    newbook.SaveAs(@"C:\Users\LV98\Desktop\Test C#\" + sheet.Name);
                    newbook.Close();
                }
            }

complete code of your button click event.按钮单击事件的完整代码。 I tested the below code and it's working fine我测试了下面的代码,它工作正常

protected void button1_Click(object sender, EventArgs e)
    {
        Excel.Application excelApp;

        string fileTemplate = "C:\\Users\\LV98\\Desktop\\Test C#\\template.xlsx";            
        excelApp = new Excel.Application();
        Excel.Workbook wbTarget;

        //Create target workbook
        wbTarget = excelApp.Workbooks.Open(fileTemplate);

        foreach (Excel.Worksheet sheet in wbTarget.Worksheets)
        {
            //Save Particular sheet as file
            if (sheet.Name == "Sheet2")
            {
                var newbook = excelApp.Workbooks.Add(1);
                sheet.Copy(newbook.Sheets[1]);

                newbook.SaveAs(@"C:\Users\LV98\Desktop\Test C#\" + sheet.Name);
                newbook.Close();
            }
        }
        wbTarget.Close(true);
        excelApp.AskToUpdateLinks = false;
        excelApp.Quit();
        GetExcelProcess(excelApp);
    }
    [DllImport("user32.dll")]
    static extern int GetWindowThreadProcessId(int hWnd, out int lpdwProcessId);
    Process GetExcelProcess(Excel.Application excelApp)
    {
        int id;
        GetWindowThreadProcessId(excelApp.Hwnd, out id);
        return Process.GetProcessById(id);
    }

After a bit of research I came across some useful links.经过一番研究,我发现了一些有用的链接。 I am pasting below a code snippet which I think will help you out:我在下面粘贴了一个我认为可以帮助你的代码片段:

IWorkbook sourceWorkbook = application.Workbooks.Open("Source.xlsx");
IWorkbook destinationWorkbook = application.Workbooks.Open("Destination.xlsx");

//Copy Excel worksheet from source workbook to the destination workbook
destinationWorkbook.Worksheets.AddCopy(sourceWorkbook.Worksheets[0]);

//Save the file
destinationWorkbook.Save();

I have found the above code in the following link: Copy Excel worksheet to another workbook我在以下链接中找到了上述代码: 将 Excel 工作表复制到另一个工作簿

Also it I believe that you will find it helpful to read the following answer: How to copy a single Excel worksheet from one workbook to another?此外,我相信您会发现阅读以下答案很有帮助:如何将单个 Excel 工作表从一个工作簿复制到另一个工作簿?

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

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