我試圖從Excel中的一些數據如下:閱讀Excel中的.NET - 一些列讀取爲空
If (FileUpload1.PostedFile.ContentType = "application/vnd.ms-excel") Then
Dim filename As String = Path.GetFileName(FileUpload1.FileName)
'Session("userid") & "-" & Date.Now()
filepath = "\excel\" & Session("userid") & "_" & Now.Date().ToString("Mdy") & "_" & filename
FileUpload1.SaveAs(Server.MapPath("~/") & filepath)
ReadExcel(filepath)
Else
StatusLabel.Text = "Only Excel file types are accepted"
End If
似乎一切都很好,除了當其中一列,有時讀作爲NULL。如果它是不同類型的,就會發生這種情況。 我無法弄清楚它是什麼。有人幫我請....
Sub ReadExcel(ByVal filepath As String)
Dim MyConnection As System.Data.OleDb.OleDbConnection
Dim DtSet As System.Data.DataSet
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & Server.MapPath("~/") & filepath & "';Extended Properties=Excel 8.0;")
DtSet = New System.Data.DataSet
Try
MyCommand.Fill(DtSet)
'gwResults.DataSource = DtSet.Tables(0)
LoopSources(DtSet)
'gwResults.DataBind()
MyConnection.Close()
Catch ex As Exception
StatusLabel.Text = "Import Excel status: The file could not be loaded. The following error occured: <br>" + ex.Message
End Try
End Sub
新的連接字符串:MyConnection = New System.Data.OleDb.OleDbConnection(「provider = Microsoft.Jet.OLEDB.4.0; Data Source ='」&Server.MapPath(「〜/」)&filepath & "'; Extended Properties = 'Excel 8.0; HDR = Yes; IMEX = 1;'「)現在可以工作了!謝謝!!! –
沒問題,我覺得你很痛苦,因爲前段時間我不得不想出這個問題,但是,如果你真的想要感謝我,你可以隨時將你的問題標記爲答案:)快樂的編碼! – mattmc3
我一直在和DBNull作鬥爭,謝謝! –