We are working on a project in which we are exporting bulk data into a CSV file for our data science team. we have to export values like Ammy, IT, Paris France which all have to remain in one column and the comma will separate them into different columns.
But problem is that sometimes which address contains the comma(i. e Paris, France) “,” or character like \r\n will be exported as separate rows in the CSV.So in this article, you are going to learn how to Insert commas into a CSV string using c#.

we can solve that problem in a number of ways but the most generic method is to use double quote ” ” around the field, but it really depends on who is going to use your CSV files, we can delimit the commas, can change the commas to a special character or we can use a different delimiter, but the most common method is to use the “” around the values.

How to write a value which contain comma to a CSV file in c#?

In the below example, we are Writing Comma-separated values between double-quotes without space to generate CSV with a single column for comma-separated values.

Ex. I have three columns name, department & address With Values John HR & 24, Paris, France. so for creating CSV with given data, we need to generate the below line in our CSV.

name,department ,address
John,HR , "24, Paris, France"

Code

public class Employee
    {
        public string name { get; set; }
        public string department { get; set; }
        public string address { get; set; }
    }
    
   public void CreateCsvFileWithComma()
        {
            string csvFilePath = @"E:\MyJson\data.csv";
            //adding item in the list
            List<Employee> employees = new List<Employee>();
            employees.Add(new Employee { name = "john", department = "HR", address = "24, place Delhi" });
            employees.Add(new Employee { name = "amit", department = "IT", address = "24, Janakpuri, Delhi" });
            StringWriter csv = new StringWriter();
            // Creating header of the CSV file
            csv.WriteLine(string.Format("{0},{1},{2}", "Name", "Department", "adddress"));
            // Creating content of the CSV file
            foreach (var item in employees)
            {
                csv.WriteLine(string.Format("{0},{1},{2}", item.name, item.department, "\"" + item.address + "\""));
            }
            File.AppendAllText(csvFilePath, csv.ToString());
        }

A CSV record, as the name suggests, ordinarily isolates data utilizing commas. It is an approach to trading organized data between programs, for example, the items in a calculation sheet, that may not be guaranteed to talk straightforwardly to one another.

However long the two projects can both open a CSV record, they can trade information. For instance, you can save contact data from Microsoft Excel as a CSV record, and import it into the Address Book in Microsoft Outlook.

The csv record design isn’t completely normalized. Isolating fields with commas is the establishment, yet commas or implanted line breaks in information must be taken care of exceptionally. A few executions deny such happy while others encompass the field with quotes, which makes the requirement for re-getting away in the event that quotes are available in the information.

The expression “CSV” likewise alludes to a few firmly related delimiter-isolated designs that utilization other field delimiters, for example, semicolons. These incorporate tab-isolated values and space-isolated values. A delimiter that is ensured not to be essential for the information enormously rearranges parsing.
Discretionary delimiter-isolated records are much of the time given the “.csv” expansion, notwithstanding the utilization of a non-comma field separator. This free phrasing can bring on some issues in information trade. Numerous applications that acknowledge CSV records have choices to choose the delimiter character and the statement character.

The post [Simple Way]-Cascading DropDownList in Asp.Net Mvc Using Jquery Ajax appeared first on Software Development | Programming Tutorials.



Read More Articles