Last post Feb 25, 2008 04:36 AM by NareshR
Feb 18, 2008 11:31 AM|NareshR|LINK
We have a web application page that binds a datatable to a grid and then exports the content to excel format. It works fine with older office versions but gives error with office 2007 when selecting open option in save dialog box. Here's the code for it.
Protected Sub ExportToExcel()
Response.Buffer = True
Response.ContentType = "application/vnd.ms-excel"
Response.Charset = ""
Me.EnableViewState = False
Dim sw As New System.IO.StringWriter()
Dim htw As New System.Web.UI.HtmlTextWriter(sw)
grdAllInqExport.DataSource = CType(Session("ALLINQRPTDV"), DataView)
following is the error that I get .
"Excel cannot open the file "Report.xlsx" because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file."
If you have came across any solutions for this. Please provide some hint or direction.
Feb 18, 2008 11:48 AM|shieldo|LINK
For a start, the mime-type for .xslx should be "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet".
Do you really want to export to .xslx, though?
Feb 18, 2008 12:55 PM|NareshR|LINK
I made that correction, but it didn't worked.
to answer your question, No! any other format that excel 2007 can interpret will also work. Earlier it was xls which worked with older versions of excel and would give warning for excel 2007. To suppress this warning, I modified the extension to xlsx. But,
the need is to export it in a format that can work with excel 2007 without any errors/warnings.
Hope my problem is clear to you now.
Feb 18, 2008 01:12 PM|shieldo|LINK
Well, because it seems you are creating some HTML and then sending it as an Excel file mime-type, rather than using some kind of library to export it as a binary file, Excel 2007 will give a warning. Furthermore, it's correct to, because anything could
have been put in the HTML.
Why don't you try exporting your data as a CSV file? That can be read by any spreadsheet program, and it doesn't seem like you're doing anything that requires a native spreadsheet format.
Feb 25, 2008 04:36 AM|NareshR|LINK
Thanks Shieldo. I am considering the CSV approach to fix this problem. Hopefully, will get a positive response from user, since this would remove any formatting on headers (right now column header appear bold in excel sheet).