Add an “Export to Excel” button to a webpage to export gridview to excel in webapplication

后端 未结 2 1991
借酒劲吻你
借酒劲吻你 2021-01-16 02:50

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 ex

2条回答
  •  梦谈多话
    2021-01-16 03:05

    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();
    

提交回复
热议问题