在不先打开Excel的情况下无法读取excel(XLS)文件

时间:2019-11-09 15:43:48

标签: excel vb.net

我从网页上下载了Excel 2003格式的XLS文件(格式没有选择),并且需要提取数据。我应该能够以数据库的形式读取内容,而无需先打开它,但是如果不先打开它,就不会让我读取它!

由于如果我打开了它的2003 xls文件,它会发出警告,说它可能已损坏等等,我需要确认文件的打开!然后,我单击读取的数据,并在我的datagridview中很好地显示了它!

Imports System.Data.SqlClient
Imports System.Data
Imports System.Data.OleDb
Imports Microsoft.Office.Interop.Excel  
Try

            Dim MyConnection As System.Data.OleDb.OleDbConnection
            Dim dataSet As System.Data.DataSet
            Dim datareader As OleDbDataReader
            Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
            Dim path As String = "C:\holding\list_demands.xls"
            MyConnection = New 
System.Data.OleDb.OleDbConnection("Provider=Microsoft.ace.OLEDB.12.0;Data 
Source=" + path + ";Excel 8.0 XML;HDR=YES;Format=xls")
        MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from 
[List_demands$]", MyConnection)
            MyConnection.Open()
            dataSet = New System.Data.DataSet
            MyCommand.Fill(dataSet)
            DataGridView1.DataSource = dataSet.Tables(0)

            MyConnection.Close()
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try

0 个答案:

没有答案