Write data to CSV using .NET

here’s a sample code in C# using the .NET framework to write data to a CSV file

using System;
using System.IO;

namespace CSVWriterExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Define the CSV file path and name
            string filePath = @"C:\Users\UserName\Documents\example.csv";

            // Define the data to be written to the CSV file
            string[] data = {"John Smith", "30", "Male"};

            // Open the CSV file for writing
            using (StreamWriter sw = new StreamWriter(filePath, true))
            {
                // Write the data to the CSV file
                sw.WriteLine(string.Join(",", data));
            }

            Console.WriteLine("Data has been written to the CSV file.");
            Console.ReadLine();
        }
    }
}

In this example, the StreamWriter class is used to write the data to the CSV file. The using statement ensures that the StreamWriter object is disposed of properly when it is no longer needed. The string.Join method is used to join the data elements into a single string separated by commas, which is the standard format for CSV files.

Pamai Tech
Turning ideas into Reality

Products

Office Add-in

Enterprise Solutions

Cloud Consulting

UI UX Design

Data Transformation

Services

FAQ's

Privacy Policy

Terms & Condition

Team

Contact Us

Company

About Us

Services

Features

Our Pricing

Latest News

© 2025 Pamai Tech