2016-06-21 3 views
0

私はcsv行を返すメソッドを持っています。しかし、私はこれをデータテーブルに変換したい。意味は、どこにでもある "、"私は新しい列セルにデータを入れたい。CSVをDatatableに変換する

private static string ConvertGridViewRowToCsvRow(GridViewRow row) 
{ 
     StringBuilder csvRow = new StringBuilder(); 
     bool firstCell = true; 

     foreach (DataControlFieldCell cell in row.Cells) 
     { 
      string text = string.Empty; 

      if (!String.IsNullOrEmpty(cell.Text)) 
      { 
       text = cell.Text; 
      } 
      else if (cell.Controls.Count > 0) 
      { 
       foreach (var control in cell.Controls) 
       { 
        if (control is ITextControl) 
        { 
         var textControl = control as ITextControl; 
         text += HttpUtility.HtmlDecode(textControl.Text).Replace("\r", string.Empty).Replace("\n", string.Empty).Trim(); 
        } 
        else if (control is TextImage) 
        { 
         var textImage = control as TextImage; 
         text += HttpUtility.HtmlDecode(textImage.Text).Replace("\r", string.Empty).Replace("\n", string.Empty).Trim(); 
        } 
       } 
      } 

      if (!firstCell) 
      { 
       csvRow.Append(","); 
      } 

      csvRow.Append(MakeTextCsvFriendly(text)); 
      firstCell = false; 
     } 

     return csvRow.ToString(); 
} 

これは私がcsvFile.Append(",");を交換するか、おそらく保存するために、このようなファイルを使用し1うの方法を考えていたConvertGridViewRowToCsvRow

public static void Export(string fileName, GridView gv, HashSet<string> selectedRows) 
{ 
     HttpContext.Current.Response.Clear(); 
     HttpContext.Current.Response.AddHeader(
      "content-disposition", string.Format("attachment; filename={0}", fileName)); 
     HttpContext.Current.Response.ContentType = "text/csv"; 

     //SetHiddenColumnsVisibility(gv); enable if you want only selected columns to visible on export (not current business requirement) 

     var csvFile = new StringBuilder(); 

     using (StringWriter sw = new StringWriter()) 
     { 
      using (HtmlTextWriter htw = new HtmlTextWriter(sw)) 
      { 
       if (gv.HeaderRow != null) 
       { 
        GridViewExportUtil.PrepareControlForExport(gv.HeaderRow); 
        bool firstCell = true; 

        foreach (DataControlFieldHeaderCell cell in gv.HeaderRow.Cells) 
        { 
         if (!firstCell) 
         { 
          csvFile.Append(","); 
         } 

         string text = cell.ContainingField.HeaderText; 
         csvFile.Append(MakeTextCsvFriendly(text)); 
         firstCell = false; 
        } 

        csvFile.Append("\r\n"); 
       } 

       if (selectedRows.Count > 0) 
       { 
        foreach (GridViewRow row in gv.Rows) 
        { 
         if (row.RowType == DataControlRowType.DataRow && selectedRows.Contains(gv.DataKeys[row.RowIndex].Value.ToString())) 
         { 
          GridViewExportUtil.PrepareControlForExport(row); 
          csvFile.AppendLine(ConvertGridViewRowToCsvRow(row)); 
         } 
        } 
       } 
       else 
       { 
        foreach (GridViewRow row in gv.Rows) 
        { 
         GridViewExportUtil.PrepareControlForExport(row); 
         csvFile.AppendLine(ConvertGridViewRowToCsvRow(row)); 
        } 
       } 

       // render the htmlwriter into the response 
       HttpContext.Current.Response.Write(csvFile.ToString()); 
       HttpContext.Current.Response.End(); 
      } 
     } 
    } 

を呼び出すボタンクリック時に呼び出されるメソッドですDataTableのデータ

しかし、私はこのメソッドを構造化する際の指針が必要です。

+0

は[FileHelpers](http://www.filehelpers.net/)それが仕事の罰金だ場合は、答えとしてそれをマークすることを忘れないでください、 –

答えて

-1

私はcsvファイルをWindowsアプリケーション用のdatatableに変換する以下のコードを試しています。

private void CSVtoDataTable(string filepath) 
{ 
    int count = 1;    
    char fieldSeparator = ','; 
    DataTable csvData = new DataTable(); 

    using (TextFieldParser csvReader = new TextFieldParser(filePath)) 
    {     
     csvReader.HasFieldsEnclosedInQuotes = true;     
     while (!csvReader.EndOfData) 
     { 
      csvReader.SetDelimiters(new string[] { "," }); 
      string[] fieldData = csvReader.ReadFields(); 
      if(count==0) 
      { 
       foreach (string column in fieldData) 
       { 
        DataColumn datecolumn = new DataColumn(column); 
        datecolumn.AllowDBNull = true; 
        csvData.Columns.Add(datecolumn); 
       } 
      } 
      else 
      { 
       csvData.Rows.Add(fieldData); 
      }     

     } 
    } 

} 
+0

を必要とするすべてです。 –

+0

あなたのコードはコンパイルされていないか、私のために働いていません。 "using Microsoft.VisualBasic.FileIO;"を含めた後"filePath"を "filepath"に変更すると、まだSystem.ArgumentExceptionがあります。入力配列がこのテーブルの列数よりも長い。 – Miryafa