vb怎么讀取有中文字符的txt規則文件
vb如何讀取有中文字符的txt規則文件
txt文件如下:是規則的文件,但有中文字符後,每行的LEN長度不一樣
1 7EIACOA6E05Z0 集成塊 Coach 6E IC DSC & MP4 PC 34.3200 100.00
2 7EIU78F9A14N3 集成塊 IC MCU 8Bit SSOP20"NEC" PC 3.8600 100.00
3 7EMC1800001G3 鏡頭加工組件Lens Module For180 PC 50.4500 100.00
4 7EMLTD015THT6 Module 1.5"TFT Toppoly w/built PC 51.4800 100.00
在vb中讀這個文檔,然後通過循環取出第一行後將值附給txt,txt(1)為文本框
txt(1)=Trim(Mid$(txt,4, 16))
txt(2)=Trim(Mid$(txt,20, 28))
txt(3)=Trim(Mid$(txt,48, 7))
執行後開始兩行沒有問題,但第三行就亂掉了,後來發現是文檔中的第二列有不同的中文,用len測試時,長度不同
len("集成塊 Coach 6E IC DSC & MP4 ")
len("鏡頭加工組件Lens Module For180 PC ")
我該怎麼取數據呢?
------解决方案--------------------
很简单的:
dim v as variant
v=split(txt," ")
n=ubound(v)
然后 v(0),v(1), ... v(n) 就是分离出来的每个项。
------解决方案--------------------
从两边按空格进行切割
txt文件如下:是規則的文件,但有中文字符後,每行的LEN長度不一樣
1 7EIACOA6E05Z0 集成塊 Coach 6E IC DSC & MP4 PC 34.3200 100.00
2 7EIU78F9A14N3 集成塊 IC MCU 8Bit SSOP20"NEC" PC 3.8600 100.00
3 7EMC1800001G3 鏡頭加工組件Lens Module For180 PC 50.4500 100.00
4 7EMLTD015THT6 Module 1.5"TFT Toppoly w/built PC 51.4800 100.00
在vb中讀這個文檔,然後通過循環取出第一行後將值附給txt,txt(1)為文本框
txt(1)=Trim(Mid$(txt,4, 16))
txt(2)=Trim(Mid$(txt,20, 28))
txt(3)=Trim(Mid$(txt,48, 7))
執行後開始兩行沒有問題,但第三行就亂掉了,後來發現是文檔中的第二列有不同的中文,用len測試時,長度不同
len("集成塊 Coach 6E IC DSC & MP4 ")
len("鏡頭加工組件Lens Module For180 PC ")
我該怎麼取數據呢?
------解决方案--------------------
很简单的:
dim v as variant
v=split(txt," ")
n=ubound(v)
然后 v(0),v(1), ... v(n) 就是分离出来的每个项。
------解决方案--------------------
从两边按空格进行切割
- VB code
Option Explicit Sub Main() Parse " 1 7EIACOA6E05Z0 集成塊 Coach 6E IC DSC & MP4 PC 34.3200 100.00 " Parse " 2 7EIU78F9A14N3 集成塊 IC MCU 8Bit SSOP20""NEC"" PC 3.8600 100.00 " Parse " 3 7EMC1800001G3 鏡頭加工組件Lens Module For180 PC 50.4500 100.00 " Parse " 4 7EMLTD015THT6 Module 1.5""TFT Toppoly w/built PC 51.4800 100.00 " End Sub Sub Parse(ByVal s As String) Dim txt(4) As String txt(0) = CutLeft(s) txt(1) = CutLeft(s) txt(4) = CutRight(s) txt(3) = CutRight(s) txt(2) = Trim$(s) Debug.Print Join(txt, " | ") End Sub Function CutLeft(ByRef s As String) As String Dim i As Long s = Trim$(s) i = InStr(1, s, " ") If i <> 0 Then CutLeft = Left(s, i - 1) s = Mid$(s, i + 1) Else CutLeft = s s = vbNullString End If End Function Function CutRight(ByRef s As String) As String Dim i As Long s = Trim$(s) i = InStrRev(s, " ") If i <> 0 Then CutRight = Mid$(s, i + 1) s = Left$(s, i - 1) Else CutRight = s s = vbNullString End If End Function
------解决方案--------------------
每一行最多四项(代号,品名,总价和单价)?最后两项之间确定只有一个空格?
dim wrkstr() as string
dim txtstr as string
dim tmpstr as string
dim i as integer
wrkstr()=split(txtstr," ")
for i=1 to ubound(wrkstr)-2
tmpstr=tmpstr & space(1) & wrkstr(i)
next i
tmpstr=trim(tmpstr)
txt(0).text=wrkstr(0)
txt(1).text=tmpstr
txt(2).text=wrkstr(ubound(wrkstr)-1)
txt(3).text=wrkstr(ubound(wrkstr))