返回列表 上一主題 發帖

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

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

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

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

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

回復 1# linshin1999


    不知道你寫入在甚麼地方
用即時運算視窗得到答案,很正常的
學海無涯_不恥下問

TOP

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

回復 3# linshin1999

你的TXT編碼是Unicode吧
轉成ANSI編碼試試看
學海無涯_不恥下問

TOP

Hsieh板主你好:

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

TOP

回復 5# linshin1999


    test.gif
學海無涯_不恥下問

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

回復 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
複製代碼
學海無涯_不恥下問

TOP

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

TOP

        靜思自在 : 口說一句好話,如口出蓮花;口說一句壞話如口吐毒蛇。
返回列表 上一主題