導航:首頁 > 文件教程 > vb中怎麼讀取txt文件

vb中怎麼讀取txt文件

發布時間:2024-07-12 09:30:25

1. vb 怎樣讀取TXT文件中的數據

dim a,b,c,d as integer
dim as,bs,cs,ds as string
Open "TESTFILE" For Input As #1 \' 打開文件。
Do While Not EOF(1) \'
Input #1, as
Input #1, bs
Input #1, cs
Input #1, ds
Loop
Close #1 \' 關閉文件
a=val(mid$(as,3,len(as)-3))
b=val(mid$(bs,3,len(as)-3))
c=val(mid$(cs,3,len(as)-3))
d=val(mid$(ds,3,len(as)-3))
'提取數據給變數

2. vb讀取文件夾中所有的txt文件

使用VB內建函數讀取文本文件

1/3

雙擊Command1添加如下代碼

Private Sub Command1_Click()

    Dim strFile     As String

    Dim intFile     As Integer

    Dim strData     As String

    strFile = "c:\學生成績.txt"

    intFile = FreeFile

    Open strFile For Input As intFile

    strData = StrConv(InputB(FileLen(strFile), intFile), vbUnicode)

    Debug.Print strData

    Close intFile

End Sub

2/3

運行代碼讀取文件

按F8開始單步調試代碼,點擊Command1,進入單步調試功能,多次按下F8或直接按下F5運行完成,就完成了讀取文本文件內容並輸出到立即窗口。

查看剩餘1張圖

3/3

關鍵代碼說明

intFile = FreeFile:獲取一個文件句柄

Open strFile For Input As intFile :打開文件

FileLen(strFile) :獲取文件內容位元組大小

InputB:讀取文件內容位元組流

StrConv:將位元組流轉換為Unicode字元串

Debug.Print strData:將字元串內容輸出到立即窗口

Close intFile:關閉文件句柄

使用FileSystemObject讀取文本文件

1/3

添加Microsoft Scripting Runtime引用

點擊Project菜單,再點擊下拉菜單中的Reference,打開引用對話框,瀏覽找到Microsoft Scripting Runtime引用,選擇後點確定按鈕。

查看剩餘1張圖

2/3

雙擊Command2添加如下代碼

Private Sub Command2_Click()

    Dim objFS

3/3

運行代碼讀取文件

按F8開始單步調試代碼,點擊Command2,進入單步調試功能,多次按下F8或直接按下F5運行完成,就完成了讀取文本文件內容並輸出到立即窗口。

3. 怎麼使用VB打開文本文件

這樣你換一種寫法試一試:
dim x As String
open "g:\雜項\123.txt" for input as #1
DO while not eof(1)
line input #1,x
text1.text = text1.text & x & vbcrlf
loop
close #1
還有一種是:版
open "g:\雜項\123.txt" for input as #1
text1.text = strconv(inputb(lof(2),#1),vbunicode)
close #1
不行的話 再問權我

4. VB如何讀取txt數據

Private
Sub
Command1_Click()
'基本讀取方法---只能讀取非中文
'
Dim
a
As
String,
b
As
String,
c
As
String,
d
As
String,
e
As
String,
f
As
String'定義幾個變數
Dim
FileNo
As
Integer
FileNo
=
FreeFile()
'獲取一個未使用的文件號
Dim
str
As
String
'用來記錄最終的值
Open
"你要讀取文件的完整路徑"
For
Input
As
#FileNo
While
Not
EOF(FileNo)
Dim
str_Read
As
String
Input
#FileNo,
str_Read
'讀取一個字元到變數str_Read---不包含換行回車符,也不包括逗號,分號及Tab,當讀到分隔符號(前面列舉的4種)時就賦值給後面的變數,如果有多個變數就讀取相對應多的分隔數據
'如果為你的007.txt文件且為你列舉的內容那麼上面一句就改成下面的語句
'
Input
#FileNo,
a,
b,
c,
d,
e,
f
'
str
=
str
&
str_Read
Wend
'Me.Text1.Text
=
str
Close
#FileNo
End
Sub

5. 請問VB中如何讀取txt文件的數據

1、新建一個標準的VB EXE工程,只有一個Form,Form上有兩個按鈕:Command1和Command2。

閱讀全文

與vb中怎麼讀取txt文件相關的資料

熱點內容
專題學習網站源碼 瀏覽:163
jsphead什麼 瀏覽:88
gps串口數據怎麼發送 瀏覽:968
win10文件主頁共享查看 瀏覽:411
中國聯通有哪些app是免流的 瀏覽:176
邊做邊保存的文件找不到了 瀏覽:858
win10照片應用文件夾名稱 瀏覽:966
編程如何解決資金的原子性 瀏覽:638
如何製作廣角鏡頭矯正文件 瀏覽:513
在網頁開發中應該選用哪個資料庫 瀏覽:742
iphone5移動卡貼 瀏覽:990
電腦文件的格式 瀏覽:127
extjs的xtype 瀏覽:959
suse11iso文件要u盤安裝 瀏覽:153
如何將報表統計數據轉化為圖形 瀏覽:444
如何寄快遞材料文件 瀏覽:265
java構造方法private 瀏覽:475
手機文件找回恢復 瀏覽:516
word怎麼把u盤里的文件拔掉 瀏覽:976
港版蘋果用的插排 瀏覽:1000

友情鏈接