返回列表 上一主題 發帖

使用 Line Input #1, mystr 中文字會變成亂碼,請問有方法克服嗎?

使用 Line Input #1, mystr 中文字會變成亂碼,請問有方法克服嗎?

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

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

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

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

會是設定的問題嗎?
V98

TOP

Hsieh板主你好:

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

TOP

本帖最後由 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可有控制輸出碼的方法嗎?謝謝!!
V98

TOP

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

回復 6# Hsieh

Hsieh板主:

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

TOP

回復 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)
V98

TOP

        靜思自在 : 原諒別人就是善待自己。
返回列表 上一主題