Board logo

標題: 使用 Line Input #1, mystr 中文字會變成亂碼,請問有方法克服嗎? [打印本頁]

作者: linshin1999    時間: 2011-3-19 16:39     標題: 使用 Line Input #1, mystr 中文字會變成亂碼,請問有方法克服嗎?

請問各位有遇到過這樣的問題嗎?

Open myfile For Input as #1
       Line input #1,  mystr
Close #1

若 mystr 有中文碼,都會變成一連串的?號,請問可以克服嗎?

作者: Hsieh    時間: 2011-3-19 16:54

回復 1# linshin1999


    不知道你寫入在甚麼地方
用即時運算視窗得到答案,很正常的
[attach]5062[/attach]
作者: linshin1999    時間: 2011-3-19 17:05

Hsieh板主:

我的文字檔是從VB產生出來的,從記事本中可以看到完整的文字檔,但是從Excel讀進來遇到中文字會變成亂碼.

myinx = 0
Open muti_f For Input As #1
        Do Until EOF(1)
            Line Input #1, mystr
            myinx = myinx +1
            mylist(myinx) = mystr
            muti_count = muti_count + 1
        Loop
    Close #1

會是設定的問題嗎?

作者: Hsieh    時間: 2011-3-19 17:49

回復 3# linshin1999

你的TXT編碼是Unicode吧
轉成ANSI編碼試試看
作者: linshin1999    時間: 2011-3-19 18:11

Hsieh板主你好:

不知道VB中的編碼的方式為何,但是如果是Unicode的話,如何轉換成Ansi 呢?

作者: Hsieh    時間: 2011-3-19 18:13

回復 5# linshin1999


    [attach]5063[/attach]
作者: linshin1999    時間: 2011-3-19 19:00

本帖最後由 linshin1999 於 2011-3-19 20:18 編輯

回復 6# Hsieh
Hsieh板主你好:

問題解決了,我的VB原始設定是(UTF-8),雖然這樣可以但是是不是每次都要做這樣的轉換呢?
另外在 Line Input 這邊解決了,但是會在滙入檔案那邊會出問題,這邊適合用(UTF-8),不然會亂碼.匯入檔案的程式如下:

With ActiveSheet.QueryTables.Add(Connection:=input_file, Destination:=Range("$A$1"))
        .Name = "表頭"
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SaveData = True
        .AdjustColumnWidth = True
        .TextFilePlatform = 65001
        .TextFileStartRow = 1
        .TextFileParseType = xlFixedWidth
        .TextFileColumnDataTypes = Array(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2)
        .TextFileFixedColumnWidths = Array(2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
End With

這一段程式接受(UTF-8),而Line Input那邊則接受(ANSI)麻煩吧,不過總是把問題找出來了,謝謝你!!

另外你是否知道VB可有控制輸出碼的方法嗎?謝謝!!

作者: linshin1999    時間: 2011-3-19 22:59

本帖最後由 linshin1999 於 2011-3-19 23:16 編輯

回復 6# Hsieh

Hsieh板主:

利用 Line Input # 滙入檔案,是不是太老式了,以前好像亂碼困擾很多人,從Office 2007 以後好像被改善了,像用 ActiveSheet.QueryTables.Add 滙進來的檔案就没問題,會用 Line input #, 是因為那是中間檔長度不固定,而且不是要直接丟到 Worksheet 去, 所以用 Line input #, 不知道有没其他方法可以滙入檔案(像讀循序檔那樣), 而且可以接受 UTF-8 的? 謝謝!!

作者: Hsieh    時間: 2011-3-19 23:58

回復 8# linshin1999
  1. Sub OpenTextFileTest()
  2.     Dim fs, f
  3.     Set fs = CreateObject("Scripting.FileSystemObject")
  4.     Set f = fs.getfile("C:\Users\Documents\testfile.txt") 'Unicode 格式檔案
  5.     Set ts = f.openastextstream(1, -1)
  6.     mystr = ts.readall '讀取Unicode 格式檔案
  7.     ts.Close
  8.     Set f = fs.getfile("C:\Users\Hsieh\Documents\testfile1.txt") 'ASCII 格式檔案
  9.     Set ts = f.openastextstream(2, 0)
  10.     ts.Write mystr '寫入ASCII 格式檔案
  11.     ts.Close
  12. End Sub
複製代碼

作者: GBKEE    時間: 2011-3-20 07:09

Sub Ex()
    Application.DisplayAlerts = False
    With Workbooks.Open("D:\Test\文件.txt") '開啟 Unicode 格式檔案
            .SaveAs Filename:="D:\Test\Test.txt", FileFormat:=xlText
            '另存為 ASCII 格式檔案
            .Close
    End With
    Application.DisplayAlerts = True
End Sub
作者: linshin1999    時間: 2011-3-21 17:46

回復 9# Hsieh

Hsieh 版主你好:

Excel中雖然有 .openastextstream(1, -1) 的功能, 但是好像不起作用,還是必須從來源檔案下手,我在 VB 將原來的程式碼
Using out_sw As StreamWriter = New StreamWriter(type_f)
改成這樣就可以了
Using out_sw As StreamWriter = New StreamWriter(type_f, True, System.Text.Encoding.ASCII)

作者: HSIEN6001    時間: 2012-7-8 12:20

回復 9# Hsieh
回復 10# GBKEE

查了很多相關資訊,功力太差
一直無法解決,只好麻煩版大幫我看看

請問
該如何設定,才可以排解亂碼問題

[attach]11615[/attach]
作者: GBKEE    時間: 2012-7-8 15:57

回復 12# HSIEN6001
試試看

[attach]11617[/attach]
作者: HSIEN6001    時間: 2012-7-8 19:14

回復 13# GBKEE

謝謝G大幫忙
測試OK!
google一下 getElementsByTagName

http://itgroup.blueshop.com.tw/ann71727/ann71727?n=convew&i=4629  
http://renren.it/a/w3school/htmldom/met_doc_getelementsbytagname.html

好深奧~還是看不懂
外行  O_Q




歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)