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());
}
}
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?
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.