Create Excel From List Object In C#

Hello, everyone! Hope you guys are doing well. Today, In this article, I will explain, How to create an Excel file from the object of the list in C#. So let’s start it. For this, I have installed the latest version of the ArrayToExcel NuGet package.

Code

using ArrayToExcel;
using System.Collections.Generic;

namespace ListToExcel
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Student> students = new List<Student>() {
                new Student(){ Id = 1, Name="Chand"},
                new Student(){ Id = 2, Name="Nishant"},
                new Student(){ Id = 3, Name="Shailja"},
                new Student(){ Id = 4, Name="Umang"},
                new Student(){ Id = 5, Name="Prince"},
                new Student(){ Id = 6, Name="Krishna"}
            };

            byte[] ExcelData = students.ToExcel();
            string Filename = @"D:\List_To_PDF.xls";
            System.IO.File.WriteAllBytes(Filename, ExcelData);
        }
        public class Student
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }
    }
}

 

Output

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories