3

I have been trying to print some chinese text in to an excel file . When i put some general english text its printing correct . But when i try to put some chinese text it printing '?' in the file .

Please let me know whats wrong in the below code

Public WithEvents cmnSaveSave As System.Windows.Forms.SaveFileDialog
Dim PANEL_DETAILS_COL1 As String = "完成的信息"
Dim FileNum as Integer

cmnSaveSave.Filter = I18N(CStr(63), "Excel Documents(*.Xls)|*.Xls")
Dim myDlgResult As System.Windows.Forms.DialogResult  
        myDlgResult = cmnSaveSave.ShowDialog()  '

cmnSaveSave.FileName = "c:\"
FileOpen(FileNum, cmnSaveSave.FileName, OpenMode.Output)
FileNum = FreeFile()

PrintLine(FileNum,  PANEL_DETAILS_COL1)
Viku
  • 2,845
  • 4
  • 35
  • 63
  • 2
    you may want to look at [What does it mean when my text is displayed as Question Marks?](http://stackoverflow.com/q/217237/2145211) – Harrison Nov 03 '13 at 19:09
  • Thanks for the link . i got it because of some character encoding i am getting this . can you please give some solution to the above . i am new to programming . – Viku Nov 03 '13 at 19:16

1 Answers1

4
   PrintLine(FileNum,  PANEL_DETAILS_COL1)

FileOpen and PrintLine are ancient VB subroutines that were last used in VB6. They are only included in VB.NET to help port old code. They use Encoding.Default to encode text, that's going to produce ? question marks on most machines in the world.

You'll need to upgrade this code, use the StreamWriter class instead. It uses utf-8 encoding by default, an encoding that has no trouble with Chinese characters and doesn't depend on the machine configuration.

Using sw = new StreamWriter(cmnSaveSave.FileName)
    sw.WriteLine(PANEL_DETAILS_COL1)
End Using
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • can you please tell me the same with printline(..) . There are lots of code which has been written with printline () in my project . So if i have to go as per your solution i have to make lots of changes..Thanks in advance.. – Viku Nov 03 '13 at 19:26
  • There is lots of code that writes text using ASCII encoding or with the characters in the machine's default code page. Look through the window of your apartment or office, do you see any signs written in Chinese? The odds that an Indian machine is configured to use a Chinese code page are very close to zero. Run your code on a Chinese machine and you'll have better odds for a readable file. This kind of random outcome based on the location of the machine is a very good reason to get rid of the legacy code and use StreamWriter instead. – Hans Passant Nov 03 '13 at 19:31