请各位vb.net大神来帮帮忙~ 我写了一个程序,想用vb.net编个软件能够读出电脑的Excel~
程序参考别人的写出了一部分,但是只能读特定的一个Excel,希望有高手帮我改一下添加个“打开”选项,让我可以读电脑里任意一个Excel!程序如下,万分感谢!PublicC...
程序参考别人的写出了一部分,但是只能读特定的一个Excel,希望有高手帮我改一下添加个“打开”选项,让我可以读电脑里任意一个Excel!程序如下,万分感谢!
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim connstr As String= "Provider=Microsoft.ACE.OLEDB.12.0;DataSource=C:\MyExcel.xls;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'"
Dim sql As String = "select * from [sheet1$]"
Dim adapter As New OleDb.OleDbDataAdapter(sql, connstr)
Dim dataset As New DataSet
adapter.Fill(dataset)
Me.DataGridView1.DataSource = dataset.Tables(0)
End Sub
End Class 展开
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim connstr As String= "Provider=Microsoft.ACE.OLEDB.12.0;DataSource=C:\MyExcel.xls;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'"
Dim sql As String = "select * from [sheet1$]"
Dim adapter As New OleDb.OleDbDataAdapter(sql, connstr)
Dim dataset As New DataSet
adapter.Fill(dataset)
Me.DataGridView1.DataSource = dataset.Tables(0)
End Sub
End Class 展开
展开全部
关键在在这一句:
String= "Provider=Microsoft.ACE.OLEDB.12.0;DataSource=C:\MyExcel.xls;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'"
你把里面的地址改成动态读取也就是了,下面是个例子
Dim nofd As New OpenFileDialog
Dim xlsname As String = ""
nofd.Filter = "Excel文件|*.xls"
If nofd.ShowDialog = Windows.Forms.DialogResult.OK Then
xlsname = nofd.FileName
'如果想打开多个文件,就设置 nofd.Multiselect = True
'然后通过循环 nofd.FileNames 来读取
'最后是你自己的处理代码
String= "Provider=Microsoft.ACE.OLEDB.12.0;DataSource="& xlsname &";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'"
'== 其它代码
Else
Exit Sub
End If
String= "Provider=Microsoft.ACE.OLEDB.12.0;DataSource=C:\MyExcel.xls;Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'"
你把里面的地址改成动态读取也就是了,下面是个例子
Dim nofd As New OpenFileDialog
Dim xlsname As String = ""
nofd.Filter = "Excel文件|*.xls"
If nofd.ShowDialog = Windows.Forms.DialogResult.OK Then
xlsname = nofd.FileName
'如果想打开多个文件,就设置 nofd.Multiselect = True
'然后通过循环 nofd.FileNames 来读取
'最后是你自己的处理代码
String= "Provider=Microsoft.ACE.OLEDB.12.0;DataSource="& xlsname &";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'"
'== 其它代码
Else
Exit Sub
End If
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询