Worksheet.Copy does not copy the name of the Worksheet


#1

In Excel, when you copy a worksheet from one workbook to another, the name of that worksheet is also copied.
Is it possible to have your worksheet.Copy function maintain this behavior?

Here is sample code.

Workbook excelWorkbook0 = new Workbook();
Workbook excelWorkbook1 = new Workbook();
excelWorkbook0.Worksheets[0].Name = "Worksheet 0";
excelWorkbook1.Worksheets[0].Copy(excelWorkbook0.Worksheets[0]);

excelWorkbook0.Save(@"C:\Final0.xls", FileFormatType.Default);
excelWorkbook1.Save(@"C:\Final1.xls", FileFormatType.Default);

It does not copy the worksheet name in all version that I have tried.
3.7.1.0
3.9.1.0
4.0.2.0
4.0.2.6
4.0.2.7
4.0.2.8


#2

Worksheet.Copy method doesn't copy sheet name to avoid sheet name collision.

It's easy to copy the sheet name if you are sure there are no collisions:

Workbook excelWorkbook0 = new Workbook();
Workbook excelWorkbook1 = new Workbook();
excelWorkbook0.Worksheets[0].Name = "Worksheet 0";
excelWorkbook1.Worksheets[0].Copy(excelWorkbook0.Worksheets[0]);

excelWorkbook1.Worksheets[0].Name = excelWorkbook0.Worksheets[0].Name;

excelWorkbook0.Save(@"C:\Final0.xls", FileFormatType.Default);
excelWorkbook1.Save(@"C:\Final1.xls", FileFormatType.Default);


#3

OK, that is what I was doing.

Thanks for checking. James