VB.NET使用OLEDB(使用SQL Where子句)读取Excel文件. [英] VB.NET Read an Excel File using OLEDB (Using SQL Where Clause).

查看:714
本文介绍了VB.NET使用OLEDB(使用SQL Where子句)读取Excel文件.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了使用OLEDB读取Excel文件的代码.
在VB.NET 2005中使用OLEDB从Excel读取数据 [

I found a code for reading a Excel file using OLEDB.
Read Data from Excel using OLEDB in VB.NET 2005 [^]

Try
    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='c:\testfile.xls'; " _
     "Extended Properties=Excel 8.0;")
    MyCommand = New System.Data.OleDb.OleDbDataAdapter _
        ("select * from [Sheet1$]", MyConnection)
    MyCommand.TableMappings.Add("Table", "TestTable")
    DtSet = New System.Data.DataSet
    MyCommand.Fill(DtSet)
    DataGridView1.DataSource = DtSet.Tables(0)
    MyConnection.Close()
Catch ex As Exception
    MsgBox(ex.ToString)
End Try



上面的代码显示Excel文件中的所有数据.我的问题是如何在SQL命令中放置Where语句.我只是尝试使用标题,但无法使用.



The above code displays all data from Excel File. My question is how to put the Where Statement in the sql command. I just try using the header but it doesn''t work.

推荐答案

",MyConnection) MyCommand.TableMappings.Add(" " TestTable") DtSet = 新建 System.Data.DataSet MyCommand.Fill(DtSet) DataGridView1.DataSource = DtSet.Tables( 0 ) MyConnection.Close() 捕获,例如 As 异常 MsgBox(例如ToString) 结束 尝试
", MyConnection) MyCommand.TableMappings.Add("Table", "TestTable") DtSet = New System.Data.DataSet MyCommand.Fill(DtSet) DataGridView1.DataSource = DtSet.Tables(0) MyConnection.Close() Catch ex As Exception MsgBox(ex.ToString) End Try



上面的代码显示Excel文件中的所有数据.我的问题是如何在SQL命令中放置Where语句.我只是尝试使用标题,但它不起作用.



The above code displays all data from Excel File. My question is how to put the Where Statement in the sql command. I just try using the header but it doesn''t work.


您应该可以使用Select
You should be able to use the Select method[^] to filter out records.


这篇关于VB.NET使用OLEDB(使用SQL Where子句)读取Excel文件.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆