0

在 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

4 回答 4

3

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

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

你需要换行的地方。

于 2012-11-19T15:14:40.853 回答
1

You can use Environment.NewLine to add new line to the end of each line in builder.

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);
于 2012-11-19T15:17:48.700 回答
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();
           }
       }
    }
}
于 2012-11-19T15:26:06.387 回答
0

And where are you see new lines in your string builder? Try this:

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);
}
于 2012-11-19T15:18:07.353 回答