使用 VB.net 我将如何实现 DataGridView 以在表单中显示二维数组的内容?
Using VB.net how would I implement DataGridView to display contents of the 2 dimensional array in the form?
此程序将 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传播sheet.
Dim w As Workbook = excel.Workbooks.Open("G:\PACE\New Style Project.xls")
遍历所有 sheets。
For i As Integer = 1 To w.Sheets.Count
获取sheet.
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
结束Class
实现此目的的一种快速简便的方法是使用 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
此程序将 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传播sheet.
Dim w As Workbook = excel.Workbooks.Open("G:\PACE\New Style Project.xls")
遍历所有 sheets。
For i As Integer = 1 To w.Sheets.Count
获取sheet.
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
结束Class
实现此目的的一种快速简便的方法是使用 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