Solved

Import datagrid data into Excel 2007 using asp.net

Posted on 2014-03-10
1
407 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
Comment Utility
Excel 2007 cannot recognize the pure html format. You'd better use xls as the extension or  use any other technique
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

The purpose of this article is to demonstrate how we can use conditional statements using Python.
It’s a strangely common occurrence that when you send someone their login details for a system, they can’t get in. This article will help you understand why it happens, and what you can do about it.
This video teaches viewers about errors in exception handling.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

762 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

12 Experts available now in Live!

Get 1:1 Help Now