【发布时间】: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
结束类
【问题讨论】: