Results 1 to 17 of 17

Thread: [2005] How to read excel file and display in text box

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2008
    Posts
    9

    [2005] How to read excel file and display in text box

    Hi all,

    How can I read the the .csv file and display the particular string (channel 1) in textbox?

    say I have write into a .csv file using code below

    SW = File.CreateText("C:\" & "test" & ".csv")
    SW.Write("Channel 1, Channel 2, Channel 3, Channel 4, Channel 5, Channel 6, Channel 7, Channel 8")
    SW.Close()

    Cheyne

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] How to read excel file and display in text box

    First up, a CSV file is not an Excel file. An Excel file is an XLS file. CSV is a standard, delimited text format that Excel can open.

    As to your question, you can read CSV files in various ways. You could use a StreamReader, but you wouldn't. I would suggest either using ADO.NET and reading the file as though it was a database, or else using a TextFieldParser object. www.connectionstrings.com can give you the details for ADO.NET and I've posted an example of using a TextFieldParser previously, so a search of the forum will bring it up.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2008
    Posts
    9

    Re: [2005] How to read excel file and display in text box

    Hi,

    If I change the file format to .xls while writing into it. all the channel 1, channel 2 cannot be written in a excel box. it will split up. But when using .csv it could fit into a box in the .csv file for the whole string (say channel 1).

    Reading a .csv file easier or reading a .xls file easier?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] How to read excel file and display in text box

    If you're going to write comma-separated values to the file then it's a CSV file. If you want to create an XLS file then you'd need to write it in the correct binary format, which you'd normally do by automating the Excel application itself. Stick with CSV unless you specifically need an XLS file. It's easier that way.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Jun 2008
    Posts
    9

    Re: [2005] How to read excel file and display in text box

    I am using this code to actually read the value to textfile. but it couldnt work

    Please advice.

    Code:
    Dim line As String
    Dim SR As StreamReader
    SR = New StreamReader("C:\" & "test" & ".csv")
    
    Do
         line = SR.ReadLine()
         TextBox1.Text = line90)
         Console.WriteLine(line)
    Loop Until line Is Nothing
    SR.Close()

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] How to read excel file and display in text box

    I've already told you to use ADO.NET or a TextFieldParser. That code does neither. If you're not going to follow the advice I provide then I can help no further.
    Quote Originally Posted by jmcilhinney
    You could use a StreamReader, but you wouldn't. I would suggest either using ADO.NET and reading the file as though it was a database, or else using a TextFieldParser object. www.connectionstrings.com can give you the details for ADO.NET and I've posted an example of using a TextFieldParser previously, so a search of the forum will bring it up.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    New Member
    Join Date
    Jun 2008
    Posts
    9

    Re: [2005] How to read excel file and display in text box

    Can you provide me a simple sample code on how to read the .csv file ? Just a simple one will do.

    Thanks in advance

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] How to read excel file and display in text box

    As I have already stated, I have already posted example code to read a delimited text file on the forum. I'm not going to do it again. For me to tell you where that example is I would have to search the forum for textfieldparser with my user name. You can do that just as easily as I can. I suggest some initiative on your part.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    New Member
    Join Date
    Jun 2008
    Posts
    9

    Re: [2005] How to read excel file and display in text box

    Hi,

    I have managed to do it.

    So, let say I am going to read a .xls file. Any recommendation on how I could do it?

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2005] How to read excel file and display in text box

    In order to create an xls file format file you wil need to use the Excel COM object library. Since you need to use the EOM you might as well open your csv file with it too to simplify and shorten code.

    See my FAQ item on opening a csv and saving to xls

    http://www.vbforums.com/showthread.php?t=408487
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] How to read excel file and display in text box

    You can also read XLS files using ADO.NET, pretty much as you would an Access database. What you can do is far more limited than using Excel Automation but it's easier so if it serves your needs then I'd go that way. www.connectionstrings.com has the connection details.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    New Member
    Join Date
    Jun 2008
    Posts
    9

    Re: [2005] How to read excel file and display in text box

    Hi Rob Dog,

    The thread you put here does not have any VB2005 code.

    jmcilhinney,

    I cant find any information related to ADO.net from the site you recommended.

    Please kindly provide me some sample code if possible.

    I would learn faster by looking at the sample code.

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] How to read excel file and display in text box

    The site I recommended provides the connection string formats for numerous ADO.NET data sources. If you don't know anything about ADO.NET in general then you should read some ADO.NET tutorials, of which there are many on the Web and even one on this site. The Data Access link in my signature also provides some code examples for common data access scenarios.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14

    Thread Starter
    New Member
    Join Date
    Jun 2008
    Posts
    9

    Re: [2005] How to read excel file and display in text box

    Hi.

    Say I want to read a particular box from the excel file. How could I proceed from the code below?

    Please advice.

    Code:
    Dim conn As New OleDbConnection
            Dim connString As String
            connString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                         "Data Source=C:\test.xls;"
            conn.ConnectionString = connString
            conn.Open()
    Thanks alot

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] How to read excel file and display in text box

    Have you read the Data Access link of mine? It shows you how to read data from an ADO.NET data source using a data reader or, alternatively, into a DataTable. If you use the DataTable option then you can get the desired row by index from the table's Rows collection, then the desired field by column name or index. For example, this code:
    vb.net Code:
    1. Dim value As Object = myDataTable.Rows(0)("SomeColumn")
    gets the value from the SomeColumn column of the first row in the table.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  16. #16

    Thread Starter
    New Member
    Join Date
    Jun 2008
    Posts
    9

    Re: [2005] How to read excel file and display in text box

    hi,

    I have read about it but not really can understand the concept behind.

    Could you just provide a simple code on how to read a value from a particular column and row.

    I just need to know the code that does the work.

    Thanks

  17. #17
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [2005] How to read excel file and display in text box

    Quote Originally Posted by cheynewong
    Hi Rob Dog,

    The thread you put here does not have any VB2005 code.

    jmcilhinney,

    I cant find any information related to ADO.net from the site you recommended.

    Please kindly provide me some sample code if possible.

    I would learn faster by looking at the sample code.
    If you notice the heading above the code example ...
    "Visual Basic .NET 2003-2008 And Excel 2000-2003"

    The question of even using ADO.NET could be answered if we know what are your plans for using the csv/xls file.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width