Saturday, March 24, 2012

trouble with cvs files and how to read them

I'm trying to read data from a cvs file in this format

persons name|phone number|desk location|staff id
persons name|desk location|staff id
persons name|phone number|desk location|email|staff id
and I've been looking over the web and found a couple of different solutions, one which used a?BufferedReader?but?couldn't?get?mine?to?work

Is this the best way to access this file or is there another?

If you use Excel or SQL Server both can import csv.

My own preference is to import them into SQL tables where I finesse them with stored procedures.


I am planning to import the cvs files into SQL however because the app is going to be automated, so the update will happen without any need for me or anyone else to do anything...

A lotus notes databases outputs a csv file which a VB.Net app then converts into SQL for a web site to use again without any need for a developer to interact with the program at any point.

so I just need a way to import the data into the vb.net app

any idea?

Hi, before 10 days I've created a similar logic code,
but it runs in C#.

Let me know if you're interested..

In general to get things started I've created the following function,
it should be easy to convert it to vb.net.. You pass 2 parameters, the name of the file and the Path that
the file is saved (on my solution on the web server..)

publicstaticstring ReadExcelFileToString(string current_file,string pathToSave)

{

string FileName ="";

FileName = pathToSave + current_file;

string file =HttpContext.Current.Server.MapPath(FileName);

StreamReader sr;

FileInfo fi =newFileInfo(file);

string fileText ="";

if (File.Exists(file))

{

sr =File.OpenText(file);

fileText += sr.ReadToEnd();

sr.Close();

}

return fileText;

}


Read the string in,

Assign it to an array like this:

Dim arrRecordas Array = strLineJustRead.Split("|")

That will split all the values out by the tokenizer (my favorite java term!)

Now you can loop through taht array to build a record, or to build an object class to pass to some database call.

Whatever suits you best.


Yep,
the split logic is exactly like mine.

Also! A few notices:
1) When you get the information in a string, write some code to clear some not wanted characters like \n\r.
2) If you open a .csv file just to take a look or delete anything, be carefull! When you save it from inside Excel, it saves some more characters,
and some times slightly changes the values of the existing data as well. For example I had a column with value 0013. After saving it with Excel,
although I've asked it to save it as simple csv and loose all excel features, it changed the number to 000013..
If you don't open it or not save it at all it will be fine.

PB


Opening the CSV files and looking at the format is a very good tip.

As an example Excel and Oracle may end the same exact line with a different character.

That's no problem if you're ready for it...

0 comments:

Post a Comment