【问题标题】:Adding Items to datagridview with Excel database使用 Excel 数据库将项目添加到 datagridview
【发布时间】:2019-11-14 08:55:02
【问题描述】:

我正在尝试使用 vb.net 代码在我的 datagridview 中添加项目。我正在使用 Excel 数据库。这是我的代码,我尝试过的所有东西都是 cmets 的形式。如果有人打电话帮助我,我将不胜感激。这周我一直在尝试。谢谢

Imports System.Data.OleDb
Imports System.IO
Imports System.Data.DataTable
Imports Microsoft.Office.Interop

Public Class Welcome
Private Sub ImportExceldata(DataExcelFile As String, ImportData As DataGridView)



    If ComboBox1.SelectedIndex.Equals(0) Then
        Try
            Dim MyConnection As System.Data.OleDb.OleDbConnection
            Dim dataSet As System.Data.DataSet
            Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
            MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & DataExcelFile & "';Extended Properties=Excel 8.0;")
            MyCommand = New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM [CandidatesCallCenterLocation$]", MyConnection)
            'MyCommand.TableMappings.Add("Table", "Attendace")
            dataSet = New System.Data.DataSet
            MyCommand.Fill(dataSet)
            ImportData.DataSource = dataSet.Tables(0)
            DataGridView.DataSource = dataSet.Tables(0)


            MyConnection.Close()

        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try

    ElseIf ComboBox1.SelectedIndex.Equals(1) Then
        Try
            Dim MyConnection As System.Data.OleDb.OleDbConnection
            Dim dataSet As System.Data.DataSet
            Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
            MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & DataExcelFile & "';Extended Properties=Excel 8.0;")
            MyCommand = New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM [ExpectedNumberofCalls$]", MyConnection)
            'MyCommand.TableMappings.Add("Table", "Attendace")
            dataSet = New System.Data.DataSet
            MyCommand.Fill(dataSet)
            ImportData.DataSource = dataSet.Tables(0)
            DataGridView.DataSource = dataSet.Tables(0)
            MyConnection.Close()
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    ElseIf ComboBox1.SelectedIndex.Equals(2) Then
        Try
            Dim MyConnection As System.Data.OleDb.OleDbConnection
            Dim dataSet As System.Data.DataSet
            Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
            MyConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & DataExcelFile & "';Extended Properties=Excel 8.0;")
            MyCommand = New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM [CostProcessingTelephoneCalls$]", MyConnection)
            'MyCommand.TableMappings.Add("Table", "Attendace")
            dataSet = New System.Data.DataSet
            MyCommand.Fill(dataSet)
            ImportData.DataSource = dataSet.Tables(0)
            DataGridView.DataSource = dataSet.Tables(0)
            MyConnection.Close()
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try

    End If
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs)

End Sub

Private Sub ComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged

    ImportExceldata("C:\Users\Home\Desktop\dss.xlsx", DataGridView)

End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    For Each row As DataGridViewRow In DataGridView.SelectedRows
        DataGridView.Rows.Remove(row)

    Next
End Sub

这部分由添加按钮和我尝试过的所有代码组成。我认为底部有效,但显然,它没有。

Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click



    ' Dim Table As New DataTable("Table")
    ' Table.Columns.Add("Area Code", Type.GetType("System.Int64"))
    ' Table.Columns.Add("Metropolitan Area", Type.GetType("System.Int64"))
    'Table.Columns.Add("Annual Fixed Cost", Type.GetType("System.Int64"))
    'Table.Columns.Add("Wage per hour", Type.GetType("System.Int64"))
    'Table.Rows.Add("", "", "", "")
    'Table.Rows.Add(2, "aaaa", "bbbb", 33)
    'Table.Rows.Add(3, "cccc", "dddd", 53)
    'Table.Rows.Add(4, "eeee", "ffff", 63)
    'Table.Rows.Add(5, "rrrr", "qqqq", 73)
    'Table.Rows.Add(6, "gggg", "hhhh", 83)
    ' Dim Add As Object
    ' Add = CreateObject("Excel.Application")
    ' Dim oneadd As Excel.Workbook
    ' Dim sheetadd As Excel.Worksheet
    'Dim fexcel As String = "C:\Users\Home\Desktop\dss.xlsx"
    ' Dim curLine As Integer = 0

    'oneadd = Add.Workbooks.Add

    'If Add.Application.Sheets.Count() < 1 Then
    'sheetadd = CType(oneadd.Worksheets.Add, Excel.Worksheet)
    ' Else

    'sheetadd = Add.Worksheets(1)
    'End If
    'sheetadd.Name = "CandidatesCallCenterLocation$"
    'sheetadd.Range("A1").Value = "Row1"
    'oneadd.SaveAs(fexcel)
    'oneadd.Close()
    'oneadd = Nothing
    'DataGridView.AllowUserToAddRows = False
    'Me.DataGridView.Rows.Add(ACTextBox.Text, MATextBox.Text, AFCTextBox.Text, TextBoxWPH.Text)
    End Sub







' Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
'Dim cn As New OleDbConnection
' Dim cm As New OleDbCommand
'cn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Home\Desktop\dss.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=YES""")
'cn.Open()
'With cm
'.Connection = cn
'.CommandText = "update [up$] set [name]=?, [QC_status]=?, [reason]=?, [date]=? WHERE [article_no]=?"
'cm = New OleDbCommand(.CommandText, cn)
'cm.Parameters.AddWithValue("?", ACTextBox.Text)
' cm.Parameters.AddWithValue("?", ComboBox1.SelectedItem)
'cm.Parameters.AddWithValue("?", TextBox3.Text)
'cm.Parameters.AddWithValue("?", DateTimePicker1.Text)
'cm.Parameters.AddWithValue("?", TextBox1.Text)
'cm.ExecuteNonQuery()
'MsgBox("UPDATE SUCCESSFUL")
' MyConnection.Close()
'End With
'End Sub
End Class

【问题讨论】:

  • “我认为底部部分有效,但显然,它没有”...我们在这里遇到的困难是“不起作用”不是t 错误消息或问题说明。我们无法在代码中修复“不工作”,就像机械师无法修复“不工作”的汽车一样,没有任何关于问题的其他信息。它怎么不工作?运行代码时究竟会发生什么?
  • 这里似乎还有两三个不同的代码尝试,其中两个被注释掉了。我不推荐第一个注释掉的版本,因为它试图使用 COM 互操作,这很容易出错,而且无论如何都不适合这个目的。第二个被注释掉的版本似乎是关于更新电子表格,而不是从中读取。因此,让我们专注于未注释的版本...是否出现了您想要强调的特定问题?

标签: excel vb.net winforms datagridview oledb


【解决方案1】:

启动一个新的 windows 窗体项目,在窗体中添加一个 DataGridView 和一个 Button,然后:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  Dim filename = "C:\Junk\Junk.xlsx"
  Dim xl As Object = CreateObject("Excel.Application")       'Late-bound
  Dim b As Object = xl.Workbooks.Open(filename)           'Late-bound
  Dim s As Object = b.Sheets(1) '1-based                     'Late-bound
  Dim values(,) As Object = s.UsedRange.Value2  '1-based
  xl.Quit
  Dim valuesDatatable = New DataTable
  Dim columnCount = values.GetUpperBound(1)
  For col = 1 To columnCount
    valuesDatatable.Columns.Add($"Column{col}")
  Next
  For row = 1 To values.GetUpperBound(0)
    Dim o(columnCount - 1) As Object
    For col = 1 To columnCount
      o(col - 1) = values(row, col)
    Next col
    valuesDatatable.Rows.Add(o)
  Next row
  DataGridView1.DataSource = valuesDatatable
End Sub

请注意,Excel 从 1 开始计数元素,而 VB 从 0 开始计数。

以上使用后期绑定,但我建议使用Microsoft Primary Interop Assemblies 进行开发(因此您有 F1 帮助),然后更改为后期绑定进行部署(因此您不会绑定到特定版本的 Excel )。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-20
    相关资源
    最近更新 更多