返回列表 上一主題 發帖

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

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

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

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

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

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

TOP

回復 12# HSIEN6001
試試看

IE下載網頁.rar (93.79 KB)

TOP

回復 9# Hsieh
回復 10# GBKEE

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

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

test.rar (56.17 KB)

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

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

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

本帖最後由 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

本帖最後由 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

回復 5# linshin1999


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

TOP

        靜思自在 : 【時日莫空過】一個人在世間做了多少事,就等於壽命有多長。因此必須與時間競爭,切莫使時日空過。
返回列表 上一主題