【问题标题】:Using VB.net how would I implement DataGridView to display contents of the 2 dimensional array in the form?使用 VB.net 我将如何实现 DataGridView 以在表单中显示二维数组的内容?
【发布时间】:2016-07-31 00:43:42
【问题描述】:

这个程序将一个 excel 文件读入一个二维数组。现在我需要在网格中显示结果,就像查看原始 excel 文件一样。有人告诉我 DataGridView 可能会提供帮助。不知道如何进行。

Imports Microsoft.Office.Interop.Excel

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

创建新应用程序。

    Dim excel As Application = New Application

打开 Excel 电子表格。

    Dim w As Workbook = excel.Workbooks.Open("G:\PACE\New Style Project\01.xls")

遍历所有工作表。

    For i As Integer = 1 To w.Sheets.Count

获取工作表。

        Dim sheet As Worksheet = w.Sheets(i)

获取范围。

        Dim r As Range = sheet.UsedRange

Load all cells into 2d array.
        Dim array(,) As Object = r.Value(XlRangeValueDataType.xlRangeValueDefault)

扫描细胞。

        If array IsNot Nothing Then
            'Console.WriteLine("Length: {0}", Array.Length)

获取数组的边界。

            Dim bound0 As Integer = Array.GetUpperBound(0)
            Dim bound1 As Integer = Array.GetUpperBound(1)

            'Console.WriteLine("Dimension 0: {0}", bound0)
            'Console.WriteLine("Dimension 1: {0}", bound1)

循环遍历所有元素。

            For j As Integer = 1 To bound0
                For x As Integer = 1 To bound1
                    Dim s1 As String = Array(j, x)
                    'Console.Write(s1)
                    'Console.Write(" "c)


                Next
                'Console.WriteLine()

            Next
        End If
    Next

    ' Close.
    w.Close()
End Sub

结束类

【问题讨论】:

    标签: arrays vb.net display


    【解决方案1】:

    实现此目的的一种快速简便的方法是使用 Microsoft.ACE.OLEDB.12.0.....

    这是一个简单的例子......

        Dim DataSet As New DataSet
        Try
            Dim con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + "<Path to your Excel file>" + ";Extended Properties=Excel 12.0;")
    
            con.Open()
            Dim atatable = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
    
            Dim sql As String
    
            Dim excelcomm = New OleDbCommand()
            Dim adexcel = New OleDbDataAdapter(excelcomm)
    
            Dim tableInfo = con.GetSchema("Tables")
            For Each row As DataRow In tableInfo.Rows
                Console.WriteLine("{0}", row("TABLE_NAME").ToString)
                Dim sheetname = row("TABLE_NAME").ToString
    
                sql = "select * from [" + sheetname + "]"
                adexcel.SelectCommand = New OleDbCommand(sql, con)
                adexcel.Fill(DataSet, sheetname)
            Next
    
    
        Catch ex As Exception
    
        End Try
        DataGridView1.DataSource = DataSet.Tables(0) ' set DataGridView1 to display first table
    

    如果出现错误,请先尝试代码

    the 'microsoft.ace.oledb.12.0' provider is not registered on the local machine
    

    在“con.Open()”处,您可能需要从此处安装 AccessDatabaseEngine.exe(32 位版本而不是 64 位版本)

    https://www.microsoft.com/en-us/download/details.aspx?id=13255

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-30
      • 2013-07-03
      • 2018-05-06
      • 2020-05-18
      相关资源
      最近更新 更多