'This takes in a GridView, converts it to Excel, prompts the user to download it
'FileName - The name of the file with no extension (ie: "filename")
Private Sub ExcelDownloadFromGridView(ByVal FileName As String, ByVal WorkBookTitle As String)
'Add the xls to the FileName of the Excel File
FileName = FileName + ".xls"
Dim Grid As New DataGrid 'Used to convert to excel
Grid.HeaderStyle.Font.Bold = True 'Make the columns bold
Grid.DataSource = Cache("GridTable") '"myGridView.DataSource 'Set in the table to the grid
Grid.DataMember = Cache("GridTable").TableName 'myGridView.DataMember
Grid.DataBind()
'Push the excel file out to the user
Response.Clear()
Response.Write("<html xmlns:x=""urn:schemas-microsoft-com:office:excel"">")
Response.Write("<head>")
Response.Write("<!--[if gte mso 9]><xml>")
Response.Write("<x:ExcelWorkbook>")
Response.Write("<x:ExcelWorksheets>")
Response.Write("<x:ExcelWorksheet>")
Response.Write("<x:Name>" & WorkBookTitle & "</x:Name>")
Response.Write("<x:WorksheetOptions>")
Response.Write("<x:Print>")
Response.Write("<x:ValidPrinterInfo/>")
Response.Write("</x:Print>")
Response.Write("</x:WorksheetOptions>")
Response.Write("</x:ExcelWorksheet>")
Response.Write("</x:ExcelWorksheets>")
Response.Write("</x:ExcelWorkbook>")
Response.Write("</xml>")
Response.Write("<![endif]--> ")
Response.Write("</head>")
Response.Write("<body>")
Response.AddHeader("content-disposition", "attachment;filename=" & FileName)
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = "application/vnd.xls"
Dim stringWrite As System.IO.StringWriter = New System.IO.StringWriter
Dim htmlWrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite)
Grid.RenderControl(htmlWrite)
Response.Write(stringWrite.ToString)
Response.Write("</body>")
Response.Write("</html>")
Response.End()
End Sub
To write out your formatting use Grid.items(index).cells(index).Backcolor = or any of the other ones; If already set it will convert over to the Excel Doc
!--removed tag-->
Enter your message below
Sign in or Join us (it's free).