Can not rename a sheet name of xls file

Can not rename a sheet name of xls file, and I got a xls file with nothing in it



code:

public static Byte[] RenameSheets(Byte[] fileBytes, params String[] sheetNames)
{
using (var fileStream = new MemoryStream(fileBytes))
{
var workbook = new Workbook(fileStream, new LoadOptions(LoadFormat.Excel97To2003));
for (int i = 0; i < sheetNames.Length; i++)
{
workbook.Worksheets[i].Name = sheetNames[i];
}
using (var resultStream = workbook.SaveToStream())
{
return resultStream.ToBytes();
}
}
}

Hi Louis,


Thank you for contacting Aspose support.

I have evaluated the presented scenario while using the latest version of Aspose.Cells for .NET (Latest Version) and following piece of code. The resultant file is correct, and does not require to be repaired as of your original resultant spreadsheet. This suggests that your current version of the API has some bug in this regard. Please give a try to the latest version on your side as well. In case the problem persists, please provide us an executable sample application to replicate the issue on our end.

C#

string[] sheetNames = new string[] { “one” };
using (var fileStream = new MemoryStream(System.IO.File.ReadAllBytes(dir + “1.xls”)))
{
var workbook = new Workbook(fileStream, new LoadOptions(LoadFormat.Excel97To2003));
for (int i = 0; i < sheetNames.Length; i++)
{
workbook.Worksheets[i].Name = sheetNames[i];
}
using (var resultStream = workbook.SaveToStream())
{
System.IO.File.WriteAllBytes(dir + “output.xls”, resultStream.ToArray());
}
}

Hi,


Thanks for providing us some details and template files.

Well, if you are renaming a worksheet in the spreadsheet (e.g XLS/XLSX, etc.) and converting to some text format (e.g Tab Delimited), the worksheet’s name won’t be changed. This behavior is same as per MS Excel and you may confirm this in MS Excel manually, e.g you may rename the worksheet and convert to Tab Delimited (Text) format, you will see that the sheet’s name is not changed at all, rather the sheet’s name would be file’s name. This is expected behavior.

Furthermore, if you are renaming a worksheet and then convert to XLS, XLSX or other spreadsheet file formats, it works fine, see the following sample code that works fine for your reference:
e.g
Sample code:

Workbook workbook = new Workbook(“e:\test2\1.xls”);
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Name = “MySheet1”;
workbook.Save(“e:\test2\out1new1.xls”)


Let us know if you still have any issue.

Thank you.

Thanks for reply,


unfortunately, I still got a empty xls file when rename a sheet of the 1.xls file. When I opened the result xls file by office 2013. it opened a alert window says: The file format and extension of ‘result.xls’ don’t match. The file could be corrupted or unsafe. Unless you trust its source, don’t open it. Do you want to open it anyway?

attachment is the file converted by the latest version of Aspose.Cells for .NET (Latest Version) Could you send me your xls file you converted?

Thanks for reply


I was not converting the xls file to any text format. I just renamed the first sheet name of 1.xls and saved it as result.xls. But the result file has nothing in it. And when I opened the result.xls file. the office 2013 alert a window say:The file format and extension of ‘result.xls’ don’t match. The file could be corrupted or unsafe. Unless you trust its source, don’t open it. Do you want to open it anyway?


Hi,


I have evaluated your attached file “result.xls”, well, it is actually Text (Tab Delimited) file, so there is some issue with your code and you might be saving the output streams to text file format instead of XLS. For confirmation, please open your output file into MS Excel (after getting/clearing the error message by MS Excel as you mentioned), now Save it as and you will see “Text (Tab Delimited)” as its save as type in Save As dialog box in MS Excel, so it is a Tab Delimited file whose extension is set as XLS which results in MS Excel to pop up an error message.

I have attached my output file which is created by using my sample code in my previous post for your reference.

As requested earlier, please please create a sample executable application/project, zip it and post us here to replicate the issue on our end, we will check it soon.

Thank you.