【问题标题】:How to copy specific sheet name from excel and save it to a new location? Using C#如何从excel复制特定的工作表名称并将其保存到新位置?使用 C#
【发布时间】:2020-03-12 12:01:04
【问题描述】:

我正在使用 C# Windows 窗体。

目标:

如果我有多个 Excel 表格。

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

如何获取特定的工作表名称,例如["Sheet2"] 并将其另存为新的 Excel 工作簿?

这是我目前所拥有的:

按钮点击:

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");
}

更新:

这就是我要去的地方。

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

}

当我打开新的 excel 文件时,它会打开“Sheet2”,这正是我所追求的!但唯一的问题是,还保存了其他工作表。我将考虑重命名新工作表 - 并删除其余工作表。

【问题讨论】:

  • 您要保存所有工作表还是只保存特定工作表?
  • @UdhayTitus 特定工作表

标签: c# excel


【解决方案1】:

只要在你的代码中试试这个就行了

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

您的按钮点击事件的完整代码。我测试了下面的代码,它工作正常

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

【讨论】:

  • @LV98 抱歉我没听明白,你能准确解释一下这个问题吗?
  • 这行代码string fileTemplate = "C:\\Users\\LV98\\Desktop\\Test C#\\template.xlsx"; - 是我们获取工作表名称并将其保存到新位置的源文件newbook.SaveAs(@"C:\Users\LV98\Desktop\Test C#\" + sheet.Name);- 应用程序关闭后,原始文件template.xlsx已锁定编辑。
  • 进入任务管理器并选择进程选项卡,在进程选项卡中检查是否有进程以EXCEL.exe的名称运行
  • 这就是我目前所做的 - 但需要防止在代码中发生这种情况。会做一些研究
【解决方案2】:

经过一番研究,我发现了一些有用的链接。 我在下面粘贴了一个代码 sn-p,我认为这会对您有所帮助:

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

我在以下链接中找到了上面的代码:Copy Excel worksheet to another workbook

另外,我相信您会发现阅读以下答案会有所帮助:How to copy a single Excel worksheet from one workbook to another?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多