关于ole显示excel 不同sheet的问题
答案:2 悬赏:30 手机版
解决时间 2021-08-20 18:07
- 提问者网友:精神病院里
- 2021-08-20 12:25
我在ole控件插入一个已经存在的xls文件,文件里有多个表格,我想用一个按钮进行切换,代码该如何写,之前用了object.sheets().select 结果不能正确的显示出来
最佳答案
- 五星知识达人网友:污到你湿
- 2021-08-20 12:58
Private Function readExcel(ByVal index As Integer) As DataTable
Dim ExcelConn As New OleDbConnection
Try
Dim connstr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & 你的xls文件路径 & ";Extended Properties=""Excel 8.0;HDR=YES;"""
ExcelConn.ConnectionString = connstr
ExcelConn.Open()
Dim dt As DataTable = ExcelConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
If dt Is Nothing Then
arrayListlog.Add("Error on readExcel : SchemaGuid can not read.")
End If
Dim dr As DataRow = dt.Rows(0)
Dim sheetName As String = dr("TABLE_NAME").ToString()
Dim cmdText As String = "Select * from [$" & sheetName & "]"
Dim excelAdpt As New OleDbDataAdapter(cmdText, connstr)
Dim ds As New DataSet
excelAdpt.Fill(ds)
Return ds.Tables(0)
Catch ex As Exception
ableToUpload = False
arrayListlog.Add("Error on readExcel : " & ex.Message)
Return Nothing
Finally
ExcelConn.Close()
ExcelConn.Dispose()
End Try
End Function
sheetName 可以改的。通过更改dt.rows(index) <- index 就是sheet的序号
在这里放的是。。。 Dim dr As DataRow = dt.Rows(0) 。。。也就是第一个sheet 这个function 可以return 一个 datatable
Dim ExcelConn As New OleDbConnection
Try
Dim connstr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & 你的xls文件路径 & ";Extended Properties=""Excel 8.0;HDR=YES;"""
ExcelConn.ConnectionString = connstr
ExcelConn.Open()
Dim dt As DataTable = ExcelConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
If dt Is Nothing Then
arrayListlog.Add("Error on readExcel : SchemaGuid can not read.")
End If
Dim dr As DataRow = dt.Rows(0)
Dim sheetName As String = dr("TABLE_NAME").ToString()
Dim cmdText As String = "Select * from [$" & sheetName & "]"
Dim excelAdpt As New OleDbDataAdapter(cmdText, connstr)
Dim ds As New DataSet
excelAdpt.Fill(ds)
Return ds.Tables(0)
Catch ex As Exception
ableToUpload = False
arrayListlog.Add("Error on readExcel : " & ex.Message)
Return Nothing
Finally
ExcelConn.Close()
ExcelConn.Dispose()
End Try
End Function
sheetName 可以改的。通过更改dt.rows(index) <- index 就是sheet的序号
在这里放的是。。。 Dim dr As DataRow = dt.Rows(0) 。。。也就是第一个sheet 这个function 可以return 一个 datatable
全部回答
- 1楼网友:末日狂欢
- 2021-08-20 14:31
什么问题?请详细描述。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯