There are many ways to generate Excel files in C# using a third party library but I am going to show you how to export Excel files from your entity without using any third party, with the help of Interop.
For reference we are going to use Microsoft.Office.Interop.Excel package.
In this demo, I have created a C# console app and to separate Excel Export Operation I have created another class called excelexport.cs.
In this demo, I have created a C# console app and to separate Excel Export Operation I have created another class called excelexport.cs.

To work on, I need a model class and a list of objects to that model. So I have created a Model folder and inside that folder I have created a class called UserManager.cs
- public class UserManager
- {
- public string Id { get; set; }
- public string Username { get; set; }
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public string Password { get; set; }
- public DateTime CreatedOn { get; set; }
- }
But I need to generate a list of objects for this class as I am not using any database for this. So I have created a seed method to generate a set of dummy datas.
- public static List<UserManager> SeedData()
- {
- return new List<UserManager>
- {
- new UserManager
- {
- Id = Guid.NewGuid().ToString(),
- FirstName = "Test 1",
- LastName = "Test 1",
- Username = "TestUsername1",
- Password = "123",
- CreatedOn = DateTime.UtcNow
- },
- new UserManager
- {
- Id = Guid.NewGuid().ToString(),
- FirstName = "Test 2",
- LastName = "Test 2",
- Username = "TestUsername2",
- Password = "123",
- CreatedOn = DateTime.UtcNow
- },
- new UserManager
- {
- Id = Guid.NewGuid().ToString(),
- FirstName = "Test 3",
- LastName = "Test 3",
- Username = "TestUsername3",
- Password = "123",
- CreatedOn = DateTime.UtcNow
- },new UserManager
- {
- Id = Guid.NewGuid().ToString(),
- FirstName = "Test 4",
- LastName = "Test 4",
- Username = "TestUsername4",
- Password = "123",
- CreatedOn = DateTime.UtcNow
- }
- };
- }
So for now my UserManager class looks like this.

Our data is ready and now we need to make our Datatable converter to convert our list to a data table instance.
We can also gather some knowledge about Interop and how to access all the objects. interop and Reflection can help you, I think.
I am going to make this as a generic but you can use strongly typed as your need. (Also read all the comments in the code, I have tried to write what’s happening on each line).
- // T is a generic class
- static DataTable ConvertToDataTable<T>(List<T> models)
- {
- // creating a data table instance and typed it as our incoming model
- // as I make it generic, if you want, you can make it the model typed you want.
- DataTable dataTable = new DataTable(typeof(T).Name);
- //Get all the properties of that model
- PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
- // Loop through all the properties
- // Adding Column name to our datatable
- foreach (PropertyInfo prop in Props)
- {
- //Setting column names as Property names
- dataTable.Columns.Add(prop.Name);
- }
- // Adding Row and its value to our dataTable
- foreach (T item in models)
- {
- var values = new object[Props.Length];
- for (int i = 0; i < Props.Length; i++)
- {
- //inserting property values to datatable rows
- values[i] = Props[i].GetValue(item, null);
- }
- // Finally add value to datatable
- dataTable.Rows.Add(values);
- }
- return dataTable;
- }





Hüsnü AKÇAKPosted Sep 3, 2021, 9:13 AM
I have used it, thank you
Anthony LevanoPosted May 18, 2021, 12:50 AM
Thanks for your help, great work!
Sourav Kumar DasPosted Nov 23, 2019, 12:47 AM
Nice Article.
Liam FlinkPosted Nov 22, 2019, 10:42 AM
I have an easy solution for you. you can use ZetExcel. I hope you will be benefited for this tools. just try it and get the result.