CSVReader is a simple, lightweight open source C# class library to read CSV data from text files and strings. To use it, simply download the latest version and add it to your project, either as a compiled DLL (it will build in any version of Visual Studio 2008) or as source.
Download: CSVReader source v1.0.2
If you want to include CSVReader directly in your source code, just download CSVReader.cs
and StringConverter.cs
and add them to your C# project.
Example
- Download the source CSVReader source code to a folder. (The easiest way to do this is to install Subversion and check it out.)
- Open Visual Studio and create a new Windows Forms Application project.
- Select File >> Add >> Existing Project and navigate to
CSVReader.csproj
. Add it to the project.
- Right-click on "References" in your test project, select "Add References", click the Projects tab, and select the CSVReader project.
- Drag a DataGridView control out of the toolbox onto your form. Make sure it's named
dataGridView1
and that its Dock property is set to Fill.
- Add
using Com.StellmanGreene.CSVReader;
to the top of the form code in Form1.cs.
- Replace the form's constructor with this code:
public Form1()
{
InitializeComponent();
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "CSV Files (*.csv)|*.csv";
if (dialog.ShowDialog() == DialogResult.OK)
{
DataTable table = CSVReader.ReadCSVFile(dialog.FileName, true);
dataGridView1.DataSource = table;
}
}
- Run your program. When it starts, it will pop up a dialog box. Navigate to a valid CSV file with a header row and click OK. The contents of the file will be loaded into the data grid on the form.
You can see lots of example code in the
source code for the unit tests. CSVReader can read CSV data:
- From a string
- Using a FileInfo object
- From a TextReader
- Using string extension methods
License
Revision history
-
CSVReader 1.0.2 -- 13-Jan-2009
Switched from string concatenation to using StringBuilder, added test for UTF-8 file with special characters
-
CSVReader 1.0.1 -- 13-Jan-2009
Switched from internal TextReader to BinaryReader to allow reading of high-bit characters
-
CSVReader 1.0.0 -- 01-Sep-2008
Initial release