I have a module where I write data into an Excel document and upload it on a server. It works when I create an excel in .xls format. But when I save it in .xlsx format, MS Excel complains about the content being unreadable. Although it still repairs the file and opens up, it looks like there is some issue in saving to the stream or reading from stream.
Hi,
Thanks for using Aspose.Cells for .NET.
Please download and use the latest version:
Aspose.Cells
for .NET v7.3.0.5
There are many ways of opening and saving the workbooks in different formats. Please refer to these articles for your complete reference.
Also, for your help, I have written the following code. Please copy and use it at your end. The code creates a workbook object and then write a Hello Aspose message in cell A1. Then it saves the workbook in XLS and XLSX formats.
Please also see the generated output xls and xlsx files as well as screenshot for your reference.
C#
//Create a workbook
Workbook workbook = new Workbook();
//Access the first worksheet
Worksheet worksheet = workbook.Worksheets[0];
//Write message in cell A1
Cell a1 = worksheet.Cells[“A1”];
a1.PutValue(“Hello Aspose!”);
//Autofit the columns.
worksheet.AutoFitColumns();
//Save the workbook in xls format
workbook.Save(“output.xls”, SaveFormat.Excel97To2003);
//Save the workbook in xlsx format
workbook.Save(“output.xlsx”, SaveFormat.Xlsx);
Screenshot:
Thanks for the quick response.
Hi,
Well, I have checked your code and it is quite ok, but you do not need to differentiate between xls and xlsx except that
For Xlsx, please use OoxmlSaveOptions and for Xls, please use XlsSaveOptions. See the following codes.
C#
//For Xlsx
OoxmlSaveOptions opts1 = new OoxmlSaveOptions(SaveFormat.Xlsx);
workbook.Save(“output.xlsx”, opts1);
//For Xls
XlsSaveOptions opts2 = new XlsSaveOptions(SaveFormat.Excel97To2003);
workbook.Save(“output.xls”, opts2);
Please also do not use Workbook.SaveToStream but use this code instead. SaveToStream will not work for XLSX format.
C#
MemoryStream ms1 = new MemoryStream();
workbook.Save(ms1, SaveFormat.Xlsx);
//Or
MemoryStream ms2 = new MemoryStream();
workbook.Save(ms2, SaveFormat.Excel97To2003);
If you want to convert your workbook into byte array, please use this code.
C#
workbook.Save(ms1, SaveFormat.Xlsx);
byte[] bytes = ms1.ToArray();
Hi,
I think I narrowed down the problem. It looks like the issue is using ‘Range’ to populate data and saving in .Xlsx format. When I removed that part, it works fine.
Hi,
Thanks for your feedback.
I think, you rightly narrowed down the problem.Please get rid of range code and use the method mentioned by us.
We will also look into range related code and resolve the issue if we found any error.
Please also let us know if you need any further help, we will be glad to assist you asap.
I verified that using Range and saving in Xlsx format corrupts the excel. Would you log a ticket and try to fix it in the next Aspose Cells version?
Hi,
Thanks for your posting and considering Aspose.Cells.
I was able to replicate your mentioned issue using the following code with the latest version:
Aspose.Cells
for .NET v7.3.0.5
I have logged this issue in our database. We will look into it and fix the problems and get back to you asap.
This issue has been logged as CELLSNET-40966.
I have also attached the generated output.xlsx file and the screenshot showing the error.
C#
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
Range range = worksheet.Cells.CreateRange(0, 0, 1, 1);
//Setting the name of the named range
range.Name = “Cover Sheet”;
range[0, 0].PutValue(“Blah”);
workbook.Save(“output.xlsx”);
Screenshot:
CodeRed:The following saves excel in corrupt format: (While opening .Xlsx, it shows a 'content unreadable' exception)Range range = wsCoverSheet.Cells.CreateRange(0, 0, 1, 1);//Setting the name of the named rangerange.Name = "Cover Sheet";range[0, 0].PutValue("Blah");You have put space while setting the Name of the range, this is not allowed while creating named ranges in MS Excel either, you may confirm this behavior in MS Excel, it is the limitation of MS Excel and nothing to do with Aspose.Cells for .NET component that follows MS Excel standards.Please change your code to:Range range = wsCoverSheet.Cells.CreateRange(0, 0, 1, 1);//Setting the name of the named rangerange.Name = "CoverSheet";//Or//range.Name = "Cover_Sheet";range[0, 0].PutValue("Blah");Now, it will work fine.Thanks for your understanding!
It works perfectly fine when I generate an excel in.xls format. We have been adding spaces in the range names and it worked fine for us earlier. Does getting rid of spaces fix the ‘unreadable content’ issue with Xlsx file?
Hi,