how to pass from datagrid to EXCEL
Visual Basic
2
Posts
2
Posters
0
Views
1
Watching
-
anyone know how to pass some information from datagrid to EXCEL?? which from the row 1 of the datagrid to the last row.
I can be somthing like this:
Private Sub cmdCreateXLTemplate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCreateXLTemplate.Click
Dim ColHeadings() As String = {"warehouse", "product", "alpha"}Dim xlApp As New Excel.Application Dim xlBook As Excel.Workbook Dim Row As Integer = 1 Dim Col As Integer xlBook = xlApp.Workbooks.Add xlBook.Sheets(2).Name = "ErrorData" 'Column Headings For Col = 1 To ColHeadings.GetUpperBound(0) xlBook.Sheets(1).cells(2, Col) = ColHeadings(Col - 1) Next 'Actual Data For j As Integer = 0 To Me.dgExcelData.RowCount - 1 xlBook.Sheets(1).cells(j+1, 1) = Me.dgExcelData.Item(0, j).ToString Next 'Saving The Excel File Dim Filename As String Filename = Application.StartupPath & "\\Import.xls" Dim i As Integer = 1 Do While IO.File.Exists(Filename) Filename = "C:\\Import" & i & ".xls" Loop Dim Save As New SaveFileDialog Save.FileName = Filename Dim Result As DialogResult Result = Save.ShowDialog If Result = Windows.Forms.DialogResult.OK Then xlBook.SaveAs(Filename) End If xlBook.Close() xlBook = Nothing xlApp.Quit() xlApp = Nothing GC.Collect() End Sub