-
Datagrid to excel
hello im having a problem with transferring datagrid to excel this is what i have
Code:
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim i As Integer
Dim j As Integer
xlApp = New Excel.ApplicationClass
xlWorkBook = xlApp.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
For i = 0 To DataGridView1.RowCount - 2
For j = 0 To DataGridView1.ColumnCount - 1
If DataGridView1.Columns(j).Visible = True Then
xlWorkSheet.Cells(i + 1, j + 1) = _
DataGridView1(j, i).Value.ToString()
End If
Next
Next
xlWorkSheet.SaveAs(SaveFileDialog1.FileName)
xlWorkBook.Close()
xlApp.Quit()
some of the columns in my datagrid are hidden, in the middle of the code theres if statement so it will copy the columns that are visible, but my problem is when i open the excel file some of the columns are empty.
-
Re: Datagrid to excel
Go to my signature , and click working with excel.