打開選擇/保存文件可以自己選擇,可以先引用Microsoft Comm Dialgo Control 6.0控件
下面是打開文件選擇框
private?strData?as?string'提取的數據Private?Sub?Command1_Click()'選擇文件
On?Error?GoTo?err
CommonDialog1.FileName?=?""
CommonDialog1.Filter?=?"*.mca|*.mca|*.*|*.*"
CommonDialog1.CancelError?=?True
CommonDialog1.ShowOpen'打開文件選擇對話框
If?CommonDialog1.FileName?<>?""?Then
Text1.Text=?CommonDialog1.FileName'選擇的文件名
GetData(Text1.Text)'提取數據
End?If
err:
End?Sub
下面是保存文件對話框
Private?Sub?Command2_Click()On?Error?GoTo?err
CommonDialog1.FileName?=?""
CommonDialog1.Filter?=?"*.mca|*.mca|*.*|*.*"
CommonDialog1.CancelError?=?True
CommonDialog1.ShowSave'打開保存文件對話框
If?CommonDialog1.FileName?<>?""?Then
Text2.Text=?CommonDialog1.FileName'保存的文件名
'以下保存文件代碼
dim?intFNo?as?long
open?Text2.Text?for?Output?as?#intFno
print?#intFno,strD?'保存到文件?
Close?#intFno
End?If
err:
End?Sub
提取文本資料代碼:
private?sub?GetData(byval?strFile?as?string)dim?intFNo?as?long
dim?strD?as?string
dim?isData?as?Boolen
dim?lngIndex?as?Long?
strData?=space(100000)'提前申請內存,要大於最大的數據量的2倍,因為數據後在有加分號(;)
intFno=freefile
open?strfile?for?Input?as?#intFno
lngIndex?=1
do?while?Not?Eof(intFno)
line?input?#intFno,strD
if?isData?then
if?strD<>"<<END>>"?then
mid(strData?,lngIndex?,len(Trim(strd))+1)=trim(strD)?&?";"'提取數據
'數據格式為:data;data;data;data;
'如果要轉換成數據:bytd=split(strdata,";")
lngIndex?=lngIndex?+len(Trim(strd))+1
else
msgbox?"數據提取完成,請保存"
exit?do
end?if
else
if?strD="<<Data>>"?then
isData=True
end?if
end?if?
Loop
close?#intFno
end?sub