如果没有打开excel文件,则无法将Excel导入DataGridView VB.net [英] Can't Import Excel to DataGridView VB.net if that excel file not opened

查看:57
本文介绍了如果没有打开excel文件,则无法将Excel导入DataGridView VB.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



请帮助,我已尝试下面的代码将excel文件导入datagridview。如果excel文件由我打开,这是有用的。如果excel文件未打开,则datagridview返回空白页,没有标题,单元格或行。因此,要使代码工作,我必须通过office打开excel文件并运行应用程序。哪里错了?



这里是代码:

 进口 System.Data.OleDb 
公共 Form1
Dim conn As OleDbConnection
Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim cmd As OleDbCommand
私有 Sub Button1_Click( ByVal sender As System。 Object ByVal e As System.Ev entArgs)句柄 Button1.Click
On 错误 恢复 下一步
OpenFileDialog1.Filter = (*。xls)| * .xls |(*。xlsx)| * .xlsx |所有文件(*。*)| *。*
OpenFileDialog1.ShowDialog()
conn = OleDbConnection( provider = Microsoft.Jet.OLEDB.4.0;& data source ='& OpenFileDialog1.FileName& ';扩展属性= Excel 8.0;
da = OleDbDataAdapter( select * from [Sheet1 $], conn)
conn.Open()
ds.Clear()
da.Fill(ds)
DataGridView1.DataSource = ds.Tables( 0
TextBox1.Text = OpenFileDialog1.FileName.ToString
conn.Close()
结束 < span class =code-keyword> Sub
结束





谢谢,任何帮助表示赞赏:)

解决方案

,conn)
conn.Open()
ds.Clear()
da.Fill(ds)
DataGridView1.DataSource = ds.Tables( 0
TextB ox1.Text = OpenFileDialog1.FileName.ToString
conn.Close()
结束 Sub
结束 班级





谢谢,任何帮助表示赞赏:)


已解决,

在Connectionstring中出错,提供商是旧的excel 2010 。 provider = Microsoft.Jet.OLEDB.4.0; 它必须是 Provider = Microsoft.ACE.OLEDB.12.0

它适用于代码:

  Imports  System.Data.OleDb 
公开 Form1
Dim conn 作为 OleDbConnection
Dim da 正如 OleDbDataAdapter
Dim ds 作为 DataSet
Dim cmd 作为 OleDbCommand
私有 Sub Button1_Click( ByVal sender As System。 Object ByVal e As System.EventArgs) Handles Button1.Click
On 错误 简历 下一步
OpenFileDialog1.Filter = (*。xls)| * .xls |(*。xlsx)| * .xlsx |所有文件(*。*)| *。*
OpenFileDialog1.ShowDialog()
conn = O. leDbConnection( Provider = Microsoft.ACE.OLEDB.12.0;& data source ='& OpenFileDialog1.FileName& ';扩展属性= Excel 8.0;
da = OleDbDataAdapter( select * from [Sheet1


,conn)
conn.Open()
ds.Clear()
da.Fill(ds)
DataGridView1.DataSource = ds.Tables ( 0
TextBox1.Text = OpenFileDialog1.FileName.ToString
conn.Close()
结束 Sub





感谢CodeProject! :)


Hi,
Please Help, I have try below code to import an excel files to datagridview. It's work if excel file open by me. If excel file not open, datagridview return blank page, no header, cell, or rows. So to make it code works, I must open the excel file by office and running the application. Where is wrong?

Here's code :

Imports System.Data.OleDb
Public Class Form1
    Dim conn As OleDbConnection
    Dim da As OleDbDataAdapter
    Dim ds As New DataSet
    Dim cmd As OleDbCommand
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        On Error Resume Next
        OpenFileDialog1.Filter = "(*.xls)|*.xls|(*.xlsx)|*.xlsx|All files (*.*)|*.*"
        OpenFileDialog1.ShowDialog()
        conn = New OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;" & "data source='" & OpenFileDialog1.FileName & "';Extended Properties=Excel 8.0;")
        da = New OleDbDataAdapter("select * from [Sheet1$]", conn)
        conn.Open()
        ds.Clear()
        da.Fill(ds)
        DataGridView1.DataSource = ds.Tables(0)
        TextBox1.Text = OpenFileDialog1.FileName.ToString
        conn.Close()
    End Sub
End Class



thanks, any help are appreciated :)

解决方案

", conn) conn.Open() ds.Clear() da.Fill(ds) DataGridView1.DataSource = ds.Tables(0) TextBox1.Text = OpenFileDialog1.FileName.ToString conn.Close() End Sub End Class



thanks, any help are appreciated :)


Solved,
Wrong in Connectionstring, the provider is to old an excel 2010. provider=Microsoft.Jet.OLEDB.4.0; it's must be Provider=Microsoft.ACE.OLEDB.12.0
And it works with code :

Imports System.Data.OleDb
Public Class Form1
    Dim conn As OleDbConnection
    Dim da As OleDbDataAdapter
    Dim ds As New DataSet
    Dim cmd As OleDbCommand
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        On Error Resume Next
        OpenFileDialog1.Filter = "(*.xls)|*.xls|(*.xlsx)|*.xlsx|All files (*.*)|*.*"
        OpenFileDialog1.ShowDialog()
        conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" & "data source='" & OpenFileDialog1.FileName & "';Extended Properties=Excel 8.0;")
        da = New OleDbDataAdapter("select * from [Sheet1


", conn) conn.Open() ds.Clear() da.Fill(ds) DataGridView1.DataSource = ds.Tables(0) TextBox1.Text = OpenFileDialog1.FileName.ToString conn.Close() End Sub



Thanks CodeProject! :)


这篇关于如果没有打开excel文件,则无法将Excel导入DataGridView VB.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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