2011-08-24 81 views
14

有很多的例子在線如何填補從文本文件數據集,但我想要做的相反。我唯一能找到的是this,但似乎...不完整?導出一個C#的DataSet到文本文件

我希望它是可讀格式,不只是逗號分隔,每行的列之間,非等間距如果是有道理的。這裏是我的意思的例子:

Column1   Column2   Column3 
Some info  Some more info Even more info 
Some stuff here Some more stuff Even more stuff 
Bits    and    bobs 

注:我只有我的數據集左右的時間內一個數據表沒有必要擔心多個數據表。

編輯:當我說:「讀」我的意思是人類可讀。

在此先感謝。

+0

你知道它只會排列固定的字體,對吧? –

+0

是的,我知道。 – bobble14988

回答

15

這將大大地隔開固定長度字體的文本,而是意味着它會處理的全部數據表兩次(通過1:發現每列最長文本,通過2:輸出文本):

static void Write(DataTable dt, string outputFilePath) 
    { 
     int[] maxLengths = new int[dt.Columns.Count]; 

     for (int i = 0; i < dt.Columns.Count; i++) 
     { 
      maxLengths[i] = dt.Columns[i].ColumnName.Length; 

      foreach (DataRow row in dt.Rows) 
      { 
       if (!row.IsNull(i)) 
       { 
        int length = row[i].ToString().Length; 

        if (length > maxLengths[i]) 
        { 
         maxLengths[i] = length; 
        } 
       } 
      } 
     } 

     using (StreamWriter sw = new StreamWriter(outputFilePath, false)) 
     { 
      for (int i = 0; i < dt.Columns.Count; i++) 
      { 
       sw.Write(dt.Columns[i].ColumnName.PadRight(maxLengths[i] + 2)); 
      } 

      sw.WriteLine(); 

      foreach (DataRow row in dt.Rows) 
      { 
       for (int i = 0; i < dt.Columns.Count; i++) 
       { 
        if (!row.IsNull(i)) 
        { 
         sw.Write(row[i].ToString().PadRight(maxLengths[i] + 2)); 
        } 
        else 
        { 
         sw.Write(new string(' ', maxLengths[i] + 2)); 
        } 
       } 

       sw.WriteLine(); 
      } 

      sw.Close(); 
     } 
    } 
+0

工程就像一個魅力。我不介意它在數據集中迭代兩次,因爲它只是很小。考慮此解決方案的其他人如果使用大型數據集,可能需要考慮替代方案。謝謝,羅伊! – bobble14988

+0

它是如何在數據集中形成多個數據表的 –

+0

@SwethaVijayan傳入DataSet(例如'ds')作爲參數而不是DataTable,然後在行的上方插入'foreach(DataTable dt in ds.Tables)'行這說'foreach(DataRow在dt.Rows中行)' –

8

您將數據導出到XML格式是更好的。

數據集對此項工作的方法:

DataSet ds = new DataSet(); 
ds.WriteXml(fileName); 

可以讀取XML和填補像下面的數據集的數據:

DataSet ds = new DataSet(); 
ds.ReadXml(fileName); 
0

只是遍歷數據表中的行,並添加行到您的文件,如提供的示例

foreach (DataRow row in dt.Rows)    
{  

    object[] array = row.ItemArray;       
    for (i = 0; i < array.Length - 1; i++)     
    {        
    sw.Write(array[i].ToString() + ";");      
    }  

    sw.Write(array[i].ToString()); 

    sw.WriteLine(); 

} 

其中sw是一個StreamWrit呃提交你要保存數據的地方。問題實際在哪裏?

6

什麼,我會做的是:

foreach (DataRow row in myDataSet.Tables[0].Rows) 
{ 
    foreach (object item in row.ItemArray) 
    { 
     myStreamWriter.Write((string)item + "\t"); 
    } 
    myStreamWriter.WriteLine(); 
} 

也許循環之前,如果你想的頭添加列名。我認爲,雖然相當簡單,但應該做到這一點。

2

怎麼樣的下面?

public static void Write(DataTable dt, string filePath) 
     { 
      int i = 0; 
      StreamWriter sw = null; 
       sw = new StreamWriter(filePath, false); 
       for (i = 0; i < dt.Columns.Count - 1; i++) 
       { 
        sw.Write(dt.Columns[i].ColumnName + " "); 
       } 
       sw.Write(dt.Columns[i].ColumnName); 
       sw.WriteLine(); 
       foreach (DataRow row in dt.Rows) 
       { 
        object[] array = row.ItemArray; 
        for (i = 0; i < array.Length - 1; i++) 
        { 
         sw.Write(array[i] + " "); 
        } 
        sw.Write(array[i].ToString()); 
        sw.WriteLine(); 
       } 
       sw.Close(); 
     } 
0

我是軟件行業的初學者,只是想幫助你。這可能不是您的問題的最終解決方案。

我也看到了你mensioned的鏈接。它的確是唯一以逗號分隔的格式輸出。如果你想出口像你一樣,你必須採取一些額外的努力。 LINQ將大大減少您的工作。你需要做的是:1)計算數據表中每列的最大寬度(使用LINQ,這可以在一個語句本身完成,否則你必須幫助foreach)。 2)在將實際單元格值轉換爲文本文件之前,使用String.Format根據第1步中的計算寬度進行定位。

List<int> ColumnLengths = new List<int>(); 

     ds.Tables["TableName"].Columns.Cast<DataColumn>().All((x) => 
     { 
      { 
       ColumnLengths.Add(ds.Tables["TableName"].AsEnumerable().Select(y => y.Field<string>(x).Length).Max()); 
      } 
      return true; 
     }); 


     foreach (DataRow row in ds.Tables["TableName"].Rows) 
     { 
      for(int col=0;col<ds.Tables["TableName"].Columns.Count;col++) 
      { 
       SW.Write(row.Field<string>(ds.Tables["TableName"].Columns[col]).PadLeft(ColumnLengths[col] + (4 - (ColumnLengths[col] % 4)))); 
      } 
      SW.WriteLine(); 
     } 

希望你會覺得這有幫助。