Chinese characters don't display on excel using asp.net

Discussion in 'ASP .Net' started by c.verma@gmail.com, Feb 16, 2005.

  1. Guest

    I have a web application. There is a page which has a datagrid on
    it.The datagrid displays the data that comes from SAP. SAP sends the
    chinese characters to this grid. Before I display CHinese charactes, I
    have to use the following code to let it display on the web page:

    Public Function ToSCUnicode(ByVal str As String) As String
    Dim enc1252 As System.Text.Encoding =
    System.Text.Encoding.GetEncoding(1252)
    Dim arrByte_GBK As Byte()
    Dim arrByte_UTF16 As Byte()
    Dim encGBK As System.Text.Encoding =
    System.Text.Encoding.GetEncoding(936)
    Dim encUTF16 As System.Text.Encoding =
    System.Text.Encoding.Unicode
    Dim strUTF16 As String

    arrByte_GBK = enc1252.GetBytes(str)

    arrByte_UTF16 = System.Text.Encoding.Convert(encGBK, encUTF16,
    arrByte_GBK)
    strUTF16 = encUTF16.GetString(arrByte_UTF16)

    Return strUTF16
    End Function

    The web page has ALWAYS displayed the correct Chinese.

    The SAME web page I display in excel using the below code:


    If Request.QueryString("contenttype") = "excel" Then
    Response.Clear()
    Response.ContentType = "application/vnd.ms-excel"
    Me.EnableViewState = False
    Response.Charset = ""

    End If

    Problem: Few records display Chinese characters fine in excel but few
    display junk..
    I have tired setting up the regional settings to Chinese. Nothing was
    of any help.

    Any ideas?

    Thanks,
    Chanda.
    , Feb 16, 2005
    #1
    1. Advertising

  2. Joerg Jooss Guest

    wrote:

    > I have a web application. There is a page which has a datagrid on
    > it.The datagrid displays the data that comes from SAP. SAP sends the
    > chinese characters to this grid. Before I display CHinese charactes, I
    > have to use the following code to let it display on the web page:
    >
    > Public Function ToSCUnicode(ByVal str As String) As String
    > Dim enc1252 As System.Text.Encoding =
    > System.Text.Encoding.GetEncoding(1252)
    > Dim arrByte_GBK As Byte()
    > Dim arrByte_UTF16 As Byte()
    > Dim encGBK As System.Text.Encoding =
    > System.Text.Encoding.GetEncoding(936)
    > Dim encUTF16 As System.Text.Encoding =
    > System.Text.Encoding.Unicode
    > Dim strUTF16 As String
    >
    > arrByte_GBK = enc1252.GetBytes(str)
    >
    > arrByte_UTF16 = System.Text.Encoding.Convert(encGBK, encUTF16,
    > arrByte_GBK)
    > strUTF16 = encUTF16.GetString(arrByte_UTF16)
    >
    > Return strUTF16
    > End Function
    >
    > The web page has ALWAYS displayed the correct Chinese.
    >
    > The SAME web page I display in excel using the below code:
    >
    >
    > If Request.QueryString("contenttype") = "excel" Then
    > Response.Clear()
    > Response.ContentType = "application/vnd.ms-excel"
    > Me.EnableViewState = False
    > Response.Charset = ""
    >
    > End If
    >
    > Problem: Few records display Chinese characters fine in excel but few
    > display junk..
    > I have tired setting up the regional settings to Chinese. Nothing was
    > of any help.
    >
    > Any ideas?


    You're code is really weird.

    What is the purpose of ToSCUnicode? All .NET strings are Unicode
    strings. There's no such thing as converting a string to another string
    having a different encoding. Such actions only apply to byte arrays.

    What you do is
    (1) str->bytes(Windows-1252)
    (2) convert bytes(Windows-1252) to bytes(UTF-16) pretending they were
    bytes(Chinese Simplified)
    (3) bytes(UTF-16)->string

    I don't see how any chinese character could even survive step (1).

    Cheers,
    --
    http://www.joergjooss.de
    mailto:
    Joerg Jooss, Feb 19, 2005
    #2
    1. Advertising

  3. Guest

    Hi Joerg,
    It would be helpful, if you send me the piece of code.

    With the code that I have written, it displays the Chinese characters
    perfectly fine on the web page but it does behave randomly on the excel
    sheet. I have tried different kinds of codes, but the above one was the
    only one that could work.

    I would really appreciate if you send me the code.

    Thanks,
    Chanda.
    , Feb 22, 2005
    #3
  4. Guest

    In the above declaration: Public Function ToSCUnicode(ByVal str As
    String) As String

    str is the Chinese string coming from SAP, which if displayed on the
    webpage, displays junk characters. But after manipulating the above
    string in ToSCUnicode function, the Chinese characters display totally
    fine on the web page. After exporting on to excel, sometimes Chinese
    characters display fine, sometimes, they display junk. But the
    characters ALWAYS display fine on web page. So the above code works for
    web pages.
    , Feb 22, 2005
    #4
  5. Joerg Jooss Guest

    wrote:

    > In the above declaration: Public Function ToSCUnicode(ByVal str As
    > String) As String
    >
    > str is the Chinese string coming from SAP, which if displayed on the
    > webpage, displays junk characters.


    Is it really junk or do you just use the wrong encoding on your page?

    > But after manipulating the above
    > string in ToSCUnicode function, the Chinese characters display totally
    > fine on the web page.


    Then whatever comes from the SAP backend seems to be already broken.

    > After exporting on to excel, sometimes Chinese
    > characters display fine, sometimes, they display junk. But the
    > characters ALWAYS display fine on web page. So the above code works
    > for web pages.


    And still fixes a problem that shouldn't be there in the first place ;-)

    Cheers,

    --
    http://www.joergjooss.de
    mailto:
    Joerg Jooss, Feb 24, 2005
    #5
  6. Joerg Jooss Guest

    wrote:

    > Hi Joerg,
    > It would be helpful, if you send me the piece of code.
    > With the code that I have written, it displays the Chinese characters
    > perfectly fine on the web page but it does behave randomly on the
    > excel sheet.


    Regardless whether your conversion method is required, you still need
    to make sure that the spreadsheet you create uses the correct character
    encoding. What do you actually create? A CSV file or a true XLS?

    Cheers,
    --
    http://www.joergjooss.de
    mailto:
    Joerg Jooss, Feb 24, 2005
    #6
  7. Guest

    Well this is what is confusing me.
    1) I get Chinese characters from SAP. They don't get displayed fine on
    ASP.NET web pages. So I have to use TSCUnicode function to convert the
    string coming from SAP to display Chinese characters on the webpage.

    2) Now EXACTLY the same webpage (where chinese characters) are
    displaying fine, gets exported to excel. Surprisingly, few records from
    display just fine in Excel but some of them display junk.

    3) It is an XLS file, not a CSV. The spredasheet has correct character
    encoding.

    If I could attach the screenshots, I could have explained it better.

    Chanda.
    , Feb 25, 2005
    #7
  8. Joerg Jooss Guest

    wrote:

    > Well this is what is confusing me.
    > 1) I get Chinese characters from SAP. They don't get displayed fine on
    > ASP.NET web pages. So I have to use TSCUnicode function to convert the
    > string coming from SAP to display Chinese characters on the webpage.


    How do you receive the characters from SAP? As raw bytes? As .NET
    strings?

    Cheers,
    --
    http://www.joergjooss.de
    mailto:
    Joerg Jooss, Feb 25, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    564
  2. omegaman
    Replies:
    1
    Views:
    555
    omegaman
    Sep 21, 2004
  3. zxo102
    Replies:
    1
    Views:
    557
    John Machin
    Aug 15, 2005
  4. Exporting to Excel with Chinese Characters

    , Oct 15, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    280
  5. sed_y
    Replies:
    0
    Views:
    997
    sed_y
    Feb 15, 2012
Loading...

Share This Page