Click here to Skip to main content
15,892,517 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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]



VB
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 :

VB
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
Posted
Comments
ChauhanAjay 9-Sep-14 0:32am    
Just check the below link
www.codeproject.com/Tips/669509/How-to-Export-Data-to-Excel-in-VB-NET

It contains the full code to export to excel. Hope it helps.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900