jjccx

jjccx's blog
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

把DataTable输出到文本文件

Posted on 2008-04-16 11:35  jjccx  阅读(1204)  评论(1)    收藏  举报
把DataTable输出到文本文件,在文本文件里必须画出表格:

private void button1_Click(object sender, EventArgs e)
{
   DataTable source = GetDataSource();
   //source.Select(
   int cols = source.Columns.Count;
   int[] maxLength = new int[cols];
   for (int i = 0; i < cols; i++) maxLength[i] = 0;
 
   for (int i = 0; i < cols; i++)
   {
      byte[] b = Encoding.Default.GetBytes(Convert.ToString(source.Columns[i].ColumnName));
      if (maxLength[i] < b.Length)
      {
        maxLength[i] = b.Length;
      }
   }
 
   foreach (DataRow dr in source.Rows)
   {
      for (int i = 0; i < cols; i++)
      {
        byte[] b = Encoding.Default.GetBytes(Convert.ToString(dr[i]));
        if (maxLength[i] < b.Length)
        {
           maxLength[i] = b.Length;
        }
      }
   }
   for (int i = 0; i < cols; i++) maxLength[i] = (maxLength[i] / 2 + 1);
 
   StringBuilder sb = new StringBuilder();
   //标头:
   sb.Append("┌");
   for (int i = 0; i < cols; i++)
   {
      for (int j = 0; j < maxLength[i]; j++)
      {
        sb.Append("─");
      }
      if (i == cols - 1) sb.Append("┐");
      else sb.Append("┬");
   }
   sb.Append(Environment.NewLine);
   sb.Append("│");
   for (int i = 1; i <= cols; i++)
   {
      sb.Append(string.Format("{0,-" + Convert.ToString(maxLength[i - 1] * 2) + "}", Convert.ToString(source.Columns[i - 1].ColumnName)));
      sb.Append("│");
   }
   sb.Append(Environment.NewLine);
   sb.Append("├");
   for (int i = 1; i <= cols; i++)
   {
      sb.Append(new string('─', maxLength[i - 1]));
      if (i == cols) sb.Append("┤");
      else sb.Append("┼");
   }
 
   for (int r = 0; r < source.Rows.Count; r++)
   {
      sb.Append(Environment.NewLine);
      sb.Append("│");
      for (int i = 1; i <= cols; i++)
      {
        string data = Convert.ToString(source.Rows[r][i - 1]);
        int length = Encoding.Default.GetBytes(data).Length;
        length = maxLength[i - 1] * 2 - (length - data.Length);
        sb.Append(string.Format("{0,-" + Convert.ToString(length) + "}", data));
        sb.Append("│");
      }
      sb.Append(Environment.NewLine);
 
      sb.Append(r == (source.Rows.Count - 1) ? "└" : "├");
      for (int i = 1; i <= cols; i++)
      {
        sb.Append(new string('─', maxLength[i - 1]));
        if (i == cols) sb.Append(r == (source.Rows.Count - 1) ? "┘" : "┤");
        else sb.Append(r == (source.Rows.Count - 1) ? "┴" : "┼");
      }
   }
 
   this.textBox1.Text = sb.ToString();
}
 
protected DataTable GetDataSource()
{
   string[,] employees = {
                   {"1", "张三", "不说了"},
                   {"2", "王小二", "抗日小英雄"},
                   {"3", "Spice Girl", "很好的组合,很好听的歌,Spice it!"},
                   {"4555555444444444445555555555555555555555", "jjccx 合唱团", "很好的组合,很好听的歌,JJJCCCXXX it!"}
                   };
 
   DataTable ret = new DataTable();
 
   ret.Columns.Add("ID");
   ret.Columns.Add("Name");
ret.Columns.Add("Descriptionxxxxxxxxxxxxxxxxxxx000000000000000000000000000000000000");
 
   int rowCount = employees.GetLength(0);
 
   for (int i = 0; i < rowCount; i++)
   {
      DataRow row = ret.NewRow();
      row["ID"] = employees[i, 0]; row["Name"] = employees[i, 1]; row["Descriptionxxxxxxxxxxxxxxxxxxx000000000000000000000000000000000000"] = employees[i, 2];
      ret.Rows.Add(row);
   }
 
   return ret;
}

┌─────────────────────┬───────┬──────────────────────────────────┐
ID                                        Name          Descriptionxxxxxxxxxxxxxxxxxxx000000000000000000000000000000000000 
├─────────────────────┼───────┼──────────────────────────────────┤
1                                         │张三          │不说了                                                              
├─────────────────────┼───────┼──────────────────────────────────┤
2                                         │王小二        │抗日小英雄                                                         
├─────────────────────┼───────┼──────────────────────────────────┤
3                                         Spice Girl    │很好的组合,很好听的歌,Spice it!                                  
├─────────────────────┼───────┼──────────────────────────────────┤
4555555444444444445555555555555555555555  jjccx 合唱团  │很好的组合,很好听的歌,JJJCCCXXX it!                              
└─────────────────────┴───────┴──────────────────────────────────┘
(在网页上表格可能显示不正确,要正确查看,把上面的格子拷到文本文件中)