大家好这是我写的词典。很简单。我初学VB.NET请大家帮忙改进
下面有源代码
Imports System.Data.OleDb Imports System.Net.Mime.MediaTypeNames Public Class Form1 Dim excelapp As System.Windows.Forms.Application '声明一个application对象 Dim excelworkbook As Excel.Workbook '声明一个工作簿对象 Dim excelsheet As Excel.Worksheet '声明一个工作表对象 Dim excelrange As Excel.Range '声明一个范围对象 Public Shared keywords As String '查询关键字 Public Shared len As Integer Public Sub TextBox1_TextChanged(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles TextBox1.TextChanged keywords = TextBox1.Text DataGridView1.DataSource = GetExcelToDataTableBySheet("E:\coperator\KXL.xlsx","Sheet1$") End Sub Public Shared Function GetExcelToDataTableBySheet(ByVal FileFullPath As String,ByVal SheetName As String) As DataTable 'string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + FileFullPath + ";Extended Properties='Excel 8.0; HDR=NO; IMEX=1'"; //此连接只能操作Excel2007之前(.xls)文件 Dim strConn As String = ("Provider=Microsoft.Ace.OleDb.12.0;" & "data source=") + FileFullPath & ";Extended Properties='Excel 12.0; HDR=NO; IMEX=1'" '此连接可以操作.xls与.xlsx文件 Dim conn As New OleDbConnection(strConn) conn.open() Dim ds As New DataSet() Dim strsql = "SELECT * FROM [{0}] WHERE F1 LIKE '" & keywords & "%'" & "OR F2 LIKE '" & keywords & "%'" & "OR F3 LIKE '" & keywords & "%'" & "OR F4 LIKE '" & keywords & "%'" Dim odda As New OleDbDataAdapter(String.Format(strsql,SheetName),conn) odda.Fill(ds,SheetName) conn.Close() Return ds.Tables(0) End Function Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click DataGridView1.DataSource = GetExcelToDataTableBySheet("E:\coperator\KXL.xlsx","Sheet1$") End Sub Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Load DataGridView1.DataSource = GetExcelToDataTableBySheet("E:\coperator\KXL.xlsx","Sheet1$") End Sub Private Sub Button2_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button2.Click TextBox1.Text = "" End Sub End Class 请各位大侠赐教。