Hello all,
I am developing an application using VB.net,
In that i want to export a contents of gridview to the excel file.
but i am not able to do proper code for it.
How can I export datatable or gridview to excel File.
Thanks.
Printable View
Hello all,
I am developing an application using VB.net,
In that i want to export a contents of gridview to the excel file.
but i am not able to do proper code for it.
How can I export datatable or gridview to excel File.
Thanks.
You can use this code.
Code:Protected Sub btnExportToExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExportToExcel.Click
Me.lblMessage.Text = ""
Me.gdvCustomer.AllowPaging = False
'If you are binding GridView under Code Behind
'Me.gdvCustomer.DataSource = GetCustomer
Me.gdvCustomer.DataBind()
Dim tw As New StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
Dim frm As HtmlForm = New HtmlForm()
Page.Response.ContentType = "application/vnd.ms-excel"
Page.Response.AddHeader("content-disposition", "attachment;Customer.xls")
Page.Response.Charset = ""
Page.EnableViewState = False
frm.Attributes("runat") = "server"
Controls.Add(frm)
frm.Controls.Add(gdvCustomer)
frm.RenderControl(hw)
Response.Write(tw.ToString())
Response.End()
Me.gdvCustomer.AllowPaging = True
'If you are binding GridView under Code Behind
'Me.gdvCustomer.DataBind()
End Sub
Check this code.
Code:protected void BtnExport_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.Charset = "";
// If you want the option to open the Excel file without saving then
// comment out the line below
// Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
Hi,
here is a code snippet how to export DataGrid to Excel in Excel ASP.NET application with GemBox.Spreadsheet Excel .NET library:
Code:// Get DataTable that DataGrid is bound to.
var dataTable = (DataTable)dataGrid.DataSource;
// Create new ExcelFile.
var ef = new ExcelFile();
// Add new worksheet to the file.
var ws = ef.Worksheets.Add(dataTable.TableName);
// Insert the data from DataTable to the worksheet starting at cell "A1".
ws.InsertDataTable(dataTable, "A1", true);
// Stream file to browser.
Response.Clear();
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment; filename=Employee.xls");
ef.SaveXls(Response.OutputStream);
Response.End();
Here a code that you Export Grid view to Excel,you tried all are a right,and perfect you do it work,both are a different way but is a very simple to use,try it is working,really a good thing to share with all it help you to all.