StringBuilder以文件维护格式

时间:2012-11-19 15:08:11

标签: c# file format stringbuilder

在c#中,我该如何导出文件一个有多行的字符串构建器,保留这些行。这是我的代码:

StringBuilder builder = new StringBuilder();
using (SqlCommand command = new SqlCommand(consulta, SqlConn))
{
  using (SqlDataReader reader = command.ExecuteReader())
  {
    while (reader != null && reader.Read())
    {
      builder.AppendFormat("{0,-5}  {1,-10}  {2,-10} {3,-18} {4,-10}  {5,-2}  {6,-10} {7,-5} {8,-10}", Convert.ToString(reader[0]), reader.GetDateTime(1).ToString("yyyy-MM-dd"), reader.GetDateTime(2).ToString("yyyy-MM-dd"), Convert.ToString(reader[3]), Convert.ToString(reader[4]), "06", Convert.ToString(reader[5]), Convert.ToString(reader[6]), Convert.ToString(reader[7]));
    }
  }
}

这里是我保存文件的地方,但它并不尊重这些行。

File.WriteAllText(saveFileDialog1.FileName, builder.ToString());

4 个答案:

答案 0 :(得分:3)

您需要使用AppendLine方法插入换行符:

builder.AppendFormat(...);
builder.AppendLine();
builder.AppendFormat(...);

你需要一个换行符。

答案 1 :(得分:1)

您可以使用Environment.NewLine将新行添加到构建器中每行的末尾。

builder.AppendFormat("{0,-5}  {1,-10}  {2,-10} {3,-18} {4,-10}  {5,-2}  {6,-10} {7,-5} {8,-10}{9}", Convert.ToString(reader[0]), reader.GetDateTime(1).ToString("yyyy-MM-dd"), reader.GetDateTime(2).ToString("yyyy-MM-dd"), Convert.ToString(reader[3]), Convert.ToString(reader[4]), "06", Convert.ToString(reader[5]), Convert.ToString(reader[6]), Convert.ToString(reader[7]), Environment.NewLine);

答案 2 :(得分:1)

如果更高效和更简单地执行此操作,这将避免不必要地使用StringBuilder以及为中间String分配内存的开销。

我还添加了额外的AppendLine来用新行划分你的行。

using (var writer = new StreamWriter(saveFileDialog1.FileName))
{
    using (SqlCommand command = new SqlCommand(consulta, SqlConn))
    {
        using (SqlDataReader reader = command.ExecuteReader())
        {
            while (reader != null && reader.Read())
            {
                writer.AppendFormat(
"{0,-5}  {1,-10}  {2,-10} {3,-18} {4,-10}  {5,-2}  {6,-10} {7,-5} {8,-10}", 
                    Convert.ToString(reader[0]),
                    reader.GetDateTime(1).ToString("yyyy-MM-dd"),
                    reader.GetDateTime(2).ToString("yyyy-MM-dd"),
                    Convert.ToString(reader[3]),
                    Convert.ToString(reader[4]), 
                    "06",
                    Convert.ToString(reader[5]),
                    Convert.ToString(reader[6]),
                    Convert.ToString(reader[7]));
                writer.AppendLine();
           }
       }
    }
}

我还建议你可以提高你这样的代码的可读性,虽然这可以被认为是主观的,

using (var writer = new StreamWriter(saveFileDialog1.FileName))
{
    using (SqlCommand command = new SqlCommand(consulta, SqlConn))
    {
        using (SqlDataReader reader = command.ExecuteReader())
        {
            while (reader != null && reader.Read())
            {
                writer.AppendFormat("{0, -7}", reader[0]);
                writer.AppendFormat("{0:yyyy-MM-dd, -12}", reader.GetDateTime(1));
                writer.AppendFormat("{0:yyyy-MM-dd, -11}", reader.GetDateTime(2));
                writer.AppendFormat("{0, -19}", reader.[3]);
                writer.AppendFormat("{0, -12}06", reader.[4]);
                writer.AppendFormat("{0, -11}, reader.[5]);
                writer.AppendFormat("{0, -6}, reader.[6]);
                writer.AppendFormat("{0, -10}, reader.[7]);
                writer.AppendLine();
           }
       }
    }
}

答案 3 :(得分:0)

你在哪里看到字符串生成器中的新行?试试这个:

while (reader != null && reader.Read())
{
     builder.AppendFormat("{0,-5}  {1,-10}  {2,-10} {3,-18} {4,-10}  {5,-2}  {6,-10} {7,-5} {8,-10}{9}", Convert.ToString(reader[0]), reader.GetDateTime(1).ToString("yyyy-MM-dd"), reader.GetDateTime(2).ToString("yyyy-MM-dd"), Convert.ToString(reader[3]), Convert.ToString(reader[4]), "06", Convert.ToString(reader[5]), Convert.ToString(reader[6]), Convert.ToString(reader[7]), Environment.NewLine);
}