Tutorials
Resources
Forum
Reviews
Communities
Interview
Jobs
Projects
Training
Your
Ad
Here
Silverlight Games
|
Mentor
|
Code Converter
|
Articles
|
Code Factory
|
Computer Jokes
|
Members
|
Peer Appraisal
|
IT Companies
|
Bookmarks
|
Polls
|
Revenue Sharing
|
Lobby
|
Gift Shop
|
Prizes & Awards
My Profile
Sign In
Register
AdSense Revenue
Active Members
Today
Pawan
(80)
Prafulla
(52)
Deepika Harida...
(51)
Last 7 Days
Alwyn
(739)
nishithraj
(512)
Manindra Upadh...
(438)
more...
Resources
»
Articles
»
General
»
About GridView Data To Excel C#
Posted Date: 05 Jun 2009
Resource Type:
Articles
Category:
General
Author:
Sayre
Member Level:
Gold
Rating:
Points
: 5
This Article will show on how to Transfer the GridView Data to Excel.
1. Suppose We have Data in GridView. Code Below will show on how to Load a Data in GridView.
//A sql query to get data from database
string sSQL = "Select * from TableName";
//Set DataTable
DataTable dt = new DataTable();
//Set SqlConnection
//yourconnectionstring ===> the sample of this string:Data Source=testserver;Initial Catalog=sampledatabase;User ID=sa;Password=sa
SqlConnection conn = New SqlConnection("yourconnectionstring");
//Set Command and DataAdapter and fill DataAdapter
SqlCommand cmd = new Sqlcommand(sSQL,conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.fill(dt);
//This Session("LoadGrid") we will use it in excel
Session("LoadGrid") = dt;
//Datasource of your Grid and Bind it.
GridView1.DataSource = Session("LoadGrid");
GridView1.DataBind();
2. Suppose we have Download Button to Excel. In the event of this button copy and paste the code Below.
Response.ClearContent()
Response.Write("
NAME OF YOUR REPORT
")
Response.Write("As of " & Date.Now.ToLongDateString & "
")
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("content-disposition", "attachment;filename=FILENAME OF YOUR REPORT_" & Date.Now.ToString("(MM dd yyyy)") & ".xls")
'Prepare to export the DataGrid
Dim oStringWriter As New System.IO.StringWriter
Dim oHtmlTextWriter As New HtmlTextWriter(oStringWriter)
Dim dgExport As New GridView
dgExport.DataSource = Session("LoadGrid")
dgExport.DataBind()
dgExport.RowStyle.VerticalAlign = VerticalAlign.Top
dgExport.HeaderRow.Style.Value = "background-color: #00acc0; color: #fff;"
dgExport.Style.Value = "font-family: Verdana;"
'Use the DataGrid control to add the details
dgExport.RenderControl(oHtmlTextWriter)
'Finish the Excel spreadsheet and send the response
Response.Write(oStringWriter.ToString())
dgExport.Dispose()
Response.End()
Quick Info: http://dotnetspider.com/sites/450/Forum-914-GridView-To-Excel.aspx
For more details, visit
http://dotnetspider.com/sites/450/Forum-914-GridView-To-Excel.aspx
Responses
No responses found. Be the first to respond and make money from
revenue sharing program
.
Feedbacks
Popular Tags
What are tags ?
Search Tags
Sign In
to add tags.
GridView
.
Excel
.
DataGrid
.
Data
.
Post Feedback
This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must
Sign In
to post a response.
Next Resource:
Short cut Keys
Previous Resource:
How to Test Your website for Mobile Web Browsers
Return to Discussion Resource Index
Post New Resource
Category:
General
Post resources and
earn money
!
More Resources
Short cut Keys
String V/s String Builder
Please Stop Spreading Sardar Ji jokes
XML versus HTML
Computer Virus
Boxing and Unboxing In C#
dotNet Slackers
About Us
Contact Us
Privacy Policy
Terms Of Use