2013-04-16 16 views

回答

1

參考代碼重要組成部分:

public void ExportDataTabletoFile(DataTable datatable, string delimited, bool exportcolumnsheader, string file) 

{ 

    StreamWriter str = new StreamWriter(file, false, System.Text.Encoding.Default); 

    if (exportcolumnsheader) 

    { 

     string Columns = string.Empty; 

     foreach (DataColumn column in datatable.Columns) 

     { 

      Columns += column.ColumnName + delimited; 

     } 

     str.WriteLine(Columns.Remove(Columns.Length - 1, 1)); 

    } 

    foreach (DataRow datarow in datatable.Rows) 

    { 

     string row = string.Empty; 



     foreach (object items in datarow.ItemArray) 

     { 



      row += items.ToString() + delimited; 

     } 

     str.WriteLine(row.Remove(row.Length - 1, 1)); 



    } 

    str.Flush(); 

    str.Close(); 



} 

全球化志願服務青年鏈接:

http://www.c-sharpcorner.com/UploadFile/cd19b9/how-to-download-datatable-to-text-file-in-C-Sharp/

+0

謝謝回覆。它的權利,我有我的流中的所有值,但它不是通過用戶的文件。我認爲我在通過文件的格式錯誤請參閱格式 –

相關問題