Hello Everyone:
I am new in programming and I am using VB2005.
Here what I am doing: I have a program that is connecting to a SQL2005, I have a search screen an so far so good.
The question that I have; after doing a search I would like to add an export button, where whatever was populated on the grid to be exported into an excel file. is this possible?
I am using a datagridview and is a databound.
Thanks
JUliusPosted Jul 16, 2008, 10:51 AM
uma uppalaPosted Jul 14, 2008, 8:09 PM
Public
Sub ExportToExcel(ByVal dt As DataTable) Try Dim ColIndex As Int16 = 0 Dim RowIndex As Int16 = 0 Dim ColPos As Int16 = 0
Dim myexcel As New Excel.Application Dim wBook As Excel.Workbook = myexcel.Workbooks.Add() Dim wSheet As Excel.Worksheet = CType(wBook.Worksheets(1), Excel.Worksheet)myexcel.Visible =
False
With wSheet For Each dc As DataColumn In dt.ColumnsColIndex = ColIndex + 1
If ColStatus(ColIndex - 1) = "True" ThenColPos = ColPos + 1
myexcel.Cells(RowIndex + 1, ColPos) = dc.ColumnName
End If Next End With With wSheet For Each dr As DataRow In dt.RowsRowIndex = RowIndex + 1
ColIndex = 0
ColPos = 0
For Each dc As DataColumn In dt.ColumnsColIndex = ColIndex + 1
If ColStatus(ColIndex - 1) = "True" ThenColPos = ColPos + 1
myexcel.Cells(RowIndex + 1, ColPos) = dr(dc.ColumnName)
End If Next Next End With With myexcel.Columns(
"A:J").ColumnWidth = 15.Range(
"A1:J1").HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter With .Range("A1:J1").Font.Name =
"Microsoft Sans Serif".FontStyle =
"Bold".Size = 10
.Strikethrough =
False.Superscript =
False.Subscript =
False.OutlineFont =
False.Shadow =
False.ColorIndex = 0
End With End Withmyexcel.Visible =
True Catch ex As ExceptionMessageBox.Show(
"Source [" & ex.Source & "] Description [" & ex.Message & "]") End Try End Sub