2009-04-16 [.NET VB] 上傳CSV格式檔案並寫入資料庫 8276 0 .Net VB 摘要:[.NET VB] 上傳CSV格式檔案並寫入資料庫 這篇又是很久以前寫的囉~記錄記錄Dim File As HttpPostedFile = FileUpload.PostedFile Dim path As String = "" Dim FileName As String = "" Dim FileExtName As String = "" msgLabel.Visible = False If File.ContentLength = 0 Then msgLabel.Text = Resources.language.Msg_UploadFileError msgLabel.Visible = True Exit Sub Else Dim FileSplit() As String = Split(File.FileName, "\") FileName = FileSplit(FileSplit.Length - 1) Dim FileExtSplit() As String = Split(FileName, ".") FileExtName = FileExtSplit(FileExtSplit.Length - 1) If FileExtName <> "csv" Then msgLabel.Text = Resources.language.Msg_UploadFileExtNameError msgLabel.Visible = True Exit Sub Else path = Server.MapPath("~/Excel/" & FileName) File.SaveAs(path) File = Nothing Dim dp_title As String Dim dp_descs As String Dim fs As System.IO.FileStream = New IO.FileStream(path,System.IO.FileMode.Open, System.IO.FileAccess.Read) Dim sr As New IO.StreamReader(fs, New System.Text.UnicodeEncoding()) Dim sr_ReadLine() As String = sr.ReadToEnd.Split(vbCrLf) For I As Integer = 0 To sr_ReadLine.Length - 2 If I > 0 Then Dim StrFile_Array() As String = sr_ReadLine(I).Split(",") dp_title = StrFile_Array(0) If StrFile_Array(1).Trim = "" Then dp_descs = "" Else dp_descs = StrFile_Array(1).Trim End If Dim dp_sqno As Int32 = ImportManager.getDepartmentSqno(dp_title) If dp_sqno = 0 Then DepartmentManager.AddDepartment(dp_title, dp_descs, Now()) Else DepartmentManager.updateDepartment(dp_sqno, dp_title, dp_descs, Now()) End If End If Next sr.Dispose() sr.Close() End If End If ASP.NETVBCSV 回首頁