none
IE7, IE8 can't export to excel in ASP.Net3.5, but works on localhost RRS feed

  • Question

  • When trying to export a file to excel in IE7 and IE8 I receive the error of;

    Unable to download #filename from #siteaddress Unable to open this internet site. The requested site is either unavailable or cannot be found. Please try again later.

    The thing of it is, is that I put in a quick fix I found online and when I run it locally in IE8 or IE7 it works correctly, is there something i'm missing why this would be working in my localhost but not when released/published to the web?

    Below is my code for the export, which works in all other versions of IE, as well as in Chrome and Firefox.

    Please Help!

    Thank you,

    Nick G

    Shared Sub ExportExcelWithDataFormat(ByVal dt As DataTable, ByVal pageObject As System.Web.UI.Page, ByVal cellNumberFormat As Int32)
        Dim style As String = "<style>.textmode{mso-number-format:\@;}</style>"
        Dim stringWrite As New System.IO.StringWriter
        Dim dg As New GridView
    
        'first let's clean up the response.object
        pageObject.Response.Clear()
        pageObject.Response.ClearContent() <---New
        pageObject.Response.ClearHeaders() <---New
        pageObject.Response.Charset = ""
        'set the response mime type for excel
        pageObject.Response.AddHeader("Content-Disposition", "attachment; filename=360Export.xls")
        pageObject.Response.ContentType = "application/unknown"  <--- changed to application/unknown instead of msexcel
    
        'create an htmltextwriter which uses the stringwriter
        Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)
    
        'set the datagrid datasource to the dataset passed in
        Dim CurrentPage As String = System.Web.HttpContext.Current.Request.Url.AbsolutePath
    
        If CurrentPage.ToUpper.Contains("TRANSACTIONS.ASPX") Then
            dt.DefaultView.Sort = "Date desc"
        End If
    
        dg.DataSource = dt
        'bind the datagrid
        dg.DataBind()
    
        For i As Int32 = 0 To dg.Rows.Count - 1
            'Apply text style to each Row  
            'For x As Int32 = 0 To dg.Rows(i).Cells.Count - 1
            If cellNumberFormat > -1 Then
                dg.Rows(i).Cells(cellNumberFormat).Attributes.Add("class", "textmode")
            End If
            'Next
        Next
    
        'tell the datagrid to render itself to our htmltextwriter
        dg.RenderControl(htmlWrite)
    
        pageObject.Response.Write(style)
    
        'all that's left is to output the html
        pageObject.Response.Write(stringWrite.ToString)
        pageObject.Response.End()
        'Return stringWrite.ToString
    End Sub

    Wednesday, September 18, 2013 3:30 PM