后期绑定错误

时间:2012-11-14 21:20:57

标签: .net vb.net visual-studio-2010 excel

所以我最近在我当前的项目中改变了Option Strict On,并在修复了选项引发的所有小错误之后,我碰到了这个错误。

Public Sub ExportarExcel(ByVal grilla As DataGridView)

    Dim excelApp As Excel.Application
    Dim workbook As Excel.Workbook
    Dim sheet As Excel.Worksheet
    Dim i As Integer = 1
    Dim j As Integer = 1
    excelApp = CType(CreateObject("Excel.Application"), Excel.Application)
    workbook = excelApp.Workbooks.Add
    sheet = CType(workbook.Worksheets.Add, Excel.Worksheet)

    For Each col As DataGridViewColumn In grilla.Columns
        sheet.Cells(1, i).Borders.LineStyle = Excel.XlLineStyle.xlContinuous 'Problematic line
        sheet.Cells(1, i) = col.HeaderText
        i = i + 1
    Next
    i = 2

    For Each row As DataGridViewRow In grilla.Rows
        j = 1
        For Each cell As DataGridViewCell In row.Cells
            sheet.Cells(i, j).Borders.LineStyle = Excel.XlLineStyle.xlContinuous 'Problematic line
            sheet.Cells(i, j) = cell.Value
            j = j + 1
        Next
        i = i + 1
    Next

    sheet.Columns.AutoFit()
    excelApp.Visible = True
End Sub

这两行(从一开始就是“Borders。”)抛出一个后期绑定错误,我不确定哪些是适当的强制转换或修复这些行。

2 个答案:

答案 0 :(得分:1)

根据我找到的文档(此处:http://msdn.microsoft.com/en-us/library/office/ff822605.aspx,此处:http://msdn.microsoft.com/en-us/library/office/aa612949(v=office.10).aspx),Borders属性返回一个集合,由常量索引,表示您想要的边框(顶部) ,底部,左,右等)

你的行应该是这样的,

sheet.Cells(1, i).Borders(xlEdgeBottom).LineStyle = Excel.XlLineStyle.xlContinuous

答案 1 :(得分:0)

您可以使用sheet.Cells(i, j).Text = cell.Valuesheet.Cells(i, j).Value = cell.Value(这可能是Value2)