Not opening the Excel file

Hi,

Today I have downloaded the trial version of aspose.cells 5.2.2 to evaluate whether this product fits in our project or not. I have below simple code which is not working. its not opening the excel file. please advice.

Dim wb As Workbook = New Workbook()

Dim sheet As Worksheet = wb.Worksheets(0)

Dim cell As Cell = sheet.Cells("A1")

cell.PutValue("Hello !")

wb.Save(Response, "HelloWorld.xls", ContentDisposition.Attachment, New XlsSaveOptions())

Many Thanks,

Bhavin

Hi Bhavan,

Thanks for considering Aspose.Cells.

Please try this code

workbook.Save(“YourFileName.xls”, FileFormatType.Excel97To2003, SaveType.OpenInExcel, System.Web.HttpContext.Current.Response)

End response to avoid unneeded HTML after XLS
System.Web.HttpContext.Current.Response.End()

Hi Shakeel,

Thanks for the quick respose but the above code is not producing any excel file. also, with above syntex of Save() method, I am getting green underline saying - "Public Sub Save(fileName As String, fileFormatTpe As Aspose.Cells.FileFormatType, saveType As Aspose.Cells.SaveType, response As System.Web.HttpResponse) is obsolete: 'Use Workbook.Save(System.Web.HttpResponse, string, ContenDisposition, SaveOptions) method instead.'".

Just some more info: I am also getting Javascript error after executing above suggested method: "System Error: - 1072896748".

I have Excel 2007 installed on my machine.

Dim wb As Workbook = New Workbook()

Dim sheet As Worksheet = wb.Worksheets(0)

Dim cell As Cell = sheet.Cells("A1")

cell.PutValue("Hello !")

wb.Save("Hello.xls", FileFormatType.Excel97To2003, SaveType.OpenInExcel, System.Web.HttpContext.Current.Response)

System.Web.HttpContext.Current.Response.End()

Many Thanks,

Bhavin

Hi Bhavan,

Thanks for letting me know. The Save method is actually obsolete, now the new code given below uses the newer method.

I created a sample application for you. Please download it from the attachment.

Here is a correct code.

VB.NET


Dim wb As Workbook = New Workbook()

Dim sheet As Worksheet = wb.Worksheets(0)


Dim cell As Cell = sheet.Cells(“A1”)

cell.PutValue(“Hello !”)


Dim responseStream As System.Web.HttpResponse = System.Web.HttpContext.Current.Response

wb.Save(responseStream, “Hello.xlsx”, ContentDisposition.Attachment, New XlsSaveOptions)

responseStream.End()



Hi Shakeel,

I am not able to open this "Hello.xlsx " file generated from your sample application. I can only save this. While opening the file generated from your sample application, I am getting this message: "Excel cannot open the file 'Hello.xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extention matches the format of the file."

Please advice ASAP. We have to move fast for this.

Many thanks,

Bhavin

Hi Bhavin,

Thanks for letting me know, the code I gave you, it should not be “Hello.xlsx” but it should be "Hello.xls"

However, if you want to get Xlsx file not Xls file, then you should modify my code like this.



Dim wb As Workbook = New Workbook()

Dim sheet As Worksheet = wb.Worksheets(0)


Dim cell As Cell = sheet.Cells(“A1”)

cell.PutValue(“Hello !”)


Dim xSaveOptions As New OoxmlSaveOptions

xSaveOptions.SaveFormat = SaveFormat.Xlsx


Dim responseStream As System.Web.HttpResponse = System.Web.HttpContext.Current.Response

wb.Save(responseStream, “Hello.xlsx”, ContentDisposition.Attachment, xSaveOptions)

responseStream.End()



Hi,

Moreover, we recommend you to see the topic for your complete reference:
http://www.aspose.com/documentation/.net-components/aspose.cells-for-.net/saving-files.html

Thank you.