PC Review


Reply
Thread Tools Rate Thread

csv read write

 
 
Eran.Yasso@gmail.com
Guest
Posts: n/a
 
      3rd Jan 2007
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.

 
Reply With Quote
 
 
 
 
=?Utf-8?B?WE9S?=
Guest
Posts: n/a
 
      3rd Jan 2007
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];
}



 
Reply With Quote
 
=?Utf-8?B?VmluY3k=?=
Guest
Posts: n/a
 
      3rd Jan 2007
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" wrote:

> 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];
> }
>
>
>

 
Reply With Quote
 
Eran.Yasso@gmail.com
Guest
Posts: n/a
 
      3rd Jan 2007

XOR wrote:
> 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?

 
Reply With Quote
 
Eran.Yasso@gmail.com
Guest
Posts: n/a
 
      3rd Jan 2007

Vincy wrote:
> 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" wrote:
>
> > 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?

 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      3rd Jan 2007
Look at http://www.codeproject.com/cs/database/csvreader.asp

Marc


 
Reply With Quote
 
Otis Mukinfus
Guest
Posts: n/a
 
      3rd Jan 2007
On Tue, 2 Jan 2007 23:37:00 -0800, Vincy <(E-Mail Removed)>
wrote:

>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" wrote:
>
>> 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
 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      3rd Jan 2007
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 Removed) sense, but soon gets scary if you really read
the spec... comments... folding whitespace... uri endpoints (e-mail
addresses without an @), etc...

Marc


 
Reply With Quote
 
Eran.Yasso@gmail.com
Guest
Posts: n/a
 
      4th Jan 2007

Otis Mukinfus wrote:
> On Tue, 2 Jan 2007 23:37:00 -0800, Vincy <(E-Mail Removed)>
> wrote:
>
> >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" wrote:
> >
> >> 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)?

 
Reply With Quote
 
Otis Mukinfus
Guest
Posts: n/a
 
      4th Jan 2007
On Wed, 3 Jan 2007 12:09:12 -0000, "Marc Gravell" <(E-Mail Removed)>
wrote:

>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 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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to write/read a ntw folder from a clickonce C# application where the user does not have permission to write/read on/from it jpf.wk@uol.com.br Microsoft Dot NET 1 26th Jul 2007 04:01 PM
Harddisks: Seek, Read, Write, Read, Write, Slow ? Skybuck Flying Windows XP Hardware 40 18th Jul 2004 05:38 PM
Re: Harddisks: Seek, Read, Write, Read, Write, Slow ? Folkert Rienstra Computer Hardware 18 17th Jul 2004 10:35 AM
Re: Harddisks: Seek, Read, Write, Read, Write, Slow ? Folkert Rienstra Storage Devices 18 17th Jul 2004 10:35 AM
Re: Harddisks: Seek, Read, Write, Read, Write, Slow ? Folkert Rienstra Windows XP Hardware 16 17th Jul 2004 10:35 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:13 AM.