views:

200

answers:

2

My given problem is follow:

I have an object with x coloums and every coloum has y values. I must now bring this to excel.

I found an snippet in which a datatable can be exported easily.

So I will bring my object to a datatable. How can I do this?

Language is C#.´Net

A: 

You can use reflection to get the fields of the object and add the columns to the DataTable:

private bool IsNullableType(Type theType)
{
    return (theType.IsGenericType && theType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)));
}


// Create the columns based on the data in the album info - get by reflection
var ai = new <your object without data>;
Type t = ai.GetType();

this.dataTable.TableName = t.Name;

foreach (PropertyInfo p in t.GetProperties())
{
    var columnSpec = new DataColumn();
    // If nullable get the underlying type
    Type propertyType = p.PropertyType;
    if (IsNullableType(propertyType))
    {
        var nc = new NullableConverter(propertyType);
        propertyType = nc.UnderlyingType;
    }
    columnSpec.DataType = propertyType;
    columnSpec.ColumnName = p.Name;
    this.dataTable.Columns.Add(columnSpec);
}

this.dataGridView.DataSource = dataTable;

Then to add a row to the table:

var info = new <your object with data>;
// Add by reflection
Type t = info.GetType();
var row = new object[t.GetProperties().Length];

int index = 0;
foreach (PropertyInfo p in t.GetProperties())
{
    row[index++] = p.GetValue(info, null);
}

this.dataTable.Rows.Add(row);
ChrisF
+2  A: 

I'm not completely certain I know what you're trying to do. I assume you want to create a DataTable and load your existing object into it. Assuming your class looks something like this:

public class MyClass {
    public int ID {get;set;}
    public string Column1 {get;set;}
    public DateTime Column2 {get;set;}
    // ...
}

and assuming you have a list of them you want to copy into a DataTable, here's how:

DataTable dt = new DataTable("MyTable");
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Column1", typeof(string));
dt.Columns.Add("Column2", typeof(DateTime));

foreach (var o in _myObjectList) {
    DataRow dr = dt.NewRow();
    dr["ID"] = o.ID;
    dr["Column1"] = o.Column1;
    dr["Column2"] = o.Column2;
    dt.Rows.Add(dr);
}
John Saunders
That is a good way, but also exactly my problem:I don't know the coloums at the beginning.I have x objects.And each object has y values.So e.g. sometimes i have 2 coloums with 500 and 1000 values, sometimes I have 5 coloums in which each can have his own value count.
Kovu
Is this a one off export or a set spreadsheet which is diverse?Instead of addressing by dr["column1"] you can address by dr[0] and you can also foreach the columns that are available. if you have patterns you can exploit in the excel I suggest you do so.
Spence
If you want your problem solved, you'll have to describe it. Edit your question with this information and any other information necessary to understand your problem.
John Saunders