k_ramprasad87 0 Newbie Poster
Shared Sub Export(ByVal recordSet As DataTable, ByVal fileName As String, Optional ByVal ExportData As Boolean = True, Optional ByVal IncludeDummy As Boolean = False, Optional ByVal header As Boolean = True)
            'Try


            Dim C_HTTP_HEADER_CONTENT As String = "Content-Disposition"
            Dim C_HTTP_ATTACHMENT As String = "attachment;filename="
            Dim C_HTTP_INLINE As String = "inline;filename="
            Dim C_HTTP_CONTENT_TYPE_OCTET As String = "application/octet-stream"
            Dim C_HTTP_CONTENT_TYPE_EXCEL As String = "application/vnd.xls"
            Dim C_HTTP_CONTENT_LENGTH As String = "Content-Length"
            Dim C_QUERY_PARAM_CRITERIA As String = "Criteria"
            Dim C_ERROR_NO_RESULT As String = "Data not found."
            Dim response As System.Web.HttpResponse = System.Web.HttpContext.Current.Response
            response.Clear()
            ' Add the header that specifies the default filename 
            ' for the Download/SaveAs dialog
            response.AddHeader(C_HTTP_HEADER_CONTENT, C_HTTP_ATTACHMENT & fileName & ".XLS")
            ' Specify that the response is a stream that cannot be read _
            ' by the client and must be downloaded
            response.ContentType = C_HTTP_CONTENT_TYPE_EXCEL
            Dim ExportContent As String
            ExportContent = ConvertDataViewToString(recordSet.DefaultView, "", vbTab, ExportData, IncludeDummy, header)
            If ExportContent.Length <= 0 Then
                ExportContent = C_ERROR_NO_RESULT
            End If

            Dim Encoding As New System.Text.UnicodeEncoding
            response.AddHeader(C_HTTP_CONTENT_LENGTH, Encoding.GetByteCount(ExportContent).ToString())
            response.BinaryWrite(Encoding.GetBytes(ExportContent))
            response.Charset = ""
            ' Stop execution of the current page
            response.End()
            'Catch ex As Exception

            'Finally
            '    'HttpContext.Current.ApplicationInstance.CompleteRequest()
            'End Try


        End Sub