17-Apr-2007
.net, c#
2,069 views
During the course of time, I often required to write solutions which involve reading CSV (Comma Separated Values) Files. After repeating the same thing for a few times, I decided to write a component that will parse the CSV file and return a DataTable.
To use the the component is really simple. First you need to declare the object,
CsvDataSource.CsvDataSource src = new CsvDataSource.CsvDataSource();
and then read the CSV file. There are three possible way to read the CSV:
//Using comma as character separator, first line is NOT treated as column names
DataTable dt1 = src.ReadFile("input.csv");
//Using comma as separator character, first line is treated as column names
DataTable dt2 = src.ReadFile("input.csv", true);
//Using pipe (|) as separator character, first line is treated as column names
DataTable dt3 = src.ReadFile("input.csv", true, '|');
The complete source code as follows: Download the class source code.
Simple demo using Command Line Application:
/* Demo of CsvDataSource using Command Line Application
* (c) 2007 Hardono Arifanto
* http://sodeve.net
*/
using System;
using System.Collections.Generic;
using System.Text;
using CsvDataSource;
using System.Data;
namespace CsvDataSourceDemo
{
class Program
{
private static void printHelp()
{
Console.WriteLine(@"Demo for CsvDataSource.
Usage: CsvDataSourceDemo [inputfile.csv]");
}
static void Main(string[] args)
{
DataTable dt;
if (args.Length > 0)
{
CsvDataSource.CsvDataSource src = new CsvDataSource.CsvDataSource();
dt = src.ReadFile(args[0],true,',');
//Display the DataTable content
foreach (DataColumn dc in dt.Columns)
{
Console.Write(dc.ColumnName + "t");
}
Console.WriteLine();
foreach (DataRow dr in dt.Rows)
{
for(int i=0; i < dt.Columns.Count; i++)
{
Console.Write(dr[i] + "t");
}
Console.WriteLine();
}
}
else
printHelp();
}
}
}
About Hardono
Howdy! I'm Hardono. I am working as a
Software
Developer. I am working mostly in Windows, dealing with .NET, conversing in C#. But I know a bit of Linux, mainly because I need to keep this blog operational. I've been working in Logistics/Transport industry for more than 11 years.
Internet has now became inseparable factor of my life. I could mindlessly browsing slashdot, digg, lifehacker, blogs@ikastara (which later leads me to other indonesian blogs) and burn hours of my ‘precious’ time.
And this has to stop. For example today, I was supposed to be writing code to create an email relay, and completes the dotnetnuke project. After 10 lines of C# code, 1 hour testing telnet to connect SMTP and POP server, and 4+ hours browsing through websites mentioned in previous paragraph, I ended up hating myself for not doing what I supposed to do.
There’s a saying “the poison is your cure” (I hope I’m not made up that one :P), which fits the situation when I read this Article @ Lifehacker.
On average, I spent 4 hours to read blogs. I definitely need to curb the number of hours into somewhere 1 to 2 hours. What about you? How many hours you spent reading blogs? Do you plan to stop/reduce the number of hours?
About Hardono
Howdy! I'm Hardono. I am working as a
Software
Developer. I am working mostly in Windows, dealing with .NET, conversing in C#. But I know a bit of Linux, mainly because I need to keep this blog operational. I've been working in Logistics/Transport industry for more than 11 years.
Until now, I have wasted many hours searching for the best WordPress plugins that might help me to make this blog looks great. Hopefully this list will help you to save your precious time.
- C6 Excerpt
- To get a more flexible excerpt out of your blog entry
- Feed Burner Feed Replacement
- To redirect feed request to Feedburner. Thus, reducing traffic load.
- Google Sitemaps
- To make Google indexes your blog better
- Link To Me Textbox
- To nag people to link back to your blog entry
- Most Commented
- To display blog entries with most comments
- Recent Comments
- To display recent comments made by your blog visitors (not the alien, big brother, or the janitor) DOH!
- Related Posts
- To display a list of your blog entries which somehow related to the current entry.
- Run PHP
- Allows you to run PHP code in your post.
- Sexy Comments
- Make the comments look like forum post.
- Ultimate Warrior Tag
- Allows you to tag your blog entries.
- WP-Email
- Allows your visitors to send blog entry to his/her friends through email.
- WP-PageNavi
- Better page navigation for your blog.
- WP-Polls
- Add polls to your blog.
- WP-Stats
- Shows your blog statistics.
- WP-UserOnline
- Displays the visitors of your blog
- WP-Syntax
- Beautifully highlight source code embedded in your blog entry
- WP-Translate
- Translate your blog to many languages. Klingon and Elvens is not yet supported.
- WP-Postratings
- Let your visitors rate how good/sucks your writing skills are
- Gregarious
- Let your visitors share your good/sucks blog entries to other people
The WP-Postratings and Gregarious added one day after this post published 🙂
About Hardono
Howdy! I'm Hardono. I am working as a
Software
Developer. I am working mostly in Windows, dealing with .NET, conversing in C#. But I know a bit of Linux, mainly because I need to keep this blog operational. I've been working in Logistics/Transport industry for more than 11 years.