csv read write

E

Eran.Yasso

Hi All,

I searched the web to see how i can read csv file (specific cells) but
didn't find much.
Can anyone give me a link or help how to read or write to a specific
cell in csv file?
Using SQL can be great.

TIA.
 
G

Guest

I generally dont search my csv files - I generally parse the entire file for
an import or something.

But basically what I do is load the file into a StringReader and then split
each line by the delimited to get each individual cell.

eg:
reader = new StreamReader( FileUploadImport.PostedFile.InputStream);
while (reader.Peek() >= 0) //not eof
{
String line = reader.ReadLine();
String[] items = line.Split(',');
//now you can get the cells
String name = items[0];
}
 
G

Guest

you can reference Interop.Excel Com object and use
oXL.Workbooks.OpenText
where oXL is the Excel.Application.

you can loop through the Usedrange to get the no. of rows and columns used.

You have Cells.Offset or Cell.Select to get the individual cell.

Regards,
Vincent
 
E

Eran.Yasso

XOR said:
I generally dont search my csv files - I generally parse the entire file for
an import or something.

But basically what I do is load the file into a StringReader and then split
each line by the delimited to get each individual cell.

eg:
reader = new StreamReader( FileUploadImport.PostedFile.InputStream);
while (reader.Peek() >= 0) //not eof
{
String line = reader.ReadLine();
String[] items = line.Split(',');
//now you can get the cells
String name = items[0];
}

Hello XOR and thanks for the reply,

Your solution won't work if user insert ',' into cell as text.
What can i do about it?
 
E

Eran.Yasso

Vincy said:
you can reference Interop.Excel Com object and use
oXL.Workbooks.OpenText
where oXL is the Excel.Application.

you can loop through the Usedrange to get the no. of rows and columns used.

You have Cells.Offset or Cell.Select to get the individual cell.

Regards,
Vincent

XOR said:
I generally dont search my csv files - I generally parse the entire file for
an import or something.

But basically what I do is load the file into a StringReader and then split
each line by the delimited to get each individual cell.

eg:
reader = new StreamReader( FileUploadImport.PostedFile.InputStream);
while (reader.Peek() >= 0) //not eof
{
String line = reader.ReadLine();
String[] items = line.Split(',');
//now you can get the cells
String name = items[0];
}

Hi Vincy,

thanks for your reply also.
Is these com object called oXL.Workbooks.OpenText that i can reference
to? Like
Microsoft Office Excel 11.0?
 
O

Otis Mukinfus

you can reference Interop.Excel Com object and use
oXL.Workbooks.OpenText
where oXL is the Excel.Application.

you can loop through the Usedrange to get the no. of rows and columns used.

You have Cells.Offset or Cell.Select to get the individual cell.

Regards,
Vincent

XOR said:
I generally dont search my csv files - I generally parse the entire file for
an import or something.

But basically what I do is load the file into a StringReader and then split
each line by the delimited to get each individual cell.

eg:
reader = new StreamReader( FileUploadImport.PostedFile.InputStream);
while (reader.Peek() >= 0) //not eof
{
String line = reader.ReadLine();
String[] items = line.Split(',');
//now you can get the cells
String name = items[0];
}

However, as revealed in your previous post where you ask how to use a semicolon
for a delimiter, you lose the ability to tailor the reading of the file(s) to
your specifications.

It is better to write the parsing code yourself unless you are OK with the
defaults of some ready made solution.

Parsing delimited files as XOR has shown you, is not at all difficult at all,
and is something every developer should learn to do proficiently .

Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 
M

Marc Gravell

I agree with the general statement, but anybody who thinks that
parsing CSV (specifically) is trivial hasn't read the full CSV spec...
in the *full* case, since it has disparate escape sequences for
different scenarios (such as quoted and unquoted, multiline, etc) it
can be just painful. And don't forget that some internationalisations
of CSV apps use semicolon instead of comma by default (Excel in French
IIRC). If you know that you only use a subset of the spec then you can
get away with simpler options like Split- and Regex-based solutions.
The codeproject link I posted claims to deal with most scenarios
including choice of delimiter (I'm not vouching for the code, but
looks useful).

Similar to parsing an e-mail address; sounds easy in the
(e-mail address removed) sense, but soon gets scary if you really read
the spec... comments... folding whitespace... uri endpoints (e-mail
addresses without an @), etc...

Marc
 
E

Eran.Yasso

Otis said:
you can reference Interop.Excel Com object and use
oXL.Workbooks.OpenText
where oXL is the Excel.Application.

you can loop through the Usedrange to get the no. of rows and columns used.

You have Cells.Offset or Cell.Select to get the individual cell.

Regards,
Vincent

XOR said:
I generally dont search my csv files - I generally parse the entire file for
an import or something.

But basically what I do is load the file into a StringReader and then split
each line by the delimited to get each individual cell.

eg:
reader = new StreamReader( FileUploadImport.PostedFile.InputStream);
while (reader.Peek() >= 0) //not eof
{
String line = reader.ReadLine();
String[] items = line.Split(',');
//now you can get the cells
String name = items[0];
}

However, as revealed in your previous post where you ask how to use a semicolon
for a delimiter, you lose the ability to tailor the reading of the file(s) to
your specifications.

It is better to write the parsing code yourself unless you are OK with the
defaults of some ready made solution.

Parsing delimited files as XOR has shown you, is not at all difficult at all,
and is something every developer should learn to do proficiently .

Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com

Can't I do it with SQL? SQL doesn't know how to get specific cell(by
cell/row number)?
 
O

Otis Mukinfus

I agree with the general statement, but anybody who thinks that
parsing CSV (specifically) is trivial hasn't read the full CSV spec...
in the *full* case, since it has disparate escape sequences for
different scenarios (such as quoted and unquoted, multiline, etc) it
can be just painful. And don't forget that some internationalisations
of CSV apps use semicolon instead of comma by default (Excel in French
IIRC). If you know that you only use a subset of the spec then you can
get away with simpler options like Split- and Regex-based solutions.
The codeproject link I posted claims to deal with most scenarios
including choice of delimiter (I'm not vouching for the code, but
looks useful).

Similar to parsing an e-mail address; sounds easy in the
(e-mail address removed) sense, but soon gets scary if you really read
the spec... comments... folding whitespace... uri endpoints (e-mail
addresses without an @), etc...

Marc

Marc,

Now you've found me out ;o)

I didn't know there was a spec for CSV files.

I do a lot of backend file parsing and have written several routines to handle
all the anomalies (known to me) of parsing delimited files (I wish we would stop
saying "CSV files", because as most of you know, there are many many more
delimiters used in delimited files).

Can you give me a link to the spec you mentioned?

Thanks,

Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 
M

Marc Gravell

It evolved one over time, like fungus. RFC4180 would be a good start:
http://tools.ietf.org/html/rfc4180. But as we are aware, various
implementations over the years lead to all sorts of nuances on this,
including (but not limited to) the semi-colon debacle, line-ending
disparity (note RFC2046 specifies CRLF for all MIME "text" subtypes,
and RFC4180 declares CSV as text/csv, hence CRLF, but LF, CR, and LFCR
should all really be handled for compatibility), and various twists on
quoting.

Marc
 
O

Otis Mukinfus

It evolved one over time, like fungus. RFC4180 would be a good start:
http://tools.ietf.org/html/rfc4180. But as we are aware, various
implementations over the years lead to all sorts of nuances on this,
including (but not limited to) the semi-colon debacle, line-ending
disparity (note RFC2046 specifies CRLF for all MIME "text" subtypes,
and RFC4180 declares CSV as text/csv, hence CRLF, but LF, CR, and LFCR
should all really be handled for compatibility), and various twists on
quoting.

Marc
Thanks, Mark!

I once did a little research on the data residing in the AS400 at our shop and
found that in all fields entered by users there was at least one usage of each
character one can type from a keyboard. That makes parsing a challenge indeed!

The data validation rule for given our AS400 developers is thus, "Don't do any
validation that slows the user down, even if it is obviously bad, because it
costs us money.". Of course that edict was given by someone who believes
supporting such systems has no cost because they have no charge-back system in
place.

Most of my work and the work of my fellow developers is trying to track down
system problems caused by invalid data that comes to our backends from the
AS400. I'm just finishing a 3 month project that is designed to find and
correct those invalid data entries in between the AS400 and the systems that are
fed by the AS400 data. And you know that didn't cost anything at all. ;o)

Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top