Imports Telerik.WinControls.UI.Export
Imports Telerik.WinControls.UI
Public Class ExportToExcel
Private Sub ExportToExcel_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Try
'this is our standard TelerikGrid class, found here http://telerikdeveloper.com/thread.aspx?Threadid=1386
Dim oTelerikGrid As New TelerikGrid
'add columns to the grid. We've added string, numeric, and date columns
Me.RadGridView1.Columns.Add(oTelerikGrid.createGridViewTextBoxColumn("ItemNumber", "ItemNumber", 30, 100, ""))
Me.RadGridView1.Columns.Add(oTelerikGrid.createGridViewTextBoxColumn("Quantity", "Quantity", 30, 100, "{0:F0}"))
Me.RadGridView1.Columns.Add(oTelerikGrid.createGridViewTextBoxColumn("UnitPrice", "UnitPrice", 30, 100, "{0:F2}"))
'date columns require a little extra formatting
Dim c As GridViewTextBoxColumn = oTelerikGrid.createGridViewTextBoxColumn("ShipDate", "ShipDate", 30, 100, "{0:MM/dd/yyyy}")
c.ExcelExportType = DisplayFormatType.ShortDate
Me.RadGridView1.Columns.Add(c)
'set grid properties
Me.RadGridView1.EnableGrouping = False
Me.RadGridView1.AllowAddNewRow = False
Me.RadGridView1.AllowDeleteRow = False
Me.RadGridView1.AllowEditRow = False
'set the grid data source
Me.RadGridView1.DataSource = GetDataSource()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Public Function GetDataSource() As DataTable
Dim oDT As New DataTable
Dim oKeys(0) As DataColumn
'create a data table to use as our input source
'create columns
Dim oCol As DataColumn = New DataColumn("RowID", System.Type.GetType("System.Int16"))
oDT.Columns.Add(oCol)
oKeys(0) = oCol
oDT.Columns.Add(New DataColumn("ItemNumber", System.Type.GetType("System.String")))
oDT.Columns.Add(New DataColumn("Quantity", System.Type.GetType("System.Double")))
oDT.Columns.Add(New DataColumn("UnitPrice", System.Type.GetType("System.Double")))
oDT.Columns.Add(New DataColumn("ShipDate", System.Type.GetType("System.DateTime")))
oDT.PrimaryKey = oKeys
'create the rows
oDT.Rows.Add(1, "Hammer", 1.0, 1.23, "1/1/2012")
oDT.Rows.Add(2, "Pliers", 2, 2.23, "1/1/2012")
oDT.Rows.Add(3, "Saw", 3, 3.23, "1/1/2012")
oDT.Rows.Add(4, "Axe", 4, 4.23, "1/1/2012")
oDT.Rows.Add(5, "2Hammer", 1, 1.23, "1/1/2012")
oDT.Rows.Add(6, "2Pliers", 2, 2.23, "1/1/2012")
oDT.Rows.Add(7, "2Saw", 3, 3.23, "1/1/2012")
oDT.Rows.Add(8, "2Axe", 4, 4.23, "1/1/2012")
oDT.Rows.Add(9, "3Hammer", 1, 1.23, "1/1/2012")
oDT.Rows.Add(10, "3Pliers", 2, 2.23, "1/1/2012")
oDT.Rows.Add(11, "3Saw", 3, 3.23, "1/1/2012")
oDT.Rows.Add(12, "3Axe", 4, 4.23, "1/1/2012")
oDT.Rows.Add(13, "4Hammer", 1, 1.23, "1/1/2012")
oDT.Rows.Add(14, "4Pliers", 2, 2.23, "1/1/2012")
oDT.Rows.Add(15, "4Saw", 3, 3.23, "1/1/2012")
oDT.Rows.Add(16, "4Axe", 4, 4.23, "1/1/2012")
Return oDT
End Function
Private Sub btnExportToExcel_Click(sender As System.Object, e As System.EventArgs) Handles btnExportToExcel.Click
'this is where the magic happens.
Try
'create a SaveFileDialog, to get the file name and path
Dim saveFileDialog1 As New SaveFileDialog
saveFileDialog1.Filter = "Excel files (*.xls)|*.xls|All files (*.*)|*.*"
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
Dim strFileName As String = saveFileDialog1.FileName
'export to Excel
Dim exporter As ExportToExcelML = New ExportToExcelML(Me.RadGridView1)
Dim fileName As String = strFileName
exporter.RunExport(fileName)
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class