2012-11-14 52 views
1

所以我最近在我當前的項目中將選項嚴格打開,修復了所有選項拋出的小錯誤後,我碰到了這個。遲綁定錯誤

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 

這兩行(從開始到「邊框」)都扔後期綁定錯誤,我不知道這是正確的澆鑄或修復這些線路。

+0

您是否嘗試過使用值1而不是'Excel.XlLineStyle.xlContinuous'? – Steve

+0

@Steve嘗試過,但錯誤指向「sheet.Cells(1,i).Borders」行的左側。 – Diego

回答

0

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