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

C

c.verma

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.
 
J

Joerg Jooss

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,
 
C

c.verma

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.
 
C

c.verma

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.
 
J

Joerg Jooss

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,
 
J

Joerg Jooss

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,
 
C

c.verma

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.
 
J

Joerg Jooss

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,
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top