Link to home
Start Free TrialLog in
Avatar of duncanb7
duncanb7

asked on

Chinese character in csv file with javascript code

Dear Expert,

I get one csv file in which has some chinese character and use following javascript code
to form a table. the table is displayed for number and Engish value is fine but it doesn't
display correct chinese character according to the cells postion in csv file. it display a strange output on
some cells. I heard people said javascript is not able to handle Chinese character or
other country character. Is it True? Is ther any method to overcome this?

Whether I need to convert the chinese character into unicode first and then put it back int  csv file before
any javascript processing ?
http://code.cside.com/3rdpage/us/unicode/converter.html
Please advise

Duncan
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript">
	function getData( url ) 
	{ 
		var xmlhttp;
		
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
				renderData( xmlhttp.responseText );
			
			}
		}
		xmlhttp.open("GET","postcodes.csv",true);
		xmlhttp.send();
	}

	function renderData( data )
	{
		var dataRows = data.split( "\n" );
		var table = document.createElement("Table");
		table.border = 0.5;
		
		for( i = 0; i < dataRows.length; i++ )
		{
			var tableRow = table.insertRow(i);
			var dataCells = dataRows[i].split(",");
			for( j = 0; j < dataCells.length; j++ )
			{
				var tableCell= tableRow.insertCell(j);      
				tableCell.innerHTML = dataCells[j];
			}
		}
		document.body.appendChild( table );
	} 

	getData();

</script>
</head>
<body>
</body>
</html> 
Toggle HighlightingOpen in New WindowSelect AllAccept Multiple Solutions Accept as Solution

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of jimyX
jimyX

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of duncanb7
duncanb7

ASKER

I goolge it for whole day, finally get the method to convert csv file into UFT-8 in vba and
find the linke at
http://code.cside.com/3rdpage/us/unicode/converter.htmlSub csvtoUTF8()
the book9.csv(after encode)  is still not matched from note++ to the link conversion above.
What is &#  stand for, it mean UTF code  ?
What is \u  stand for,  it means  classical Unicode ?

Question-1, I use notepad to view the conversion file of boo9.csv with option to UTF8
 the hex code in notepad  is different from hex code with &# and \u , Why ?
QUestion-2   Since by question-1 problem, so I try to convert one character  from
the link, and then use decodeURIComponent( escape(&#xxxx ) ) it works and display right character in javascript
but decodeURIComponent( escape(\uxxxxx ) ) is not displayed the correct character,
What is stand for HTML Unicode and Java Unicode in the link ?
Question-3, SHould I set <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
in my javascript ?


VBA code for book8.csv convert to book9.csv in UTF-8
===========================================
Dim sfol As String
Dim dfol As String
sfol = ActiveWorkbook.path & "\"
dfol = sfol
Call CopyFile2File(sfol, "book8.csv", dfol, "book9.csv")
Call Encode(sfol & "book9.csv", "UTF-8")

End Sub
Sub Encode(ByVal sPath$, Optional SetChar$ = "UTF-8")
With CreateObject("ADODB.Stream")
.Open
.LoadFromFile sPath ' Loads a File
.Charset = SetChar ' sets stream encoding (UTF-8)
.SaveToFile sPath, 2 ' adSaveCreateOverWrite
.Close
End With
End Sub
RE-send it for typing mistake , I goolge it for whole day, finally get the method to convert csv file into UFT-8 in vba and
find the linke at
http://code.cside.com/3rdpage/us/unicode/converter.html
the book9.csv(after encode)  is still not matched from note++ to the link conversion above.
What is &#  stand for, it mean UTF code  ?
What is \u  stand for,  it means  classical Unicode ?

Question-1, I use notepad to view the conversion file of boo9.csv with option to UTF8
 the hex code in notepad  is different from hex code with &# and \u , Why ?
QUestion-2   Since by question-1 problem, so I try to convert one character  from
the link, and then use decodeURIComponent( escape(&#xxxx ) ) it works and display right character in javascript
but decodeURIComponent( escape(\uxxxxx ) ) is not displayed the correct character,
What is stand for HTML Unicode and Java Unicode in the link ?
Question-3, SHould I set <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
in my javascript ?


VBA code for book8.csv convert to book9.csv in UTF-8
===========================================
Sub csvtoUTF8()

Dim sfol As String
Dim dfol As String
sfol = ActiveWorkbook.path & "\"
dfol = sfol
Call CopyFile2File(sfol, "book8.csv", dfol, "book9.csv")
Call Encode(sfol & "book9.csv", "UTF-8")

End Sub
Sub Encode(ByVal sPath$, Optional SetChar$ = "UTF-8")
With CreateObject("ADODB.Stream")
.Open
.LoadFromFile sPath ' Loads a File
.Charset = SetChar ' sets stream encoding (UTF-8)
.SaveToFile sPath, 2 ' adSaveCreateOverWrite
.Close
End With
End Sub
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I read it before, and now I understand &#aaaa, where a is for decimal code, \ubbbb, where b is for hex code
the question how I convert it to &# or \u  in vba  so, the text file will
be

After new conversion, book9.csv will be like this
=================================
Hello, &#23980;&#21110;&#21340, good, day,
1,2,3,4,4
5,6,7,8

================================

In other words, I use above function of csvtoUTF8() is incorrect. ?
But how to convert chinese charter to &#aaaaa in vba ?
Finally, get the function to convert the chinese character to Unicode

http://www.blog.highub.com/javascript/javascript-advanced/convert-chinese-characters-to-unicode/
Sorry it is in javascript not in vba

Could you help provide a code for Vba
No. Sorry. I added the VBA zone for you
Thanks,

I think I could do VBA code accroding the javascript code provide by myself
but others can reply it, it is really welecom and thanks

Duncan
Thanks for your reply

If I put the &# Unciode in csv file
like this, it display back all
chinese character by Javascript
Hello,&#23980;&#21110;&#21340
good, day
1,2,3,4,4
5,6,7,8