Solved

Import datagrid data into Excel 2007 using asp.net

Posted on 2014-03-10
1
417 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 7

Accepted Solution

by:
Kishan Zunjare earned 500 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

Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

Question has a verified solution.

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

Thoughout my experience working on eCommerce web applications I have seen applications succumbing to increased user demand and throughput. With increased loads the response times started to spike, which leads to user frustration and lost sales. I ha…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

690 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