gridview data export to excel in asp.net

前端 未结 7 1052
情歌与酒
情歌与酒 2020-12-15 11:25

i tried to transfer grid view data to excel .... But the output is a blank excel sheet.How to solve this problem?Is there any code to transfer grid view value to excel sheet

7条回答
  •  -上瘾入骨i
    2020-12-15 12:06

    The Best way is to use closedxml. Below is the link for reference

    https://closedxml.codeplex.com/wikipage?title=Adding%20DataTable%20as%20Worksheet&referringTitle=Documentation

    and you can simple use

        var wb = new ClosedXML.Excel.XLWorkbook();
    DataTable dt = GeDataTable();//refer documentaion
    
    wb.Worksheets.Add(dt);
    
    Response.Clear();
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    Response.AddHeader("content-disposition", "attachment;filename=\"FileName.xlsx\"");
    
    using (var ms = new System.IO.MemoryStream()) {
        wb.SaveAs(ms);
        ms.WriteTo(Response.OutputStream);
        ms.Close();
    }
    
    Response.End();
    

提交回复
热议问题