Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Tue Mar 19, 2024 2:16 pm

Hi, I have a C# software that's using the Spire.Xls nugets and in a specific case I want to create a copy of a sheet already present in the workbook and place it at the end of the same workbook, this worksheet contains some custom names for cells I need to update later on using the .Range[] method, when I use the AddCopy() method the new worksheet copy is created but the list of names inside is cleared, How can I duplicate a Worksheet object in the same Workbook transferring all the names as well?
For now I tried something like this:

Worksheet temp = workbook.Worksheets[j];
workbook.Worksheets.Add("CopySheetName");
workbook.Worksheets[workbook.Worksheets.Count() - 1 ].CopyFrom(temp);

And

workbook.Worksheets.AddCopy(temp, WorksheetCopyType.CopyAll);

And

workbook.Worksheets.Add("CopySheetName");
workbook.Worksheets[workbook.Worksheets.Count - 1].CopyFrom(workbook.Worksheets[j]);

But neither of these options copies the cell names in the new sheet, I'm using Spire.Xls 14.2.1

SamueleSV
 
Posts: 4
Joined: Tue Mar 19, 2024 2:01 pm

Wed Mar 20, 2024 2:23 am

Hello,

Thanks for your inquriy.
Based on your description, I simulated an Excel file and customized the A1 cell name in Sheet1 to "MyName". Then I used the following code to copy Sheet1, but did not reproduce your issue, the cell names were also copied to the new sheet, as shown in the attached screenshot. To assist us in further investigation, please provide us with your Excel file. You can attach it here or send it to this email: [email protected]. Thank you in advance.
Code: Select all
Workbook sourceWorkbook = new Workbook();
sourceWorkbook.LoadFromFile(@"test.xlsx");
Worksheet srcWorksheet = sourceWorkbook.Worksheets[0];
Worksheet target = sourceWorkbook.Worksheets.Add("CopySheetName");
sourceWorkbook.Worksheets[sourceWorkbook.Worksheets.Count - 1].CopyFrom(srcWorksheet);
String outputFile = "Output.xlsx";
sourceWorkbook.SaveToFile(outputFile, ExcelVersion.Version2013);

Sincerely,
William
E-iceblue support team
Attachments
res.png
res.png (34.08 KiB) Viewed 6551 times
User avatar

William.Zhang
 
Posts: 454
Joined: Mon Dec 27, 2021 2:23 am

Return to Spire.XLS