Writing to Excel Sheet Using EPPlus in C#
EPPlus is a very helpful open-source 3rd party DLL for writing data to excel. EPPlus supports multiple properties of spreadsheets like cell ranges, cell styling, charts, pictures, shapes, comments, tables, protection, encryption, pivot tables, data validation, conditional formatting, formula calculation, etc.
First of all install EPPlus using Packet Manager console by writing the following command:
Install-Package EPPlus
Let’s see how to create and write to an excel-sheet using C#.
using System; using System.IO; // The following to two namespace contains // the functions for manipulating the // Excel file using OfficeOpenXml; using OfficeOpenXml.Style; class Program { static void Main(string[] args) { var Articles = new[] { new { Id = "101", Name = "C++" }, new { Id = "102", Name = "Python" }, new { Id = "103", Name = "Java Script" }, new { Id = "104", Name = "GO" }, new { Id = "105", Name = "Java" }, new { Id = "106", Name = "C#" } }; // Creating an instance // of ExcelPackage ExcelPackage excel = new ExcelPackage(); // name of the sheet var workSheet = excel.Workbook.Worksheets.Add("Sheet1"); // setting the properties // of the work sheet workSheet.TabColor = System.Drawing.Color.Black; workSheet.DefaultRowHeight = 12; // Setting the properties // of the first row workSheet.Row(1).Height = 20; workSheet.Row(1).Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; workSheet.Row(1).Style.Font.Bold = true; // Header of the Excel sheet workSheet.Cells[1, 1].Value = "S.No"; workSheet.Cells[1, 2].Value = "Id"; workSheet.Cells[1, 3].Value = "Name"; // Inserting the article data into excel // sheet by using the for each loop // As we have values to the first row // we will start with second row int recordIndex = 2; foreach (var article in Articles) { workSheet.Cells[recordIndex, 1].Value = (recordIndex - 1).ToString(); workSheet.Cells[recordIndex, 2].Value = article.Id; workSheet.Cells[recordIndex, 3].Value = article.Name; recordIndex++; } // By default, the column width is not // set to auto fit for the content // of the range, so we are using // AutoFit() method here. workSheet.Column(1).AutoFit(); workSheet.Column(2).AutoFit(); workSheet.Column(3).AutoFit(); // file name with .xlsx extension string p_strPath = "H:\\geeksforgeeks.xlsx"; if (File.Exists(p_strPath)) File.Delete(p_strPath); // Create excel file on physical disk FileStream objFileStrm = File.Create(p_strPath); objFileStrm.Close(); // Write content to excel file File.WriteAllBytes(p_strPath, excel.GetAsByteArray()); Console.ReadKey(); } } |
Output:
In this program, we are taking static values for the Articles data but in real-time, we can use database call and foreach loop for iteration of each record.
Recommended Posts:
- Range Structure in C# 8.0
- Basics Operations of File and Directory in C#
- Check if the given ranges are equal or not in C#
- How to Create a Range to a Specified End in C#?
- How to Create a Range From a Specified Start in C#?
- Getting the Hash Code of the Specified Range in C#
- Range Constructor in C#
- Finding the Start Index of the Specified Range in C#
- Finding the End Index of the Specified Range in C#
- Finding all the Elements of a Range from Start to End in C#
- 5 Best Programming Languages For Newbies
- Index Struct in C# 8.0
- Finding the Index which Points Beyond the Last Element in C#
- Checking the Given Indexes are Equal or not in C#
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



