locked
Reading and writing CSV and using csv data in fields. RRS feed

  • Question

  • Hi all,

     

    I have a problem. I have found a way to make a csv file from a windows form. So I have three textfields that arre filled in and then "Save" function to save it into a csv. (So manual input into the csv file).

    I can see the whole csv file in a large textfield. When I open the CSV in notepad I can see that everything is there as I wanted it o be. -->>> data1,data2,data3

    But now I was looking for a way to read that data and put it back in to some textfields in a windows form.

    Something like

    line1 - data1,data2,data3 has to go in  line1 - textfield1 - textfield2 - textfield3 (on a windows form)

    This should be repeated until the whole csv is shown on textfields.

    Eventualy I would like to adjust some data from thos textfields and save that again. (the hard part)

    I would be happy if I could already read the data into the textfields

     

    If you have any questions .. please don't hesitate

     

    Preve

    Thursday, October 18, 2007 8:14 AM

Answers

  • Preve,

     

    According to your question on reading and writing CSV file and using in text fields, I would like to provide you the points as follows:

     

    1. CSV is just coma delimited plain text file. if the field contains coma, you cna use double quote around it. Such as:

    1024, "test, another test", nothing

    Basically you need to parse/write such format. if there's no ',' coma in your fields, it's just trivial. You can read each line, use string.split(',') to get all fields into string[]

    Then you can use StreamWriter.Writeline(string.format("{0},{1},{2},{3},{4}",fields[0],fields[3],fields[5],fields[4]) to convert the 6 fields to 5 fields as you presented above.

    For further discussion on the topic, please take a look at the following thread:

     

    Format CSV using C#

     

    2. I would like to recommend you to take a look at the CSV importer in this article: Import Table - CSV

     

    CSV refers to a family of ASCII (plain text) formats that save tabular data in the form of one line per record with the values for each field in that record separated by a delimiting character. The comma is most frequently used as a delimiter, hence the name Comma Separated Value (CSV) format. Files that use characters other than commas, such as tab characters or some other character, are not "CSV" files as a matter of strict nomenclature and often use a different three letter extension such as .txt or .tab. Manifold's CSV importer can handle most such cases.

     

    There is considerable variation in how different programs work with "CSV" format. Some programs like to see a comma at the end of each line and others do not. Most programs expect to see only string field values enclosed in quotes, but some like to see all values enclosed in quotation marks whether they are strings or not. Manifold's CSV exporter provides options to deal with both situations.

     

    The CSV importer (due to a limitation of the Microsoft driver used) can only import CSV files from read/write media such as hard disks. It cannot import a CSV file from read-only media such as CDs, nor can it import a file from a folder in which we do not have write permission. In such cases, we must copy the file from the originating folder into a working folder in which we have read/write permissions. For example, we might copy a file from a CD onto a folder on our hard disk.

     

    3. There is an example in code project on the more complicated issue, hope the can provide you some idea:

     

    A Fast CSV Reader

     

    One would imagine that parsing CSV files is a straightforward and boring task. I was thinking that too, until I had to parse several CSV files of a couple GB each. After trying to use the OleDB JET driver and various regular expressions, I still ran into serious performance problems. At this point, I decided I would try the custom class option. I scoured the net for existing code, but finding a correct, fast, and efficient CSV parser and reader is not so simple, whatever platform/language you fancy.

     

    Hope that can help you.

     

    This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Monday, October 22, 2007 7:52 AM