csv read write

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eran.Yasso@gmail.com

    csv read write

    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.

  • =?Utf-8?B?WE9S?=

    #2
    RE: csv read write

    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( FileUploadImpor t.PostedFile.In putStream);
    while (reader.Peek() >= 0) //not eof
    {
    String line = reader.ReadLine ();
    String[] items = line.Split(',') ;
    //now you can get the cells
    String name = items[0];
    }



    Comment

    • =?Utf-8?B?VmluY3k=?=

      #3
      RE: csv read write

      you can reference Interop.Excel Com object and use
      oXL.Workbooks.O penText
      where oXL is the Excel.Applicati on.

      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( FileUploadImpor t.PostedFile.In putStream);
      while (reader.Peek() >= 0) //not eof
      {
      String line = reader.ReadLine ();
      String[] items = line.Split(',') ;
      //now you can get the cells
      String name = items[0];
      }
      >
      >
      >

      Comment

      • Eran.Yasso@gmail.com

        #4
        Re: csv read write


        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( FileUploadImpor t.PostedFile.In putStream);
        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?

        Comment

        • Eran.Yasso@gmail.com

          #5
          Re: csv read write


          Vincy wrote:
          you can reference Interop.Excel Com object and use
          oXL.Workbooks.O penText
          where oXL is the Excel.Applicati on.
          >
          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( FileUploadImpor t.PostedFile.In putStream);
          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.O penText that i can reference
          to? Like
          Microsoft Office Excel 11.0?

          Comment

          • Marc Gravell

            #6
            Re: csv read write

            Look at http://www.codeproject.com/cs/database/csvreader.asp

            Marc


            Comment

            • Otis Mukinfus

              #7
              Re: csv read write

              On Tue, 2 Jan 2007 23:37:00 -0800, Vincy <Vincy@discussi ons.microsoft.c om>
              wrote:
              >you can reference Interop.Excel Com object and use
              oXL.Workbooks.O penText
              >where oXL is the Excel.Applicati on.
              >
              >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( FileUploadImpor t.PostedFile.In putStream);
              >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


              Comment

              • Marc Gravell

                #8
                Re: csv read write

                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 internationalis ations
                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
                joe.bloggs@some where.com sense, but soon gets scary if you really read
                the spec... comments... folding whitespace... uri endpoints (e-mail
                addresses without an @), etc...

                Marc


                Comment

                • Eran.Yasso@gmail.com

                  #9
                  Re: csv read write


                  Otis Mukinfus wrote:
                  On Tue, 2 Jan 2007 23:37:00 -0800, Vincy <Vincy@discussi ons.microsoft.c om>
                  wrote:
                  >
                  you can reference Interop.Excel Com object and use
                  oXL.Workbooks.O penText
                  where oXL is the Excel.Applicati on.

                  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( FileUploadImpor t.PostedFile.In putStream);
                  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.tomchilders.com
                  Can't I do it with SQL? SQL doesn't know how to get specific cell(by
                  cell/row number)?

                  Comment

                  • Otis Mukinfus

                    #10
                    Re: csv read write

                    On Wed, 3 Jan 2007 12:09:12 -0000, "Marc Gravell" <marc.gravell@g mail.com>
                    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 internationalis ations
                    >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
                    >joe.bloggs@som ewhere.com 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


                    Comment

                    • Marc Gravell

                      #11
                      Re: csv read write

                      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


                      Comment

                      • Otis Mukinfus

                        #12
                        Re: csv read write

                        On Thu, 4 Jan 2007 12:57:16 -0000, "Marc Gravell" <marc.gravell@g mail.com>
                        wrote:
                        >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
                        >implementation s 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


                        Comment

                        Working...