简体   繁体   English

如何复制具有不同名称的工作表 - C# 和 Excel 互操作

[英]How to copy a sheet with a different name - C# and Excel Interop

I would like to simply copy one sheet within my workbook and give it a different name.我想简单地在我的工作簿中复制一张纸并给它一个不同的名称。

var pointName1 = workbook.Worksheets["PointName1"] as Worksheet;
pointName1.Copy(); // How do I access this newly created sheet?

Ideally I would like be able to write a method like this理想情况下,我希望能够编写这样的方法

pointName1.CopyTo("New Sheet");

where 'New Sheet' is a renamed copy of 'PointName1'.其中“New Sheet”是“PointName1”的重命名副本。

Sometimes PointName1 will be the only sheet in the workbook, other times there will be others.有时 PointName1 将是工作簿中唯一的工作表,有时会有其他工作表。

You can achieve this in multiple ways - probably the easiest way is to copy after the last sheet and then rename it using the index:您可以通过多种方式实现这一点 - 可能最简单的方法是在最后一张纸之后复制,然后使用索引重命名它:

Excel.Application xlApp = Marshal.GetActiveObject("Excel.Application") as Excel.Application;
Excel.Workbook xlWb = xlApp.ActiveWorkbook as Excel.Workbook;
Excel.Worksheet xlSht = xlWb.Sheets[1];
xlSht.Copy(Type.Missing, xlWb.Sheets[xlWb.Sheets.Count]); // copy
xlWb.Sheets[xlWb.Sheets.Count].Name = "NEW SHEET";        // rename

I believe this MSDN guide also answers your question.我相信这个MSDN 指南也回答了你的问题。

If you want to get the index of a sheet, look up Worksheet.Index property .如果要获取Worksheet.Index的索引,请查找Worksheet.Index属性

You should be able to use the Copy function, but you won't be able to rename the sheet in the same step.您应该能够使用复制功能,但您将无法在同一步骤中重命名工作表。 The MSDN documentation shows the additional parameters: MSDN文档显示了附加参数:

pointName1.Copy(pointName1, Type.Missing); //Place a copy before the existing sheet

From the documentation: If you do not specify either Before or After, Microsoft Office Excel creates a new workbook that contains the copied sheet.来自文档:如果您未指定 Before 或 After,Microsoft Office Excel 将创建一个包含复制的工作表的新工作簿。

To rename the sheet you'll need to get a reference to the new sheet (by index or name) and use the Name property of worksheet to change the name.要重命名工作表,您需要获取对新工作表的引用(按索引或名称)并使用worksheetName属性更改名称。

EDIT:编辑:

If you use the code above you can use the index of the original sheet (since you're placing the copy before the original):如果您使用上面的代码,您可以使用原始工作表的索引(因为您将副本放在原始工作表之前):

int index = pointName1.Index;
pointName1.Copy(pointName1, Type.Missing);
Worksheet newWS = (Worksheet)xlApp.Worksheets[index];

well, other solution mentioned here did not work for me.好吧,这里提到的其他解决方案对我不起作用。 I got this solution.我得到了这个解决方案。

Step 1: Create a copy of the source file (ie TempFile )步骤 1:创建源文件的副本(即 TempFile )

Step 2: Copy desired sheet of source file to TempFile步骤 2:将所需的源文件表复制到 TempFile

Step 3: Delete the source file第三步:删除源文件

Step 4: Rename TempFile to Source File.步骤 4:将 TempFile 重命名为源文件。

Note: You will need the "Microsoft.Office.Interop.Excel" package from Nuget for this solution.注意:对于此解决方案,您将需要来自 Nuget 的“Microsoft.Office.Interop.Excel”包。 Also, add using Excel = Microsoft.Office.Interop.Excel;另外,添加using Excel = Microsoft.Office.Interop.Excel;

static void Main(string[] args)
{
    Excel.Application excelApp;

    string sourceFileName = "Original.xlsx"; //Source excel file
    string tempFileName = "temp.xlsx";

    string folderPath = @"C:\FodlerPath\";

    string sourceFilePath = System.IO.Path.Combine(folderPath, sourceFileName);
    string destinationFilePath = System.IO.Path.Combine(folderPath, tempFileName);

    System.IO.File.Copy(sourceFilePath,destinationFilePath,true);

    /************************************************************************************/

    excelApp = new Excel.Application();
    Excel.Workbook wbSource, wbTarget;
    Excel.Worksheet currentSheet;

    wbSource = excelApp.Workbooks.Open(sourceFilePath);
    wbTarget = excelApp.Workbooks.Open(destinationFilePath); 

    currentSheet = wbSource.Worksheets["Sheet1"]; //Sheet which you want to copy
    currentSheet.Name = "TempSheet"; //Give a name to destination sheet

    currentSheet.Copy(wbTarget.Worksheets[1]); //Actual copy
    wbSource.Close(false);
    wbTarget.Close(true);
    excelApp.Quit();

    System.IO.File.Delete(sourceFilePath);
    System.IO.File.Move(destinationFilePath, sourceFilePath);
}

The easiest way is to copy it after the last sheet as presented in the accepted answer.最简单的方法是在接受的答案中显示的最后一张纸之后复制它。

Please note that if the excel file contains hidden sheets, the copy will be place between the visible sheets and the hidden ones, therefore the hidden sheets will be pushed to the right.请注意,如果 Excel 文件包含隐藏工作表,则副本将放置在可见工作表和隐藏工作表之间,因此隐藏工作表将被推到右侧。

If you then try to set the name with xlWb.Sheets[xlWb.Sheets.Count].Name = "NEW SHEET" you will end up renaming your last hidden sheet instead of your new copy.如果您然后尝试使用xlWb.Sheets[xlWb.Sheets.Count].Name = "NEW SHEET"设置名称,您最终将重命名您的最后一个隐藏工作表而不是您的新副本。

I have managed to get round this by accessing the new copy by it's name: xlWb.Sheets["_oldName_ (2)"].Name = "NEW SHEET" .我设法通过按名称访问新副本来解决此问题: xlWb.Sheets["_oldName_ (2)"].Name = "NEW SHEET"

Other fix that will enable you to use xlWb.Sheets[xlWb.Sheets.Count].Name = "NEW SHEET" is to set all sheets visible before copying the desired sheet.使您能够使用xlWb.Sheets[xlWb.Sheets.Count].Name = "NEW SHEET"其他修复是在复制所需工作表之前将所有工作表设置为可见。

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

相关问题 如何使用 C# 和 Excel Interop 应用过滤器并将过滤器结果复制到另一个工作表 - How to apply Filter and and copy filter results to another sheet using C# and Excel Interop 上传Excel文件并使用C#将工作表重命名为新工作表名称不使用excel interop - upload excel file and rename sheet to new sheet name using c# not use excel interop C#如何使用Interop重命名Excel工作表 - C# how to rename Excel Sheet using Interop 如何在不使用互操作的情况下通过c#取消隐藏excel表 - How to unhide an excel sheet through c# without using interop 如何从excel复制特定的工作表名称并将其保存到新位置? 使用 C# - How to copy specific sheet name from excel and save it to a new location? Using C# 如何通过使用Excel Interop添加新工作表来覆盖现有Excel文件-C# - How to Overwrite existing Excel file by adding new sheet with Excel Interop - C# C#Excel互操作获取工作表的所有单元格的名称 - C# Excel interop get names all cells of sheet 在 C# 中使用 Interop 读取大型 Excel 表格 - Read large excel sheet using Interop in C# C#COM Interop:在Excel工作表中写入单元格 - C# COM Interop: Writing to a Cell in an Excel Sheet 如何将网格线添加到一个工作表而不是另一个工作表(C#Excel Interop)? - How can I add gridlines to one sheet and not the other (C# Excel Interop)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM