?
Solved

Import datagrid data into Excel 2007 using asp.net

Posted on 2014-03-10
1
Medium Priority
?
422 Views
Last Modified: 2014-03-24
Hi, Experts!

I am trying to export DataGrid into Excel 2007 in my asp.net application. I am using code below.

 Response.ClearHeaders()
 Response.ClearContent()
 Response.Write("<meta http-equiv=Content-Type content='text/html; charset=utf-8'>")
 Response.AppendHeader("Content-Disposition", "attachment; filename=" & dt.TableName & ".xlsx")
 Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
 Response.Charset = ""
 Me.EnableViewState = False
 Dim tw As New System.IO.StringWriter
 Dim hw As New System.Web.UI.HtmlTextWriter(tw)
 dtResult = dt
 Table.DataSource = dt
 Table.DataBind()
 Table.RenderControl(hw)
 Response.Write(tw.ToString())
 Response.End()


But I am getting the following error when I try to open the Excel sheet....

Error:
Excel cannot open the file fileName.xlsx because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matched the format of the file.

Above code is working fine for the Excel 2003 format. I just change two lines in above code for Excel 2003

 Response.AppendHeader("Content-Disposition", "attachment; filename=" & dt.TableName & ".xls")
 Response.ContentType = "application/vnd.ms-excel"


Please suggest at earliest !!

Thanks
0
Comment
Question by:amazursky
1 Comment
 
LVL 7

Accepted Solution

by:
Kishan Zunjare earned 1000 total points
ID: 39925558
Excel 2007 cannot recognize the pure html format. You'd better use xls as the extension or  use any other technique
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses
Course of the Month16 days, 23 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question