Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert single column of a DataTable to CSV

Using VB.NET, what is the most concise way to convert a single column of a DataTable to a CSV? The values are integers, so I don't need to worry about character escaping or encoding.

like image 986
Mass Dot Net Avatar asked Aug 22 '11 14:08

Mass Dot Net


People also ask

How to use a CSV file in Excel?

Another use of a CSV file is to directly open the file in Excel and then the data will be auto-filled into Excel cells. The following is a snapshot of a sample CSV file: Here is the process of creating a DataTable and exporting its data to a .csv file. a. Create a DataTable We added a class containing a method that returns a DataTable.

How to convert a Dataframe to CSV using PANDAS to_CSV?

file is the file name for the csv created from the dataframe. In this example, we are converting only id column without headers By default pandas to_csv will convert a dataframe to CSV using comma as the separator which is the most common delimiter in CSV files. But if you wish to select a different separator then you can use following syntax:

How to export DataTable to CSV using ADO?

If you're new to ADO.NET and DataTable, first read this: DataTable in C# Code: b. Create UI to display DataTable Here we created a simple DataGridView to bind to the DataTable. Code After adding the Extension method the ToCSV method is now appearing in the list below: d. Export to CSV on button click e. Call ToCSV method

How do I export data from a project to a CSV?

Build and run the project Now build and run the project. Click on the button to export data. The output file will be test.csv. When you open the CSV file in Notepad, you will see this: By default, this file opens in Excel. Double-click to open this file in Excel. This is how the file looks like.


1 Answers

What do you mean with "convert to CSV"? If you want to generate a string with comma-separated values, you can use this(tbl is your DataTable and Int-Column is the name of the DataColumn):

String.Join(",", (From row In tbl.AsEnumerable Select row("Int-Column")).ToArray)

A CSV normally is a file-format where the columns are separated by comma(or other separators) and the rows are separated by new lines. Hence you simply have to replace String.Join("," with String.Join(Environment.NewLine

like image 143
Tim Schmelter Avatar answered Oct 23 '22 02:10

Tim Schmelter