Introduction
Exporting datagrid to excel is very common practice and is mostly used for reporting purposes so the exported datagrid should be fully formatted. Here I have tried a bit for this that the exported grid should be formatted enough for presentation.
How it Works
I have developed a ddl for this and refer that ddl to web project you can make changes in this ddl and can do the same but if your are not comfortable you can use the same code in your desired application. If you want to use the ddl you just need to pass some parameters your desired excel sheet will be populated.
Code
Here I will mention the code used in the ddl.
Imports System.Data.OleDb
Imports System.Web.HttpResponse
Public Class DataGrid
Inherits System.Web.UI.Page
Public Sub Excel_View(ByVal Query As String, ByVal Report_Title As String, ByVal Connection_String As String)
Dim conn As New OleDbConnection(Connection_String)
conn.Open()
Dim dg As New System.Web.UI.WebControls.DataGrid()
Dim da As New OleDbDataAdapter(Query, conn)
Dim ds As DataSet
ds = New DataSet()
da.Fill(ds)
dg.DataSource = ds
dg.DataBind()
dg.Font.Name = "Verdana"
dg.BorderStyle = System.Web.UI.WebControls.BorderStyle.Solid
dg.HeaderStyle.BackColor = System.Drawing.Color.Blue
dg.HeaderStyle.ForeColor = System.Drawing.Color.White
dg.HeaderStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center
dg.HeaderStyle.Wrap = False
dg.HeaderStyle.Font.Bold = True
context.Response.Buffer = True
context.Response.ClearContent()
context.Response.ClearHeaders()
context.Response.ContentType = "application/vnd.ms-excel"
EnableViewState = True
Dim tw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
dg.RenderControl(hw)
context.Response.Write(Now)
context.Response.Write("<b><center><font size=3 face=Verdana color=#0000FF>" & Report_Title & "</font></center></b>")
Context.Response.Write(tw.ToString())
Context.Response.Flush()
context.Response.Close()
context.Response.End()
da.Dispose()
da = Nothing
ds.Dispose()
ds = Nothing
conn.Close()
conn.Dispose()
conn = Nothing
End Sub
End Class
Wokring With Demo Project
if you want to run the attached demo then extract the zip file and place it in your localhost folder, Then create a virtual directory ExcelTest and run this in browser as http://localhost/exceltest
Working With Code
If you want to use the same ddl then download the demo project copy ddl to your project and use the following code.
<p style="TEXT-ALIGN: justify">
Dim abc As New GridToExcel.DataGrid
abc.Excel_View(query,report_title,connection_String)
</p>
but if you want to use the code you can use it according to your needs and can accordingly modify it.
Point of Interest
- Using this ddl you do not need to write the code again and again
- As you pass connection string so you donot need to be worried about the database, you can use every kind of database and pass the required database string.
- Even you can customize the report according to your desired format.
I am working as Manager Enterprise Services where key responsibilities are to support end users on their day to day IT related issues.
The other function I am responsible is IT IPCC Services Support Since September 2013 and my key responsibilities are to maintain IPCC Uptime along with end users support at both CMPak call centers. IPCC enhancements and new feature development is also part of my job role.
The third area that was handed over to me in March 2015 is in-house apps support, where key responsibility is to provide support for all the application developed by IT department and to maintain the availability of these applications.
Previously I was working as Manager Automation & Planning and this is my key area of expertise. My responsibilities were to manage team in Project Planning, System Analysis, Requirements gathering, Preparation of SRS, Presentations to client about the requirements, getting Approvals on SRS from the Client, Database Design, Development Tracking, System Deployments, end user Feedback along with Change Management.
Planning and Defining Scope, Resource Planning, Time Estimating, Creating Charts and Schedules, Risk Analysis
Managing Risks and Issues, Monitoring and Reporting Progress, Team Leadership, Working with Vendors, Scalability were the key area of working along with an efficient problem-solver in professional way with envisions business and technical perspectives to develop workable solutions.
I started my career as developer in 2004 and promoted as team lead IT Automation in 2007 and then as manager so I spend 7 years in Software Development & Project Planning.