I am trying to export chart control along with grid but with the below code I am just able to get the chart visible in Excel, the Grid does not appear in Excel. Please help what is missing / wrong in my code.
Protected Sub bttnExportToExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bttnExportToExcel.Click
Dim tmpChartName As String = "test2.jpg"
Dim imgPath As String = HttpContext.Current.Request.PhysicalApplicationPath + tmpChartName
Chart1.SaveImage(imgPath)
Dim imgPath2 As String = Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute(Convert.ToString("~/") & tmpChartName)
Response.Clear()
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("Content-Disposition", "attachment; filename=Performance Metrics.xls;")
Dim stringWrite As New StringWriter()
Dim htmlWrite As New HtmlTextWriter(stringWrite)
Dim headerTable As String = (Convert.ToString("<Table><tr><td><img src='") & imgPath2) + "' \></td></tr></Table>"
Response.Write(headerTable)
Response.Write(stringWrite.ToString())
Response.ContentEncoding = Encoding.UTF8
Response.BinaryWrite(Encoding.UTF8.GetPreamble())
Dim strStyle As String = "<style>.text {mso-number-format:\@; } </style>" ' to help retain leading zeroes
Response.Write(strStyle)
Response.Write("<H4>")
Response.Write("Performance Metrics " & Date.Now.ToString("d"))
Response.Write("</H4>")
Response.Write("<BR>")
Dim sw As StringWriter = New StringWriter()
Dim hw As HtmlTextWriter = New HtmlTextWriter(sw)
Chart1.RenderControl(hw)
Response.Write(sw.ToString())
Response.End()
Response.[End]()
End Sub