Writing data into CSV file in C#

后端 未结 15 1311
清酒与你
清酒与你 2020-11-22 17:15

I am trying to write into a csv file row by row using C# language. Here is my function

string first = reader[0].ToString();
string second=image.         


        
15条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 17:55

    This is a simple tutorial on creating csv files using C# that you will be able to edit and expand on to fit your own needs.

    First you’ll need to create a new Visual Studio C# console application, there are steps to follow to do this.

    The example code will create a csv file called MyTest.csv in the location you specify. The contents of the file should be 3 named columns with text in the first 3 rows.

    https://tidbytez.com/2018/02/06/how-to-create-a-csv-file-with-c/

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;
    
    namespace CreateCsv
    {
        class Program
        {
            static void Main()
            {
                // Set the path and filename variable "path", filename being MyTest.csv in this example.
                // Change SomeGuy for your username.
                string path = @"C:\Users\SomeGuy\Desktop\MyTest.csv";
    
                // Set the variable "delimiter" to ", ".
                string delimiter = ", ";
    
                // This text is added only once to the file.
                if (!File.Exists(path))
                {
                    // Create a file to write to.
                    string createText = "Column 1 Name" + delimiter + "Column 2 Name" + delimiter + "Column 3 Name" + delimiter + Environment.NewLine;
                    File.WriteAllText(path, createText);
                }
    
                // This text is always added, making the file longer over time
                // if it is not deleted.
                string appendText = "This is text for Column 1" + delimiter + "This is text for Column 2" + delimiter + "This is text for Column 3" + delimiter + Environment.NewLine;
                File.AppendAllText(path, appendText);
    
                // Open the file to read from.
                string readText = File.ReadAllText(path);
                Console.WriteLine(readText);
            }
        }
    }
    

提交回复
热议问题