Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think First you convert your list to datatable by using below code</p> <pre><code>public static DataTable ToDataTable&lt;T&gt;(List&lt;T&gt; l_oItems) { DataTable oReturn = new DataTable(typeof(T).Name); object[] a_oValues; int i; //#### Collect the a_oProperties for the passed T PropertyInfo[] a_oProperties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); //#### Traverse each oProperty, .Add'ing each .Name/.BaseType into our oReturn value //#### NOTE: The call to .BaseType is required as DataTables/DataSets do not support nullable types, so it's non-nullable counterpart Type is required in the .Column definition foreach (PropertyInfo oProperty in a_oProperties) { oReturn.Columns.Add(oProperty.Name, BaseType(oProperty.PropertyType)); } //#### Traverse the l_oItems foreach (T oItem in l_oItems) { //#### Collect the a_oValues for this loop a_oValues = new object[a_oProperties.Length]; //#### Traverse the a_oProperties, populating each a_oValues as we go for (i = 0; i &lt; a_oProperties.Length; i++) { a_oValues[i] = a_oProperties[i].GetValue(oItem, null); } //#### .Add the .Row that represents the current a_oValues into our oReturn value oReturn.Rows.Add(a_oValues); } //#### Return the above determined oReturn value to the caller return oReturn; } private static Type BaseType(Type oType) { //#### If the passed oType is valid, .IsValueType and is logicially nullable, .Get(its)UnderlyingType if (oType != null &amp;&amp; oType.IsValueType &amp;&amp; oType.IsGenericType &amp;&amp; oType.GetGenericTypeDefinition() == typeof(Nullable&lt;&gt;) ) { return Nullable.GetUnderlyingType(oType); } //#### Else the passed oType was null or was not logicially nullable, so simply return the passed oType else { return oType; } } </code></pre> <p>Then Use below code for export datatable to csv..</p> <pre><code>public static string ToCSV(DataTable dataTable) { //create the stringbuilder that would hold the data StringBuilder sb = new StringBuilder(); //check if there are columns in the datatable if (dataTable.Columns.Count != 0) { //loop thru each of the columns for headers foreach (DataColumn column in dataTable.Columns) { //append the column name followed by the separator sb.Append(column.ColumnName + ","); } //append a carriage return sb.Append("\r\n"); //loop thru each row of the datatable foreach (DataRow row in dataTable.Rows) { //loop thru each column in the datatable foreach (DataColumn column in dataTable.Columns) { //get the value for the row on the specified column // and append the separator sb.Append("\"" + row[column].ToString() + "\"" + ","); } //append a carriage return sb.Append("\r\n"); } } return (sb.ToString()); } </code></pre> <p>Main Function is : This</p> <pre><code>DataTable dt = GeneralFunctions.ToDataTable(HereputyourlistforcovertToDataTable); if (dt != null) { if (dt.Rows.Count &gt; 0) { Response.Clear(); Response.ClearHeaders(); Response.ClearContent(); Response.ContentType = "text/CSV"; Response.AddHeader("content-disposition", "attachment; filename=ReceivingLog.csv"); Response.Write(ToCSV(dt)); Response.End(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload