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
  1. using System;  
  2. using System.IO;  
  3. using System.Text; 
Example 1: How to convert a CSV file to a string in C# ASP.Net?
  1. /// <summary>  
  2. /// This is used to download CSV File.  
  3. /// </summary>  
  4. public void DownloadCSV() {  
  5.   string csvPath = "D:\\CSVFile.csv";  
  6.   
  7.   //Download and read all Texts within the uploaded Text file.  
  8.   string csvContentStr = File.ReadAllText(csvPath);  
  9.   
  10.   //Here you can see the output as CSV content.  
  11.   Response.Write(csvContentStr);  

Example 2: How to Convert a string to a CSV file in C# ASP.Net?
  1. /// <summary>  
  2. /// This is used to upload text content to CSV file.  
  3. /// </summary>  
  4. public void UploadCSV() {  
  5.   StringBuilder csvContent = new StringBuilder();  
  6.   
  7.   // Adding Header Or Column in the First Row of CSV  
  8.   csvContent.AppendLine("First Name,Last Name");  
  9.   csvContent.AppendLine("Lajapathy,Arun");  
  10.   
  11.   string csvPath = "D:\\CSVFile.csv";  
  12.   
  13.   // Save or upload CSV format File (.csv)  
  14.   File.AppendAllText(csvPath, csvContent.ToString());  

Example 3: How to download a CSV file using C# ASP.Net?
  1. /// <summary>  
  2. /// This is used to Save a content as CSV file and Download it.  
  3. /// </summary>  
  4. public void SaveReadCSVFile() {  
  5.  StringBuilder csvContent = new StringBuilder();  
  6.   
  7.  // Adding Header Or Column in the First Row of CSV  
  8.  csvContent.AppendLine("First Name,Last Name");  
  9.  csvContent.AppendLine("Lajapathy,Arun");  
  10.  csvContent.AppendLine("Anand,Babu");  
  11.  csvContent.AppendLine("Sathiya,Seelan");  
  12.   
  13.  string csvPath = "D:\\CSVFile.csv";  
  14.   
  15.  //Here we delete the existing file to avoid duplicate records.  
  16.  if (File.Exists(csvPath)) {  
  17.    File.Delete(csvPath);  
  18.  }  
  19.   
  20.  // Save or upload CSV format File (.csv)  
  21.  File.AppendAllText(csvPath, csvContent.ToString());  
  22.   
  23.  //Download and read all Texts within the uploaded Text file.  
  24.  string csvContentStr = File.ReadAllText(csvPath);  
  25.   
  26.  //This saves content as CSV File.  
  27.  this.SaveCSVFile("CSVFileName", csvContentStr);  
  28.   

Example 4: What is the download mechanism for a CSV file using ASP.Net C#?
  1. /// <summary>  
  2. /// This is used to file as CSV.  
  3. /// </summary>  
  4. public void SaveCSVFile(string fileName, string csvContentStr) {  
  5.  try {  
  6.    fileName = fileName + "_" + String.Format("{0:MMMM}", DateTime.Today) + "_" + String.Format("{0:yyyy}", DateTime.Today);  
  7.   
  8.    Response.Clear();  
  9.    // This is content type for CSV.  
  10.    Response.ContentType = "Text/vnd.ms-excel";  
  11.    Response.AddHeader("Content-Disposition""attachment;filename=\"" + fileName + ".csv\"");  
  12.   
  13.    Response.Write(csvContentStr); //Here Content write in page.  
  14.  } finally {  
  15.    Response.End();  
  16.  }  

Example 5: How to convert a Text file to a CSV file using ASP.Net C#?
  1. /// <summary>  
  2. /// This is used to Save a content as Text file and Download it.  
  3. /// </summary>         
  4. public void ConvertTextFileToCSV() {  
  5.    StringBuilder csvContent = new StringBuilder();  
  6.    // Adding Header Or Column in the First Row of CSV  
  7.    csvContent.AppendLine("First Name,Last Name");  
  8.    csvContent.AppendLine("Lajapathy,Arun");  
  9.    csvContent.AppendLine("Anand,Babu");  
  10.    csvContent.AppendLine("Sathiya,Seelan");  
  11.   
  12.    string textPath = "D:\\CSVTextFile.txt";  
  13.   
  14.    //Here we delete the existing file to avoid duplicate records.  
  15.    if (File.Exists(textPath) {  
  16.          File.Delete(textPath);  
  17.        }  
  18.   
  19.        // Save or upload CSV format string to Text File (.txt)  
  20.        File.AppendAllText(textPath, csvContent.ToString());  
  21.   
  22.        //Download or read all Text within the uploaded Text file.  
  23.        string csvContentStr = File.ReadAllText(textPath);  
  24.   
  25.        //This saves content as CSV File.  
  26.        this.SaveCSVFile("CSVFileName", csvContentStr);  
  27.      } 
Example 6: How to download a CSV file by a custom string using C# ASP.Net?
  1. /// <summary>  
  2. /// Forming csvformat string and download it directly.  
  3. /// </summary>  
  4. public void DownloadCSVFile() {  
  5.  StringBuilder csvContent = new StringBuilder();  
  6.   
  7.  // Adding Header Or Column in the First Row of CSV  
  8.  csvContent.AppendLine("First Name,Last Name");  
  9.  csvContent.AppendLine("Lajapathy,Arun");  
  10.  csvContent.AppendLine("Anand,Babu");  
  11.  csvContent.AppendLine("Sathiya,Seelan");  
  12.   
  13.  //This saves content as CSV File.  
  14.  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.