Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading a csv file or xls file 1

Status
Not open for further replies.

ACuba10

Programmer
Jul 25, 2006
14
US
Can someone tell me how to tell when the end of a line is??
For instance, i have a csv file that we'll say looks like:

Supplies,Computers,24.95,annual,Books,19.95,one-time
Food,Chicken,15.95,annual,Steak,15.95,annual
Clothes,Shirt,9.95,one-time,shorts,5.95,one-time

My question is when reading the file, how can i tell when it has reached the end of each line and begins the next line?? Right now, there is no comma after the one-time column and therefore groups the last word of the line with the first word of the next line.
 
Either use the Microsoft Text Driver and read the data into a DataReader or use a StreamReader and read one line at a time - both methods will negate the need to know where the line ends.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I am using a streamreader and it doesnt recognize the end of the line. My code looks like this:
Code:
Dim file as string = server.mappath("Data.csv")
Dim filestream as StreamReader
filestream = filestream.ReadToEnd
Dim txtdelimeter as string
txtdelimeter = ","
Dim splitout = Split(readcontents, txtdelimeter)
Dim i as integer
For i = 0 to Ubound(splitout)
Console.Write &= "<b>Split </b>" & i + 1 & ") " & splitout(i) & <br>"
Next

What am i doing wrong that it desont recognize the end of each line, it only recognizes the commas.
 
that is because filestream.ReadToEnd is reading the entire file; use filestream.ReadLine
 
ok thanx but then how do i read the next line??
 
here is a sample of how i'm currently using it

Code:
        if (strFileName == String.Empty)
        {
            strFileName = FileUpload1.PostedFile.FileName;

        }
        StreamReader sr;

        sr = File.OpenText(strFileName);

        string strContents = String.Empty;
        char strDelimiter = '^';

        DataTable dtHotels = CreateMappingTable();

        while (sr.Read() > 0)
        {

            strContents = sr.ReadLine();
            string[] strSplit = strContents.Split(strDelimiter);

            DataRow dr = dtHotels.NewRow();
            dr["CustomerHotelID"] = strSplit[0];
            dr["PropertyInfo"] = strSplit[1] + " " + strSplit[2] + " " + strSplit[3] + " " + strSplit[4] + " " + strSplit[5];
            dtHotels.Rows.Add(dr);
        }

        sr.Close();

        grdvHotels.DataSource = dtHotels;
        grdvHotels.DataBind();
 
Thankyou very much for your help - that helped alot and i figured out my issue.
 
ok thanx but then how do i read the next line??
If you want to know how to use a function, read the help file on the method in question as in most cases there is an example of what it does and how to use it.



____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top