i built a patient management software for a clinic and i need to export patiet list from ASP.net grid view to excel file
my question is:
Is there a way to export gridview to excel i am using vb.net and visual web developer 2010
i store datasource from advanced search page into a session and redirect to result page here is the code of result page
Partial Class Sresults
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
GridView1.DataSource = Session("dsource")
GridView1.DataBind()
End Sub
Protected Sub Backbtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Backbtn.Click
Session("dsource") = ""
Response.Redirect("searchme.aspx")
End Sub
Protected Sub Page_PreRenderComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRenderComplete
Response.Write(GridView1.Rows.Count.ToString + " Records")
End Sub
End Class
try below code on the button click
// 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();