Solved

Import datagrid data into Excel 2007 using asp.net

Posted on 2014-03-10
1
410 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 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
squareUp  challenge 22 132
Line meaning 9 82
use of cookies and regulations 1 76
delphi parse string to params 3 100
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…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

895 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now