Export data from datatable to Excel
-
Hi all, Can any one tell me how to export the data from datatable to excel without using Microsoft.office.tools..... and formating the cells in the excel like in one cell entering 2-3 values etc. Thanks in advance..
sarangkhandare wrote:
Can any one tell me how to export the data from datatable to excel
sarangkhandare wrote:
formating the cells in the excel like in one cell entering 2-3 values
I don't understand this point. Can you be please more specific? Thank you!
cheers, Abhijit CodeProject MVP Web Site:abhijitjana.net
-
Hi all, Can any one tell me how to export the data from datatable to excel without using Microsoft.office.tools..... and formating the cells in the excel like in one cell entering 2-3 values etc. Thanks in advance..
give a linkbutton "export to excel" and do the following code on its click event Dim Grid As New System.Web.UI.WebControls.GridView Try Grid.AutoGenerateColumns = True Grid.RowStyle.Wrap = True Grid.AlternatingRowStyle.Wrap = True Grid.DataSource = [YOUR DATASET] Grid.DataBind() -- header style required by you for excel Grid.HeaderRow.Cells(0).Width = 200 Grid.HeaderRow.Style("font-family") = "Verdana" Grid.HeaderRow.Style("font-size") = "12px" Grid.HeaderRow.Font.Bold = True Grid.HeaderRow.ForeColor = Color.White Grid.HeaderRow.BackColor = Color.Blue For Each row As System.Web.UI.WebControls.GridViewRow In Grid.Rows -- row style required by you for excel row.Cells(0).Width = 100 row.Style("font-family") = "Verdana" row.Style("font-size") = "12px" row.Style("text-align") = "left" Next Response.Clear() Response.AddHeader("content-disposition", "attachment;filename=" & [YOUR PREFERRED FILE NAME] & ".xls") Response.Charset = "" Response.Cache.SetCacheability(HttpCacheability.NoCache) Response.ContentType = "application/vnd.xls" Dim stringWrite As New System.IO.StringWriter() Dim htmlWrite As New HtmlTextWriter(stringWrite) Grid.RenderControl(htmlWrite) Response.Write(stringWrite.ToString()) Response.End() Catch ex As Exception Finally Grid.Dispose() Grid = Nothing End Try
Regards, Kapil Thakur (Where's there is Kapil , there is a way) - thakur.kapil@gmail.com