I have a task that I am sure can be easily generalized in a simple way.
I have different tables to be exported to excel dynamically, and what I am doing is creating the table columns in HTML from the Object parameters, and the rows using a foreach on the List to create the rows.
So for example if I have a list of Customer (List<Customer> )
I create the tr basing on the amount and name of the Object properties:
public class Customer
{
public int DeliveryAddressId { get; set; }
public DateTime CreatedDate { get; set; }
public string Firstname { get; set; }
public string Lastname { get; set; }
....
}
var output = "<table>";
output += "<thead>";
output += "<tr>";
output += "<th>DeliveryAddressId </th>";
.....
and the foreach to fill in the rows:
foreach (var customer in List<Customer>)
{
output += "<tr>";
output += "<td>" + customer.DeliveryAddressId + "</td>";
.....
But then I have different objects with different parameters, so the question is:
How can I create a generic method that takes a List<Object> as input and create this kind of table using the object parameter names as columns name and then cycles the rows accordingly?