我有一个文件上传和一个在GridView中加载该文件的功能。当我选择一个excel文件并单击上传按钮时,文件上传到目录但在加载时出现以下错误。对于.txt文件,它工作正常
Private Function UploadCSVFile() As String
Try
If fileCSV.HasFile Then
strFileName = fileCSV.FileName
hdnFileName.Value = fileCSV.FileName
fileCSV.SaveAs(Server.MapPath(strDirectory) & strFileName)
Call LoadData(strFileName) // Gives error in this function
lblImportMsg.Text = "File saved succesfully!"
End If
Return strFileName
Catch ex As Exception
lblImportMsg.Text = ex.ToString
Return "None"
End Try
End Function
Private Sub LoadData(ByVal strFileName As String)
If Not strFileName = "" Then
ds = New DataSet
Dim strFilePath As String = Server.MapPath(strDirectory) '& strFileName
Dim cnn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFilePath & ";Extended Properties=Text;")
Dim da As New OleDb.OleDbDataAdapter("Select * from [" & strFileName & "]", cnn)
da.Fill(ds)
gvFileInfo.DataSource = ds.Tables(0)
gvFileInfo.DataBind()
End If
End Sub
答案 0 :(得分:0)
xls文件可以解释为文本文件,如csv文件(带逗号的文本)。 但XLSX与众不同。如果你用.zip替换.xlsx,你会看到(winzip,winrar。atc)它有很多内部文件......它是Office Open XML SpreadsheetML文件格式。
https://msdn.microsoft.com/en-us/library/dd922181(v=office.12).aspx
要打开XLSX文件,请尝试另一个连接字符串,如下所示:
Dim cnn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Path_to_your_file.xlsx;Extended Properties='Excel 12.0 Xml;HDR=YES;IMEX=1'")
Dim ds As New DataSet
Dim da As New OleDb.OleDbDataAdapter("Select * from [Sheet1$]", cnn)
da.Fill(ds)