如何使Excel文件成为dataGridview表单的VB.NET资源?

时间:2018-12-05 03:08:42

标签: vb.net datagridview

我正在学习VB.NET,所以请原谅我的无知...

我正在使用dataGridView表单显示Excel文件(.xls)作为帮助文件。我使用Excel格式是因为它的格式非常好,可以在Excel中进行更新,并且在dataGridView表单中看起来很棒...比标准文本文件更容易和更好。

但是,即使我可以从磁盘(“ C:\ DTC.XLS”)访问文件,我还是希望将文件作为资源放置在VB应用程序本身中,但我无法成功完成此操作。我将文件拖到“解决方案资源管理器”>“我的项目”>“资源”中,在文件下显示为“ DTC”。

在我的代码中,如果我用资源句柄(My.Resources.DTC)替换文件路径('C:\ DTC.XLS'),则会出现异常:“ System.Data.OleDb.OleDbException:'无法更新。数据库或对象是只读的。’

我可以选择“只读” ...这只是一个帮助文件。如何获取dataGridView表单以无例外地打开此资源?

我的例程:

Private Sub DTCinfo(sender As Object, e As EventArgs) Handles DTCdataGridView_button.Click

    If DTCdataGridView_button.Text = "CLOSE Info" Then
        DataGridView1.Hide()
        DTCdataGridView_button.Text = "DTC Info"
        DTCdataGridView_button.BackColor = SystemColors.ControlDarkDark

    Else
        DTCdataGridView_button.Text = "CLOSE Info"
        DTCdataGridView_button.BackColor = Color.Red

        Dim MyConnection As System.Data.OleDb.OleDbConnection
        Dim DtSet As System.Data.DataSet
        Dim MyCommand As System.Data.OleDb.OleDbDataAdapter

        ' ***** THE FOLLOWING LINE WORKS OK WITH THIS SUBROUTINE:
        ' MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\DTC.xls';Extended Properties=Excel 8.0;")

        ' ***** THE FOLLOWING LINE DOES NOT WORK WITH THIS SUBROUTINE (GENERATES AN EXCEPTION):
        MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source=My.Resources.DTC;Extended Properties=Excel 8.0;")

        MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection)
        MyCommand.TableMappings.Add("Table", "Net-informations.com")
        DtSet = New System.Data.DataSet
        MyCommand.Fill(DtSet)

        DataGridView1.DataSource = DtSet.Tables(0)
        DataGridView1.ReadOnly = True
        DataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.[True]

        AutoSizeColumnsMode = True
        AutoSizeRowsMode = True
        DataGridView1.Columns(0).AutoSizeMode = False

        DataGridView1.Columns(0).Width = 50
        DataGridView1.Columns(2).Width = 200
        DataGridView1.Columns(3).Width = 150

        DataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells

        With DataGridView1
            .RowHeadersVisible = False
            .Columns(0).HeaderText = "DTC"
            .Columns(1).HeaderText = "Test Mode"
            .Columns(2).HeaderText = "Description"
            .Columns(3).HeaderText = "Action"
        End With

        MyConnection.Close()
        DataGridView1.Show()

    End If

End Sub

我找到了另一个在线执行此操作的示例。它更容易阅读,也更容易理解,但是我仍然遇到相同的异常:“无法更新:数据库或对象是只读的。”如何解决?

    ' here's another test....
Sub testDataGridView()

    Dim dt As New DataTable
    ' Note, you should use My.Settings rather than My.Resources for storing the data source
    '        Dim connectionString As String =
    '            $"provider=Microsoft.Jet.OLEDB.4.0;Data Source={My.Settings.gizmotest};Extended Properties=Excel 8.0;"
    Dim connectionString As String =
        $"provider=Microsoft.Jet.OLEDB.4.0;Data Source={My.Resources.DTC};Extended Properties=Excel 8.0;"
    ' Note, you should use import statements for these objects e.g.
    ' Place this as the first line in the code file
    ' Imports System.Data.OleDb

    Using cn As New OleDbConnection With {.connectionString = connectionString}
        Using cmd As New OleDbCommand With {.Connection = cn, .CommandText = "select * from [Sheet1$]"}
            Try
                cn.Open()
                dt.Load(cmd.ExecuteReader)
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try

        End Using
    End Using

End Sub

1 个答案:

答案 0 :(得分:0)

好吧,似乎DataGridView表单不适用于只读源...或者至少在使用Excel输入格式时不起作用...是吗?而且我猜资源只能是只读的...我猜是吗?我无法确定所有这些,但这是我最好的猜测...

因此,我在打开表单时在磁盘上创建了一个临时文件,将资源复制到该文件,然后在DataGridView表单中打开了该文件,然后关闭并删除了该文件。瞧!作品。对我来说似乎有点笨拙,但是,嘿,它有效,看起来不错,所以现在开始讨论更大的事情。...