テーブルのリストをコンマ区切り値(CSV)にエクスポートしようとしていますが、このコードから出力される正しい出力が得られないのは、htmlテーブルの形式です。ここ は、私が最初にあなたが次にCSVファイルへのエクスポートデータテーブル用のコードの下に使用したコードテーブルのリストをCSVファイルにエクスポート
public static DataTable ToDataTable<T>(List<T> 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 < 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 && oType.IsValueType &&
oType.IsGenericType && oType.GetGenericTypeDefinition() == typeof(Nullable<>)
)
{
return Nullable.GetUnderlyingType(oType);
}
//#### Else the passed oType was null or was not logicially nullable, so simply return the passed oType
else
{
return oType;
}
}
の下に使用してデータテーブルにあなたのリストを変換すると思います....私を助けて、 おかげ
public void ExportToCsv()
{
DataClassesDataContext db = new DataClassesDataContext();
var employee = db.Employees.ToList();
var grid = new System.Web.UI.WebControls.GridView();
grid.DataSource = employee;
grid.DataBind();
StringBuilder strbldr = new StringBuilder();
for (int i = 0; i < grid.Columns.Count; i++)
{
strbldr.Append(grid.Columns[i].HeaderText + ',');
}
for (int j = 0; j < grid.Rows.Count; j++)
{
for (int k = 0; k < grid.Columns.Count; k++)
{
//separating gridview columns with comma
strbldr.Append(grid.Rows[j].Cells[k].Text + ',');
}
//appending new line for gridview rows
strbldr.Append("\n");
}
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=YourFileName.csv");
Response.ContentType = "text/csv";
StringWriter sa = new StringWriter(strbldr);
HtmlTextWriter ht = new HtmlTextWriter(sa);
grid.RenderControl(ht);
Response.Write(sa.ToString());
Response.End();
}
出力がこの形態である(
http://stackoverflow.com/questions/4959722/c-sharp-datatable-toこれをチェック-csv – BizApps
私はCSVにデータをエクスポートする必要があり、この驚くべきライブラリは私にとってはhttps://github.com/JoshClose/CsvHelper/wikiでした。あなたはNuGetからでもそれを得ることができます! – Intrigue