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
- Download and upload mechanism.
- 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?
-
-
-
- public void DownloadCSV() {
- string csvPath = "D:\\CSVFile.csv";
-
-
- string csvContentStr = File.ReadAllText(csvPath);
-
-
- Response.Write(csvContentStr);
- }
Example 2: How to Convert a string to a CSV file in C# ASP.Net?
-
-
-
- public void UploadCSV() {
- StringBuilder csvContent = new StringBuilder();
-
-
- csvContent.AppendLine("First Name,Last Name");
- csvContent.AppendLine("Lajapathy,Arun");
-
- string csvPath = "D:\\CSVFile.csv";
-
-
- File.AppendAllText(csvPath, csvContent.ToString());
- }
Example 3: How to download a CSV file using C# ASP.Net?
-
-
-
- public void SaveReadCSVFile() {
- StringBuilder csvContent = new StringBuilder();
-
-
- csvContent.AppendLine("First Name,Last Name");
- csvContent.AppendLine("Lajapathy,Arun");
- csvContent.AppendLine("Anand,Babu");
- csvContent.AppendLine("Sathiya,Seelan");
-
- string csvPath = "D:\\CSVFile.csv";
-
-
- if (File.Exists(csvPath)) {
- File.Delete(csvPath);
- }
-
-
- File.AppendAllText(csvPath, csvContent.ToString());
-
-
- string csvContentStr = File.ReadAllText(csvPath);
-
-
- this.SaveCSVFile("CSVFileName", csvContentStr);
-
- }
Example 4: What is the download mechanism for a CSV file using ASP.Net C#?
-
-
-
- public void SaveCSVFile(string fileName, string csvContentStr) {
- try {
- fileName = fileName + "_" + String.Format("{0:MMMM}", DateTime.Today) + "_" + String.Format("{0:yyyy}", DateTime.Today);
-
- Response.Clear();
-
- Response.ContentType = "Text/vnd.ms-excel";
- Response.AddHeader("Content-Disposition", "attachment;filename=\"" + fileName + ".csv\"");
-
- Response.Write(csvContentStr);
- } finally {
- Response.End();
- }
- }
Example 5: How to convert a Text file to a CSV file using ASP.Net C#?
-
-
-
- public void ConvertTextFileToCSV() {
- StringBuilder csvContent = new StringBuilder();
-
- csvContent.AppendLine("First Name,Last Name");
- csvContent.AppendLine("Lajapathy,Arun");
- csvContent.AppendLine("Anand,Babu");
- csvContent.AppendLine("Sathiya,Seelan");
-
- string textPath = "D:\\CSVTextFile.txt";
-
-
- if (File.Exists(textPath) {
- File.Delete(textPath);
- }
-
-
- File.AppendAllText(textPath, csvContent.ToString());
-
-
- string csvContentStr = File.ReadAllText(textPath);
-
-
- this.SaveCSVFile("CSVFileName", csvContentStr);
- }
Example 6: How to download a CSV file by a custom string using C# ASP.Net?
-
-
-
- public void DownloadCSVFile() {
- StringBuilder csvContent = new StringBuilder();
-
-
- csvContent.AppendLine("First Name,Last Name");
- csvContent.AppendLine("Lajapathy,Arun");
- csvContent.AppendLine("Anand,Babu");
- csvContent.AppendLine("Sathiya,Seelan");
-
-
- 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.