导入excel到vb.net中的gridview [英] import excel to gridview in vb.net
本文介绍了导入excel到vb.net中的gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨我正在将excel导出到gridview,但是问题是:
他的Microsoft Office Access数据库引擎无法找到对象'Products $'。确保对象存在,并正确拼写其名称和路径名。
my代码是
hi i am exporting excel to gridview but problem is
he Microsoft Office Access database engine could not find the object 'Products$'. Make sure the object exists and that you spell its name and the path name correctly.
my code is
Protected Sub btnImport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnImport.Click
Dim connString As String = ""
Dim strFileType As String = Path.GetExtension(fileuploadExcel.FileName).ToLower()
Dim path__1 As String = fileuploadExcel.PostedFile.FileName
'Connection String to Excel Workbook
If strFileType.Trim() = ".xls" Then
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & path__1 & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=2"""
ElseIf strFileType.Trim() = ".xlsx" Then
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & path__1 & ";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=2"""
End If
Dim query As String = "SELECT SLNo,TagName,Description,Remarks FROM [Products$]"
Dim conn As New OleDbConnection(connString)
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
Dim cmd As New OleDbCommand(query, conn)
Dim da As New OleDbDataAdapter(cmd)
Dim ds As New DataSet()
da.Fill(ds)
grvExcelData.DataSource = ds.Tables(0)
grvExcelData.DataBind()
da.Dispose()
conn.Close()
conn.Dispose()
End Sub
我的Excel工作表名称是产品$
可以对此有任何帮助
And my excel sheet name is Products$
can any help regarding this
推荐答案
'。确保对象存在,并正确拼写其名称和路径名。
my代码是
'. Make sure the object exists and that you spell its name and the path name correctly.
my code is
Protected Sub btnImport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnImport.Click
Dim connString As String = ""
Dim strFileType As String = Path.GetExtension(fileuploadExcel.FileName).ToLower()
Dim path__1 As String = fileuploadExcel.PostedFile.FileName
'Connection String to Excel Workbook
If strFileType.Trim() = ".xls" Then
connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & path__1 & ";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=2"""
ElseIf strFileType.Trim() = ".xlsx" Then
connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & path__1 & ";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=2"""
End If
Dim query As String = "SELECT SLNo,TagName,Description,Remarks FROM [Products
Dim conn 作为 新 OleDbConnection(connString)
如果 conn.State = ConnectionState.Closed 那么
conn.Open()
结束 如果
Dim cmd 作为 新 OleDbCommand(查询,conn)
Dim da As 新 OleDbDataAdapter(cmd)
Dim ds 作为 新 DataSet()
da.Fill(ds)
grvExcelData.DataSource = ds.Tables( 0 )
grvExcelData.DataBind()
da.Dispose()
conn.Close()
conn.Dispose()
结束 Sub
" Dim conn As New OleDbConnection(connString) If conn.State = ConnectionState.Closed Then conn.Open() End If Dim cmd As New OleDbCommand(query, conn) Dim da As New OleDbDataAdapter(cmd) Dim ds As New DataSet() da.Fill(ds) grvExcelData.DataSource = ds.Tables(0) grvExcelData.DataBind() da.Dispose() conn.Close() conn.Dispose() End Sub
我的Excel工作表名称是产品
And my excel sheet name is Products
可以对此有任何帮助
can any help regarding this
这篇关于导入excel到vb.net中的gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文