Sayfalar

17 Ocak 2013 Perşembe

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


Controller
public void GridExportToExcel()
{
    string dosyaAdi = "ornek_dosya_adi";
    var table = // Buraya veritabanınından gelen herhangi bir dataSource gelebilir.( DataTable, DataSet, kendi oluşturduğunuz, herhangi bir ICollection tipinde entitiy model)
    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", "ExportController")';
        window.open(url);
    }
</script> 

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

Excele aktarma işlemleri hazır. Excele aktar linkine tıklayınca sizin belirttiginiz bir DataSource u excel dosyasına yazdırıp download edecektir. İsterseniz url içerinde parametre gönderip, controller metodunda bu parametreye göre DataSource oluşturabilirsiniz.

13 yorum:

  1. Gördüğüm en temiz excel aktarımı tebrikler.

    YanıtlaSil
  2. Mükemmelsiniz Gerçekten Teşekkür Ederim

    YanıtlaSil
  3. Hocam yalnız Türkçe karter problemi yaşıyoruz

    YanıtlaSil
  4. Selamun Aleykum;
    Kodunuzdan istifade ettim. Çorbada benimde katkım olsun Hesabından.
    Tabel değişkeni için kod bloğu ;

    var products = new System.Data.DataTable("teste");
    products.Columns.Add("col1", typeof(int));
    products.Columns.Add("col2", typeof(string));

    products.Rows.Add(1, "product 1");
    products.Rows.Add(2, "product 2");
    products.Rows.Add(3, "product 3");
    products.Rows.Add(4, "product 4");
    products.Rows.Add(5, "product 5");
    products.Rows.Add(6, "product 6");
    products.Rows.Add(7, "product 7");


    var grid = new GridView();
    grid.DataSource = products;
    grid.DataBind();

    YanıtlaSil
  5. Bu yorum yazar tarafından silindi.

    YanıtlaSil
  6. Bu yorum yazar tarafından silindi.

    YanıtlaSil
  7. En temiz en kolay en en en... excel export kodu
    paylaşım için teşekkürler. Peki excel dosyasına dosyayı oluşturanın bilgilerini nasıl ekleyebiliriz

    YanıtlaSil
  8. Türkçe karakter sorununu çözdüm

    var table = *************;
    var grid = new GridView();
    grid.DataSource = table;
    grid.DataBind();

    Response.ClearContent();
    Response.AddHeader("content-disposition", string.Format("attachment; filename=******* {0}.xls", Helper.GetDateTimeNow().ToShortDateString()));
    Response.ContentType = "application/ms-excel";
    Response.ContentEncoding = System.Text.Encoding.Unicode;
    Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());

    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);

    grid.RenderControl(htw);

    Response.Write(sw.ToString());
    Response.End();

    YanıtlaSil
  9. Bu yorum yazar tarafından silindi.

    YanıtlaSil
  10. Valla beni öle bi sıkıntıdan kurtardın ki Allah razı olsun

    YanıtlaSil
  11. Bu yorum yazar tarafından silindi.

    YanıtlaSil