what i'm trying to do is export data from datagridview using the below codes. I have values in my Datagridview2 which is :
1,ic,456,456.00,465.00
2,465,465,465.00,56.00
3,dfggh,fggh,10.0,1.00
[this is from a text files]
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
xlApp = New Microsoft.Office.Interop.Excel.Application
If filecounter = 1 Then
xlWorkBook = xlApp.Workbooks.Add(misValue)
Else
xlWorkBook = xlApp.Workbooks.Open("D:\try.xlsx")
End If
xlWorkSheet = xlWorkBook.Sheets("Sheet1")
For x As Integer = 0 To DataGridView2.Rows.Count - 1
For i As Integer = 0 To DataGridView2.Columns.Count - 1
xlWorkSheet.Cells(1, 2) = "Try"
xlWorkSheet.Cells(1, 3) = "try"
xlWorkSheet.Cells(1, 4) = "Spec3"
xlWorkSheet.Cells(1, 5) = "CWPA"
xlWorkSheet.Cells(1, 6) = "CWPB"
xlWorkSheet.Cells(i + 2, 2) = DataGridView2.Rows(x).Cells(i).Value.ToString
xlWorkSheet.Cells(i + 2, 3) = DataGridView2.Rows(x).Cells(i).Value.ToString
xlWorkSheet.Cells(i + 2, 4) = DataGridView2.Rows(x).Cells(i).Value.ToString
xlWorkSheet.Cells(i + 2, 5) = DataGridView2.Rows(x).Cells(i).Value.ToString
xlWorkSheet.Cells(i + 2, 6) = DataGridView2.Rows(x).Cells(i).Value.ToString
Next
Next
I have errors in :
xlWorkSheet.Cells(i + 2, 2) = DataGridView2.Rows(x).Cells(i).Value.ToString
xlWorkSheet.Cells(i + 2, 3) = DataGridView2.Rows(x).Cells(i).Value.ToString
xlWorkSheet.Cells(i + 2, 4) = DataGridView2.Rows(x).Cells(i).Value.ToString
xlWorkSheet.Cells(i + 2, 5) = DataGridView2.Rows(x).Cells(i).Value.ToString
xlWorkSheet.Cells(i + 2, 6) = DataGridView2.Rows(x).Cells(i).Value.ToString
and I do not know why.
The output I want in excel is this:
[row1]1|ic|456
[row2]456.00
[row3]465.00
[row5 - SPACE]
[row6]1|ic|456
[row7]456.00
[row8]465.00
[row9-SPACE]
[row10]1,ic,456
[row11]456.00
[row12]56.00
[row 13 - SPACE]
[row14]3| dfggh|fggh,
[row15]10.0
[row16]1.00