{"id":183,"date":"2025-05-04T11:24:46","date_gmt":"2025-05-04T11:24:46","guid":{"rendered":"https:\/\/www.webmobkey.com\/blog\/?p=183"},"modified":"2025-11-16T10:22:05","modified_gmt":"2025-11-16T10:22:05","slug":"asp-net-core-ile-dinamik-excel-dosyasi-olusturma","status":"publish","type":"post","link":"https:\/\/www.webmobdesign.com\/blog\/tr\/asp-net-core-ile-dinamik-excel-dosyasi-olusturma\/","title":{"rendered":"ASP.NET Core ile Dinamik Excel Dosyas\u0131 Olu\u015fturma"},"content":{"rendered":"\n<p id=\"90d6\">ASP.NET Core projelerinde dinamik olarak Excel dosyalar\u0131 olu\u015fturmak, genellikle raporlama veya veri payla\u015f\u0131m\u0131 gibi i\u015flemlerde olduk\u00e7a s\u0131k ihtiya\u00e7 duyulan bir \u00f6zelliktir. Bu blog yaz\u0131s\u0131nda, yukar\u0131daki kod \u00f6rne\u011fini kullanarak&nbsp;<strong>ClosedXML<\/strong>&nbsp;k\u00fct\u00fcphanesi yard\u0131m\u0131yla dinamik bir Excel dosyas\u0131 olu\u015fturmay\u0131 ve bunu nas\u0131l indirilebilir hale getirece\u011finizi anlataca\u011f\u0131m.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"db23\">ClosedXML Nedir?<\/h4>\n\n\n\n<p id=\"9678\">ClosedXML, .NET uygulamalar\u0131nda Excel dosyalar\u0131n\u0131 kolayca olu\u015fturman\u0131z\u0131 ve d\u00fczenlemenizi sa\u011flayan g\u00fc\u00e7l\u00fc bir k\u00fct\u00fcphanedir. Hem&nbsp;<code>.xlsx<\/code>&nbsp;format\u0131n\u0131 destekler hem de h\u00fccre bi\u00e7imlendirme, veri ekleme gibi i\u015flemleri kolayca ger\u00e7ekle\u015ftirir.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"8049\">Kodun \u0130\u015fleyi\u015fi<\/h4>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"d1ea\">1. Projenin Yap\u0131land\u0131r\u0131lmas\u0131<\/h4>\n\n\n\n<p id=\"c5fc\">ClosedXML k\u00fct\u00fcphanesini kullanmadan \u00f6nce proje \u00fczerinde a\u015fa\u011f\u0131daki NuGet paketini y\u00fcklemeniz gerekiyor:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ndotnet add package ClosedXML\n<\/pre><\/div>\n\n\n<p id=\"9126\">Bu paket Excel dosyalar\u0131 olu\u015fturmak ve d\u00fczenlemek i\u00e7in gerekli t\u00fcm \u00f6zellikleri sa\u011flar.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"0ef1\">2. Controller\u2019da Excel Olu\u015fturma<\/h4>\n\n\n\n<p id=\"d6f3\">Kodun temelinde iki ana fonksiyon bulunuyor:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code><strong>Index<\/strong><\/code><strong>\u00a0Action Method:<\/strong><\/li>\n\n\n\n<li>Dinamik veri listeleri (<code>data1<\/code>\u00a0ve\u00a0<code>data2<\/code>) olu\u015fturur.<\/li>\n\n\n\n<li><code>CreateExcelFile<\/code>\u00a0fonksiyonunu \u00e7a\u011f\u0131rarak Excel dosyas\u0131n\u0131 olu\u015fturur.<\/li>\n\n\n\n<li>Excel dosyas\u0131n\u0131 indirilebilir bir dosya olarak d\u00f6ner.<\/li>\n\n\n\n<li><code><strong>CreateExcelFile<\/strong><\/code><strong>\u00a0Method:<\/strong><\/li>\n\n\n\n<li>Verilen verilerden dinamik bir Excel dosyas\u0131 olu\u015fturur.<\/li>\n\n\n\n<li><strong>Ba\u015fl\u0131k sat\u0131r\u0131<\/strong>\u00a0ve\u00a0<strong>veri sat\u0131rlar\u0131n\u0131<\/strong>\u00a0ekler.<\/li>\n\n\n\n<li>S\u00fctun ba\u015fl\u0131klar\u0131na sar\u0131 renk ekler ve s\u00fctun geni\u015fliklerini ayarlar.<\/li>\n\n\n\n<li>T\u00fcm Excel verilerini bir\u00a0<code>MemoryStream<\/code>\u00a0kullanarak byte dizisine d\u00f6n\u00fc\u015ft\u00fcr\u00fcr.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"9d63\">3. Ba\u015fl\u0131k Sat\u0131rlar\u0131n\u0131n Eklenmesi<\/h4>\n\n\n\n<p id=\"0981\">Ba\u015fl\u0131k sat\u0131rlar\u0131, her bir veri listesinin ilk eleman\u0131n\u0131n anahtarlar\u0131 (keys) kullan\u0131larak dinamik \u015fekilde olu\u015fturulur. H\u00fccrelerin sar\u0131 renkle vurgulanmas\u0131 ve s\u00fctun geni\u015fliklerinin ayarlanmas\u0131 kodda \u015fu \u015fekilde yap\u0131lm\u0131\u015ft\u0131r:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; title: ; notranslate\" title=\"\">\nvar columnNames = data.First().Keys.ToList();\nfor (int i = 0; i &lt; columnNames.Count; i++)\n{\n    var cell = worksheet.Cell(currentRow, i + 1);\n    cell.Value = columnNames&#x5B;i];\n    cell.Style.Fill.BackgroundColor = XLColor.Yellow; \/\/ Sar\u0131 renk ayar\u0131\n    worksheet.Column(i + 1).Width = 50; \/\/ S\u00fctun geni\u015fli\u011fi ayar\u0131\n}\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\" id=\"292b\">4. Dinamik Veri Ekleme<\/h4>\n\n\n\n<p id=\"2406\">Her veri sat\u0131r\u0131, dinamik olarak belirlenen s\u00fctun isimlerine g\u00f6re Excel dosyas\u0131na eklenir:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; title: ; notranslate\" title=\"\">\nforeach (var row in data)\n{\n    currentRow++;\n    for (int i = 0; i &lt; columnNames.Count; i++)\n    {\n        worksheet.Cell(currentRow, i + 1).Value = row&#x5B;columnNames&#x5B;i]]?.ToString();\n    }\n}\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\" id=\"28e7\">5. Listeler Aras\u0131nda Bo\u015fluk B\u0131rakma<\/h4>\n\n\n\n<p id=\"3ba9\">E\u011fer birden fazla listeyi ayn\u0131 Excel dosyas\u0131na ekliyorsan\u0131z, listeler aras\u0131nda bo\u015f sat\u0131r b\u0131rakarak verilerin ayr\u0131\u015fmas\u0131n\u0131 sa\u011flayabilirsiniz:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ncurrentRow += 2; \/\/ Listeyi bitirdikten sonra 2 sat\u0131r atla\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\" id=\"ae22\">Kodun \u00c7\u0131kt\u0131s\u0131: \u00d6rnek Excel Dosyas\u0131<\/h4>\n\n\n\n<p id=\"0364\">Bu kod \u00e7al\u0131\u015ft\u0131r\u0131ld\u0131\u011f\u0131nda \u015fu \u015fekilde bir Excel dosyas\u0131 olu\u015fturulur:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u0130lk sayfa, birden fazla listeyi i\u00e7erir.<\/li>\n\n\n\n<li>Ba\u015fl\u0131k sat\u0131rlar\u0131 sar\u0131 renkle vurgulan\u0131r.<\/li>\n\n\n\n<li>Listeler aras\u0131nda bo\u015f sat\u0131rlarla g\u00f6rsel ayr\u0131m yap\u0131l\u0131r.<\/li>\n<\/ul>\n\n\n\n<p id=\"4526\">Tam Kod<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: xml; title: ; notranslate\" title=\"\">\nusing ExcelList.Models;\n\/\/ Controllers\/HomeController.cs\nusing Microsoft.AspNetCore.Mvc;\nusing ClosedXML.Excel;\n\nnamespace YourNamespace.Controllers\n{\n    public class HomeController : Controller\n    {\n        public IActionResult Index()\n        {\n            \/\/ Dinamik liste objelerini olu\u015ftural\u0131m\n            var data1 = new List&lt;Dictionary&lt;string, object&gt;&gt;\n            {\n                new Dictionary&lt;string, object&gt; { { &quot;ID&quot;, 1 }, { &quot;Name&quot;, &quot;\u00dcr\u00fcn A&quot; }, { &quot;Price&quot;, 100.0 } },\n                new Dictionary&lt;string, object&gt; { { &quot;ID&quot;, 2 }, { &quot;Name&quot;, &quot;\u00dcr\u00fcn B&quot; }, { &quot;Price&quot;, 150.5 } },\n                new Dictionary&lt;string, object&gt; { { &quot;ID&quot;, 3 }, { &quot;Name&quot;, &quot;\u00dcr\u00fcn C&quot; }, { &quot;Price&quot;, 200.75 } }\n            };\n\n            var data2 = new List&lt;Dictionary&lt;string, object&gt;&gt;\n            {\n                new Dictionary&lt;string, object&gt; { { &quot;ID&quot;, 4 }, { &quot;Name&quot;, &quot;\u00dcr\u00fcn D&quot; }, { &quot;Price&quot;, 300.0 } },\n                new Dictionary&lt;string, object&gt; { { &quot;ID&quot;, 5 }, { &quot;Name&quot;, &quot;\u00dcr\u00fcn E&quot; }, { &quot;Price&quot;, 400.5 } },\n                new Dictionary&lt;string, object&gt; { { &quot;ID&quot;, 6 }, { &quot;Name&quot;, &quot;\u00dcr\u00fcn F&quot; }, { &quot;Price&quot;, 500.75 } }\n            };\n\n            \/\/ Dosya ismini burada tan\u0131mlayal\u0131m\n            string fileName = &quot;CustomExcelFileName.xlsx&quot;;\n\n            \/\/ Excel dosyas\u0131n\u0131 olu\u015fturmak i\u00e7in CreateExcelFile fonksiyonunu \u00e7a\u011f\u0131r\u0131yoruz\n            var excelFile = CreateExcelFile(new List&lt;List&lt;Dictionary&lt;string, object&gt;&gt;&gt; { data1, data2 }, fileName);\n\n            return File(excelFile, &quot;application\/vnd.openxmlformats-officedocument.spreadsheetml.sheet&quot;, fileName);\n        }\n\n        private byte&#x5B;] CreateExcelFile(List&lt;List&lt;Dictionary&lt;string, object&gt;&gt;&gt; dataLists, string fileName)\n        {\n            using (var workbook = new XLWorkbook())\n            {\n                var worksheet = workbook.Worksheets.Add(&quot;Data&quot;);\n                int currentRow = 1;\n\n                foreach (var data in dataLists)\n                {\n                    if (data.Count &gt; 0)\n                    {\n                        \/\/ Ba\u015fl\u0131k sat\u0131r\u0131n\u0131 ekleyelim\n                        var columnNames = data.First().Keys.ToList();\n                        for (int i = 0; i &lt; columnNames.Count; i++)\n                        {\n                            var cell = worksheet.Cell(currentRow, i + 1);\n                            cell.Value = columnNames&#x5B;i];\n                            cell.Style.Fill.BackgroundColor = XLColor.Yellow; \/\/ Sar\u0131 renk ayar\u0131\n                            worksheet.Column(i + 1).Width = 50; \/\/ S\u00fctun geni\u015fli\u011fi ayar\u0131\n                        }\n\n                        \/\/ Verileri ekleyelim\n                        foreach (var row in data)\n                        {\n                            currentRow++;\n                            for (int i = 0; i &lt; columnNames.Count; i++)\n                            {\n                                worksheet.Cell(currentRow, i + 1).Value = row&#x5B;columnNames&#x5B;i]]?.ToString();\n                            }\n                        }\n\n                        \/\/ Her liste aras\u0131nda bo\u015f bir sat\u0131r b\u0131rak\n                        currentRow += 2; \/\/ Listeyi bitirdikten sonra 2 sat\u0131r atla\n                    }\n                }\n\n                \/\/ Excel dosyas\u0131n\u0131 haf\u0131zaya kaydedelim ve byte dizisi olarak d\u00f6nd\u00fcrelim\n                using (var stream = new MemoryStream())\n                {\n                    workbook.SaveAs(stream);\n                    return stream.ToArray();\n                }\n            }\n        }\n    }\n}\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>ASP.NET Core projelerinde dinamik olarak Excel dosyalar\u0131 olu\u015fturmak, genellikle raporlama veya veri payla\u015f\u0131m\u0131 gibi i\u015flemlerde olduk\u00e7a &hellip; <a title=\"ASP.NET Core ile Dinamik Excel Dosyas\u0131 Olu\u015fturma\" class=\"hm-read-more\" href=\"https:\/\/www.webmobdesign.com\/blog\/tr\/asp-net-core-ile-dinamik-excel-dosyasi-olusturma\/\"><span class=\"screen-reader-text\">ASP.NET Core ile Dinamik Excel Dosyas\u0131 Olu\u015fturma<\/span>Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[],"class_list":["post-183","post","type-post","status-publish","format-standard","hentry","category-web-tr"],"_links":{"self":[{"href":"https:\/\/www.webmobdesign.com\/blog\/wp-json\/wp\/v2\/posts\/183","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webmobdesign.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webmobdesign.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webmobdesign.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webmobdesign.com\/blog\/wp-json\/wp\/v2\/comments?post=183"}],"version-history":[{"count":1,"href":"https:\/\/www.webmobdesign.com\/blog\/wp-json\/wp\/v2\/posts\/183\/revisions"}],"predecessor-version":[{"id":322,"href":"https:\/\/www.webmobdesign.com\/blog\/wp-json\/wp\/v2\/posts\/183\/revisions\/322"}],"wp:attachment":[{"href":"https:\/\/www.webmobdesign.com\/blog\/wp-json\/wp\/v2\/media?parent=183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webmobdesign.com\/blog\/wp-json\/wp\/v2\/categories?post=183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webmobdesign.com\/blog\/wp-json\/wp\/v2\/tags?post=183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}