Reading special characters from CSV with Visual Basic .NET

Posted on 2007-10-02
Last Modified: 2010-04-23
I created a csv file (using excel 2007).  I need to be able to save text from different languages in this file, and I can.  I paste phrases in the csv, save it and when I open the file, the phrases show up exactly as I pasted them in.  An example of one such phrase is "Disposición".  The problem is that I need to process this csv with a visual basic program (using Visual Studio 2005).  When I read the lines of the csv into VB, the accented character shows up as a "square"...

Here's a snippet of my code:
        Dim fileLine As String

        ' open the file...
        Using streamReader As New IO.StreamReader(txtFileName.Text)

            fileLine = streamReader.ReadLine

"fileLine" contains:  "Disposiciýn"

Can someone tell me how I can read this csv and get the special characters?  I've tried creating my StreamReader like so:
Using streamReader As New IO.StreamReader(txtFileName.Text, Encoding.Unicode)

and this is worse...

Any help would be greatly appreciated!
Question by:ablesa
    LVL 27

    Accepted Solution

    Try this:

        Using streamReader As New IO.StreamReader(txtFileName.Text, Encoding.GetEncoding(0))

    LVL 27

    Expert Comment

    You can also use 1252, like this:

        Using streamReader As New IO.StreamReader(txtFileName.Text, Encoding.GetEncoding(1252))


    Author Comment

    the first one works with the spanish, but as I was testing further, I see that I now have a problem with my csv file.  I copied and pasted some chinese characters into my csv and saved it (using Arial).  When I pasted the text into the file, it was displaying properly, but when I save and re-open the file, it displays "????" where the chinese characters should have been.  Any ideas there?

    Thanks so much for your help!
    LVL 27

    Expert Comment

    I'm not sure how to handle that, because they use different encodings.  You may need to come up
    with a way to tag each section with the appropriate codepage (ex. 1252 for Western-European), and
    then extract the tag and change encoding before reading it...



    Author Comment

    no  problem.  thanks for your help.  You got me past the major hurdle!  

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now