Avatar of j1981
j1981
Flag for India asked on

Export Data into excel in excel 2007 format(xlsx)

How can I export  data  in .xlsx file format(MSExcel 2007).
Currently I could do it in MSExcell 2003 format (.xls).
I tried by putting .xlsx in
 Response.AddHeader("content-disposition", "attachment;filename=sample.xlsx") ... But this will be a corrupted file.

Desired Fix:
 I want to export data in .xlsx (MS Excel 2007) format.
private sub ExportToExcel()

	dim htmlString AS string
	htmlString = "<HTML></TABLE><tr><td>My Data</td></tr></TABLE></HTML>")
	
	Response.Clear()
	Response.Buffer = True
	
	Response.AddHeader("content-disposition", "attachment;filename=sample.xls")
	Response.Charset = ""
	Response.ContentType = "application/vnd.ms-excel"
	Response.Write(htmlString)
	Response.End()

End sub

Open in new window

.NET ProgrammingASP.NETVisual Basic.NET

Avatar of undefined
Last Comment
Nasir Razzaq

8/22/2022 - Mon
Nasir Razzaq

jagrut_patel

Set

Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

and try.

Hope this works!
jagrut_patel

Forgot to mention that in content-disposition filename extension should be .xlsx
Your help has saved me hundreds of hours of internet surfing.
fblack61
j1981

ASKER
Thanks a lot for the comments Mr jagrut_patel and CodeCruiser.  
I could not implement this in my application. Please give me some more days..
Thnx
j1981

ASKER
could not test till now.. will do in few days ..
j1981

ASKER
Hi jagrut_patel,

Sorry for the late reply. I have done as you have told me with including the new code.
It downloads in xlsx form, But shows the error attached while opening and cant open it. If we change the extension of the downloaded file to .xls it will open.

Dear CodeCruiser,

When i went through the link you have given,  The accepted solution was
"Excel 2007 cannot recognize the pure html format. You'd better use xls as the extension or render the excel format to client end."

Thanks

Response.AddHeader("content-disposition", "attachment;filename=test.xlsx")
        Response.Charset = ""
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"

Open in new window

errorMsg.JPG
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
jagrut_patel

See this ditto discussion.
Last reply on that thread explains why you get the above error.

I'm think how to achieve "render the excel format to client end" to get a valid xslx!!!!
Nasir Razzaq

>Excel 2007 cannot recognize the pure html format.
You mean you want to prove that wrong or do you want an alternative method of exporting?
jagrut_patel

I think a valid "XSLX" can be generated using either Office OpenXML or Excel Application object. I haven't tried these options. The former approach will not need Excel installed on the server but latter approach does.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
j1981

ASKER
I have the MSOffice 2007 installed. But it show the message I had attachec. If I change the extension of that downloaded excel file to .xls, it will open.
ASKER CERTIFIED SOLUTION
jagrut_patel

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
j1981

ASKER
No direct way to load in .xlsx format. Need further research as jagrut_patel suggested
Nasir Razzaq

That is what I told you in first comment right?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.