This project has moved and is read-only. For the latest updates, please go here.

InsertTabel() method does't insert child class properties after base class properties

Jan 30, 2012 at 4:03 PM


I'm using insertTable(List<T>, true) to insert records,.. My List contains objects which inherit from another class. Everything works well but i did't want one thing:

When insertTable() generate tables it first insert child class properties (colomns) and then parent class properties(colomns), but i want the base class properties first, in excel table. 

Is there any way,???  that i can ensure the order of properties because its important.


Jan 30, 2012 at 4:21 PM

var orderedProperties = 
     from i in yourList
     select new {

Jan 30, 2012 at 5:23 PM
Edited Jan 30, 2012 at 5:23 PM

Thanks Mdeleon,

It's works.. 

but it's works for only one list however i have 4 or 5 lists, and i did't find a way to make it a generic function,,. because of its return type.


I am new to .net, can i get some idea about that ???


Many thanks

Jan 31, 2012 at 4:28 AM

I think you should create the queries to return the data you want in the order that you want. The alternative is to use reflection but that's probably overkill for what you're describing and it's also something you should get into after you're more familiar with the CLR.

Jan 31, 2012 at 2:38 PM

Here's a suggestion. You have data object T which has properties that are defined in a certain order, but you want the columns in your table displayed in a different order. Create another object class called Display_Of_T where you define the properties in the order you want them to be displayed. In this object class you have a constructor that takes object T and creates a new object Display_Of_T and copies the property values from T to the property values of Display_Of_T. The object class can also have a method that takes List<T> as the argument and returns List<Display_Of_T>. Now your application code does insertTable(List<Display_Of_T>, true) to insert the records into the worksheet. 

ClosedXML will now use the properties of Display_Of_T to define the column order. I still get a thrill whenever I do insertTable and there -- as if by magic -- is all my data with column headings and colors and everything else I might want. It's so great.

If you want to get fancy, you can define data attributes (also known as data annotations) to set your column heading names which can be different from the property name. See this link,

This kind of programming practice is known as "Separation of Concerns". The format in which the data is stored or constructed shouldn't dictate the format by which the data is viewed. In C# and other object oriented languages one of the ways to implement Separation of Concerns is to define separate objects for data storage and for data display along with defining the mappings between the objects. 

Have fun coding.