Reputation: 3311
I'm trying to export the content of a DataGridView to an excel worksheet by using the below code
Dim xlApp As New Microsoft.Office.Interop.Excel.Application
Dim xlBook As Microsoft.Office.Interop.Excel.Workbook = xlApp.Workbooks.Add
Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet = CType(xlBook.Sheets(1), Microsoft.Office.Interop.Excel.Worksheet)
xlSheet.Name = "MySheet"
xlApp.Visible = True
With xlSheet
For C As Integer = 0 To DGV_IntCalc.Columns.Count - 1
.Range(.Cells(1, C + 1)).Value = DGV_IntCalc.Columns(C).HeaderText
For R As Integer = 0 To DGV_IntCalc.RowCount - 1
.Range(.Cells(R + 2, C + 1)).Value = DGV_IntCalc.Rows(R).Cells(C).Value
Next
Next
End With
I get error on .Range(.Cells(1, C + 1)).Value = DGV_IntCalc.Columns(C).HeaderText
I have Option Strict On
so I can't use .Cells(1, C + 1).Value = DGV_IntCalc.Columns(C).HeaderText
because of the late binding
Where am I wrong?
Upvotes: 0
Views: 519
Reputation: 3311
Even if the range has only one cell, you need to insert both starting and ending cells of the range.
The solution is to change:
.Range(.Cells(1, C + 1)).Value = DGV_IntCalc.Columns(C).HeaderText
with:
.Range(.Cells(1, C + 1), .Cells(1, C + 1)).Value = DGV_IntCalc.Columns(C).HeaderText
Upvotes: 2