Solved

Import datagrid data into Excel 2007 using asp.net

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

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

Suggested Solutions

Title # Comments Views Activity
Using YubiKey with REST API application 2 136
Places to advertise 6 63
Connecting to multiple databases to create a Dashboard 5 69
Adjust the codes 3 31
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
This video teaches viewers about errors in exception handling.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

749 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