問題
使用 array[]配合Split方式組字串 讀取csv檔案進行資料匯入處理
解決方法
//取得執行位置 Dim Parent_LogPath As String Parent_LogPath = System.IO.Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).ToString //取得執行位置底下的.csv檔 Dim sr As StreamReader = New StreamReader(Parent_LogPath & "\data\" &"test0520.csv") Dim _str1 As String = "" Dim _str2 As String = "" Dim _str3 As String = "" //將讀出的資料以,分割成陣列 Dim Ary As Array = Split(sr.ReadToEnd(), ",") //取得陣列筆數 Dim _arycount As Integer _arycount = Ary.Length - 1 //宣告用於取得目前位置 Dim _arypoint As Integer //宣告用於取得筆數 Dim excutesub As Integer = 1 //以for迴圈 從第3筆以後開始取值,一次取3筆 For _arypoint = 3 To _arycount Step 3 _str1 = _str1 & Ary(_arypoint) & "," _str2 = _str2 & Ary(_arypoint + 1) & "," _str3 = _str3 & Ary(_arypoint + 2) & "," excutesub = excutesub + 1 //1千筆執行一次,或讀到最後一筆執行一次 If (excutesub = 1000 Or (_arycount - 2) = _arypoint) Then _str1 = _str1.Substring(0, _str1.Length - 1) _str2 = _str2.Substring(0, _str2.Length - 1) _str3 = _str3.Substring(0, _str3.Length - 1) ToWriteLog(_str1, _str2, _str3) //不是最後一筆資料時,休息3分鐘 If (_arycount - 2) == _arypoint Then //調用Imports System.Threading //跑一次後休息3分鐘,1秒=1000 Thread.Sleep(180000) End If End If Next
沒有留言:
張貼留言