2011-08-24 83 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(); 
     } 

希望你会觉得这有帮助。