Operations of CSV Files and Text Files Using C# ASP.NET

Introduction

In this article, we are going to play with CSV and Text files. Here we are going to see various mechanisms like Downloading, Uploading, Saving, Reading, and Converting.

Things you learn here

  1. Download and upload mechanism.
  2. Convert CSV to Text and Text to CSV file.

Note. Above each line of code, I have provided some textual information to explain the code. So please read the comments, since they help to understand the code step by step.

Used Namespace

using System;
using System.IO;
using System.Text;

Example 1. How to convert a CSV file to a string in C# ASP.Net?

/// <summary>
/// This method is used to download a CSV file.
/// </summary>
public void DownloadCSV()
{
    string csvPath = "D:\\CSVFile.csv";

    // Download and read all texts within the uploaded text file.
    string csvContentStr = File.ReadAllText(csvPath);

    // Here you can see the output as CSV content.
    Response.Write(csvContentStr);
}

Example 2. How to Convert a string to a CSV file in C# ASP.Net?

/// <summary>
/// This method is used to upload text content to a CSV file.
/// </summary>
public void UploadCSV()
{
    StringBuilder csvContent = new StringBuilder();

    // Adding Header Or Column in the First Row of CSV
    csvContent.AppendLine("First Name,Last Name");
    csvContent.AppendLine("Lajapathy,Arun");

    string csvPath = "D:\\CSVFile.csv";

    // Save or upload CSV format File (.csv)
    File.AppendAllText(csvPath, csvContent.ToString());
}

Example 3. How to download a CSV file using C# ASP.Net?

/// <summary>
/// This method is used to save content as a CSV file and download it.
/// </summary>
public void SaveReadCSVFile()
{
    StringBuilder csvContent = new StringBuilder();

    // Adding Header Or Column in the First Row of CSV
    csvContent.AppendLine("First Name,Last Name");
    csvContent.AppendLine("Lajapathy,Arun");
    csvContent.AppendLine("Anand,Babu");
    csvContent.AppendLine("Sathiya,Seelan");

    string csvPath = "D:\\CSVFile.csv";

    // Here we delete the existing file to avoid duplicate records.
    if (File.Exists(csvPath))
    {
        File.Delete(csvPath);
    }

    // Save or upload CSV format File (.csv)
    File.AppendAllText(csvPath, csvContent.ToString());

    // Download and read all Texts within the uploaded Text file.
    string csvContentStr = File.ReadAllText(csvPath);

    // This saves content as CSV File.
    this.SaveCSVFile("CSVFileName", csvContentStr);
}

Example 4. What is the download mechanism for a CSV file using ASP.Net C#?

/// <summary>
/// This method is used to save a file as CSV.
/// </summary>
public void SaveCSVFile(string fileName, string csvContentStr)
{
    try
    {
        fileName = fileName + "_" + String.Format("{0:MMMM}", DateTime.Today) + "_" + String.Format("{0:yyyy}", DateTime.Today);

        Response.Clear();
        // This is content type for CSV.
        Response.ContentType = "Text/vnd.ms-excel";
        Response.AddHeader("Content-Disposition", "attachment;filename=\"" + fileName + ".csv\"");

        Response.Write(csvContentStr); // Here Content write in page.
    }
    finally
    {
        Response.End();
    }
}

Example 5. How to convert a Text file to a CSV file using ASP.Net C#?

/// <summary>
/// This method is used to save a content as Text file and Download it.
/// </summary>
public void ConvertTextFileToCSV()
{
    StringBuilder csvContent = new StringBuilder();
    // Adding Header Or Column in the First Row of CSV
    csvContent.AppendLine("First Name,Last Name");
    csvContent.AppendLine("Lajapathy,Arun");
    csvContent.AppendLine("Anand,Babu");
    csvContent.AppendLine("Sathiya,Seelan");

    string textPath = "D:\\CSVTextFile.txt";

    // Here we delete the existing file to avoid duplicate records.
    if (File.Exists(textPath))
    {
        File.Delete(textPath);
    }

    // Save or upload CSV format string to Text File (.txt)
    File.AppendAllText(textPath, csvContent.ToString());

    // Download or read all Text within the uploaded Text file.
    string csvContentStr = File.ReadAllText(textPath);

    // This saves content as CSV File.
    this.SaveCSVFile("CSVFileName", csvContentStr);
}

Example 6. How to download a CSV file by a custom string using C# ASP.Net?

/// <summary>
/// Forming csv format string and download it directly.
/// </summary>
public void DownloadCSVFile()
{
    StringBuilder csvContent = new StringBuilder();

    // Adding Header Or Column in the First Row of CSV
    csvContent.AppendLine("First Name,Last Name");
    csvContent.AppendLine("Lajapathy,Arun");
    csvContent.AppendLine("Anand,Babu");
    csvContent.AppendLine("Sathiya,Seelan");

    // This saves content as CSV File.
    this.SaveCSVFile("CSVFileName", csvContent.ToString());
}

Summary

Thus we have seen the complete manipulation of CSV and Text files. Thanks for reading this article. Wish you all the best.