ASP.NET MVC Excele Aktarma

ASP.NET MVC Excele Aktarma (ASP.NET MVC Export to Excel)



Controller

public void GridExportToExcel()
        {
            string dosyaAdi = "denemeExcelDosya";
            var table = bll_Deneme.GetAll(); // Veri Datanınızdan Veya Excele Gömmek 
 
            var grid = new GridView();
            grid.DataSource = table;
            grid.DataBind();
 
            Response.ClearContent();
            Response.AddHeader("content-disposition""attachment; filename=" + dosyaAdi + ".xls");
 
            Response.ContentType = "application/excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
 
            grid.RenderControl(htw);
 
            Response.Write(sw.ToString());
            Response.End();
        }
Yapmamız gereken View sayfasından bu Controller ı tetiklemek. Örneğin;

Script
<script>
    $(document).ready(function () {
        $("#export").click(function () {
            ExportToExcel();
        });
    });
 
    function ExportToExcel()
    {
        var url = '@Url.Action("GridExportToExcel""Deneme")';
        window.open(url);
    }
</script> 

HTML

<a href="#" id="export">Excele Aktar</a>

Hiç yorum yok:

Yorum Gönder