Vb.net 将excel单元格数据写入datagridview单元格(visual basic)

Vb.net 将excel单元格数据写入datagridview单元格(visual basic),vb.net,excel,loops,datagridview,Vb.net,Excel,Loops,Datagridview,我在列中循环,在列中循环行。excel文件的布局方式是,列是每个日期,行包括各种值的单独数据。问题是dataviewgrid没有使用我的excel文件中的值填充。这是我的循环 ' Get the Excel application object. excel_app = New Excel.Application ' Make Excel visible (optional). excel_app.Visible = False ' Open the work

我在列中循环,在列中循环行。excel文件的布局方式是,列是每个日期,行包括各种值的单独数据。问题是dataviewgrid没有使用我的excel文件中的值填充。这是我的循环

' Get the Excel application object.
    excel_app = New Excel.Application

    ' Make Excel visible (optional).
    excel_app.Visible = False

    ' Open the workbook.
    workbook = excel_app.Workbooks.Open("C:\Users\Michael Jr\Desktop\iBudgetBuddy\Records.xlsx", , True)
    sheet_name = "Sheet1"

    sheet = excel_app.Worksheets("Sheet1")

    Dim ColumnCount, RowCount As Long
    ColumnCount = sheet.Range("A1").CurrentRegion.Columns.Count
    RowCount = sheet.Range("A1").CurrentRegion.Rows.Count

    DataGridView1.ColumnCount = ColumnCount
    DataGridView1.RowCount = RowCount
    DataGridView1.ColumnHeadersVisible = False
    DataGridView1.RowHeadersVisible = False

    **For ecIndex As Integer = 0 To ColumnCount
        Dim drIndex As Integer = 0
        Dim dcolumnIndex As Integer = 0
        For erIndex As Integer = 0 To RowCount
            DataGridView1.Rows(drIndex).Cells(dcolumnIndex).Value = sheet.Cells(erIndex, ecIndex).Value
            erIndex = erIndex + 1
            drIndex = drIndex + 1
        Next
        dcolumnIndex = dcolumnIndex + 1
        ecIndex = ecIndex + 1
    Next**

End Sub
知道它为什么不起作用吗

*编辑* 我已经能够成功地让程序读取并创建行和列。它填充第一列,即该行包含的标题、示例、收入和费用。但是,它不会填充上第2列中的任何内容。为什么会这样?我编辑的循环如下所示

    For cIndex As Integer = 0 To ColumnCount

        For rIndex As Integer = 0 To RowCount
            DataGridView1.Rows(rIndex).Cells(cIndex).Value = sheet.Range("A1").Offset(rIndex, cIndex).Value()
        Next

    Next

当您使用XLSX时,我建议您放弃互操作,使用类似CloseXML的东西来读取exel文件。显示错误或什么都没有发生?您可以使用OleDB打开excel文件并填充数据集,然后使用该数据集填充DGV?我的猜测是,您将此代码包装在一个try…catch中,它只会消耗错误,当您尝试在循环中第一次引用sheet.Cells0,0时,它会爆炸。Excel.Interop对单元格使用基于1的索引。没有错误,只是什么都没有发生。我曾尝试使用OleDB,但我遇到了太多问题,错误是我没有尝试过的任何版本,我是x64 windows 7,我假设使用x32 office。我已经尝试了很多解决OleDB问题的方法,但是都没有用,所以我已经放弃了。你有什么建议或者我可以插入的代码吗?