Write Rows from DataTable to Text File

前端 未结 6 1574
面向向阳花
面向向阳花 2021-02-14 11:31
public void GenerateDetailFile()
{
  if (!Directory.Exists(AppVars.IntegrationFilesLocation))
  {
    Directory.CreateDirectory(AppVars.IntegrationFilesLocation);
  }

          


        
相关标签:
6条回答
  • 2021-02-14 12:06

    You need to write the columns from each DataRow. Currently you are writing the DataRow object that is dataRow.ToString() hence you get string name "System.Data.DataRow" of dataRow in your file

    foreach(DataRow row in table.Rows)
    {
     foreach(DataColumn column in table.Columns)
     {
      sw.WriteLine(row[column]);
     }
    }
    
    0 讨论(0)
  • 2021-02-14 12:07

    There is no "natural" string representation for a DataRow. You need to write it out in whatever format you desire, i.e., comma-separated list of values, etc. You can enumerate the columns and print their values, for instance:

    foreach (DataRow row in table.Rows)
    {
        bool firstCol = true;
        foreach (DataColumn col in table.Columns)
        {
            if (!firstCol) sw.Write(", ");
            sw.Write(row[col].ToString());
            firstCol = false;
        }
        sw.WriteLine();
    }
    
    0 讨论(0)
  • 2021-02-14 12:08

    Try this:

    To write the DataTable rows to text files in the specific directory

                var dir = @"D:\New folder\log";  // folder location
    
                if (!Directory.Exists(dir))  // if it doesn't exist, create
                    Directory.CreateDirectory(dir);
    
                foreach (DataRow row in dt.Rows)
                {
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        result.Append(row[i].ToString());
                        result.Append(i == dt.Columns.Count - 1 ? "\n" : ",");
                    }
                    result.AppendLine();
                }
                string path = System.IO.Path.Combine(dir, "item.txt");
                StreamWriter objWriter = new StreamWriter(path, false);
                objWriter.WriteLine(result.ToString());
                objWriter.Close();
    
    0 讨论(0)
  • 2021-02-14 12:20

    When you try to print out a DataRow like that, it is calling Object.ToString(), which simply prints out the name of the type. What you want to do is something like:

    sw.WriteLine(String.Join(",", row.ItemArray));
    

    This will print a comma separated list of all of the items in the DataRow.

    0 讨论(0)
  • 2021-02-14 12:25

    Something like:

    sw.WriteLine(row["columnname"].ToString());
    

    would be more appropriate.

    0 讨论(0)
  • 2021-02-14 12:28

    The below code will let you to write text file each column separated by '|'

        foreach (DataRow row in dt.Rows)
          {  
             object[] array = row.ItemArray;
             for (int i = 0; i < array.Length - 1; i++)
             {
               swExtLogFile.Write(array[i].ToString() + " | ");
             }
              swExtLogFile.WriteLine(array[array.Length - 1].ToString());             
         }
    

    Reference link

    0 讨论(0)
提交回复
热议问题