Vous êtes sur la page 1sur 4

Saving, Reading, Uploading, Downloading Mechanism of CSV File and Text File using c# Asp.

net
In this article we are going to play with the CSV file and Txt file. Here we are going to see the different mechanism 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 the each line of code, I have explained some textual info to understand code. So please read the comments, so it helps to understand the code step by step.

Used Namespace:
using System; using System.IO; using System.Text;

Example 1: How to Convert CSV File to String in c# asp.net?

/// <summary> /// This is used to download CSV File. /// </summary> public void DownloadCSV() { string csvPath = "D:\\CSVFile.csv"; //Download and read all Text 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 String to CSV File c# asp.net?

/// <summary> /// This is used to upload text content to 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 CSV File using c# asp.net?

/// <summary> /// This is used to Save a content as 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 exisitng 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 Text within the uploaded Text file. string csvContentStr = File.ReadAllText(csvPath); //This saves content as CSV File. this.SaveCSVFile("CSVFileName", csvContentStr); }

Example 4 : Download Mechanism of CSV File using asp.net C#?

/// <summary> /// This is used to 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 Text File to CSV File using asp.net C#?

/// <summary> /// This 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 exisitng 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 CSV File by Custom String using c# asp.net?

/// <summary> /// Forming csvformat 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 manupulation with the CSV and Text File. Thanks for Reading this Article. Wish you all the best.

Vous aimerez peut-être aussi