2011-12-16 18 views
2

テーブルのリストをコンマ区切り値(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(); 
    } 
+0

出力がこの形態である(​​1​​vikko​​1​​1988年10月11日) –

+1

http://stackoverflow.com/questions/4959722/c-sharp-datatable-toこれをチェック-csv – BizApps

+0

私はCSVにデータをエクスポートする必要があり、この驚くべきライブラリは私にとってはhttps://github.com/JoshClose/CsvHelper/wikiでした。あなたはNuGetからでもそれを得ることができます! – Intrigue

答えて

0

私のコードです..

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()); 
    } 

主な機能は次のとおりです。この

DataTable dt = GeneralFunctions.ToDataTable(HereputyourlistforcovertToDataTable); 
       if (dt != null) 
       { 
        if (dt.Rows.Count > 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(); 
        } 
       } 
+0

を確認し、何か問題を見つけたらお知らせください!!!! – sikender

+0

感謝sikender私は試みたが、これは動作していません。私のコードの問題では、grid.Columns.Countは常にゼロです –

+0

私はすでにすべてのコードを提供します。 – sikender

関連する問題