使用Excel数据库向Datagridview添加项目

问题描述 投票:0回答:1

我正在尝试使用vb.net代码在我的datagridview中添加项目。我正在使用Excel数据库。这是我的代码,我尝试过的所有内容都以注释的形式出现,如果有人打电话帮助我,我将不胜感激。我整个星期都在尝试。谢谢

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
excel vb.net winforms datagridview oledb
1个回答
0
投票

启动一个新的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) 。

© www.soinside.com 2019 - 2024. All rights reserved.