[英]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.要重命名工作表,您需要获取对新工作表的引用(按索引或名称)并使用
worksheet
的Name
属性更改名称。
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.