7

A method in dumping a GridView to an Excel file to download/open from the internet was recently broken with new Windows Updates.

My code dumps from a GridView to an XLS file using StringWriter, HTMLTextWriter and RenderControl. A common approach using the following code from http://www.aspsnippets.com/Articles/Export-GridView-to-Excel-in-ASPNet-with-Formatting-using-C-and-VBNet.aspx

Protected Sub ExportToExcel(sender As Object, e As EventArgs)
    Response.Clear()
    Response.Buffer = True
    Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls")
    Response.Charset = ""
    Response.ContentType = "application/vnd.ms-excel"
    Using sw As New StringWriter()
        Dim hw As New HtmlTextWriter(sw)

        'To Export all pages
        GridView1.AllowPaging = False
        Me.BindGrid()

        GridView1.HeaderRow.BackColor = Color.White
        For Each cell As TableCell In GridView1.HeaderRow.Cells
            cell.BackColor = GridView1.HeaderStyle.BackColor
        Next
        For Each row As GridViewRow In GridView1.Rows
            row.BackColor = Color.White
            For Each cell As TableCell In row.Cells
                If row.RowIndex Mod 2 = 0 Then
                    cell.BackColor = GridView1.AlternatingRowStyle.BackColor
                Else
                    cell.BackColor = GridView1.RowStyle.BackColor
                End If
                cell.CssClass = "textmode"
            Next
        Next

        GridView1.RenderControl(hw)
        'style to format numbers to string
        Dim style As String = "<style> .textmode { } </style>"
        Response.Write(style)
        Response.Output.Write(sw.ToString())
        Response.Flush()
        Response.[End]()
    End Using
End Sub

Public Overrides Sub VerifyRenderingInServerForm(control As Control)
    ' Verifies that the control is rendered
End Sub

Excel (2013) will open to a blank window, with no warning or message as to why anything was blocked, and without option to accept the file to open.

My code is run on an intranet site, and I do have access to group policies / settings / user configurations in Windows.

Taylor Brown
  • 1,480
  • 1
  • 16
  • 29

7 Answers7

8

Solution 1

1) Open Excel Go to File Options

2) Click Trust Center -> Trust Center Settings

3) Go to Protected View. there are 3 options that show that were all clicked. Uncheck the first option that reads -- "Enable Protected View for files originating from the Internet". In some cases as reported in the comments below both the 1st and 2nd options need to be unchecked (Thanks @mosheb)

Solution 2

Uninstall these Windows Updates:

  • Windows Update KB3115262 (Excel 2013)
  • Windows Update KB3115130 (Excel 2010)
Taylor Brown
  • 1,480
  • 1
  • 16
  • 29
  • 1
    This can only be applied by the user at the other end of the pipe, and not everyone will be willing to take this approach. To me, this is a one off workaround and not a solution. – Paul Jul 27 '16 at 07:48
  • @Paul Honestly the only true solution I have found is to not use this block of code anymore. If anyone can alter this code and make it work I will accept that answer instead. – Taylor Brown Jul 27 '16 at 19:52
  • @paul I submitted this as Q/A style to answer my own question to share my knowledge. I have edited the question in an attempt to better describe what my situation was and the types of access I have over my users and code in the intranet environment this runs in. This has served as a solution for me given my situation. – Taylor Brown Jul 27 '16 at 19:59
  • 2
    We had to uncheck the first AND the second to get this working on our intranet – mosheb Aug 01 '16 at 14:19
  • thank you for this. for the others this will save their time. – Denver Oct 04 '16 at 16:06
3

Solution 3

  • Go into the properties of the file (R click - properties)
  • Click 'Unblock'
  • Click 'Apply'
  • As with the accepted solution, this can only be applied by the user at the request end of the pipe. I don't really see this as a solution. – Paul Jul 27 '16 at 07:47
3

changing the security options was unfortunately not an option, but it turns out that if you export to CSV instead of XLS then the file will open in Excel ignoring the trust center stuff.

We're in Classic ASP, so we change the page from exporting an HTML table format to CSV and changed our header and contenttype to this:

Response.AddHeader "content-disposition", "attachment; filename=search_results.csv"
Response.ContentType = "text/csv"

and did line breaks with: Response.Write (chr(10))

Paul
  • 3,962
  • 2
  • 23
  • 49
user713813
  • 691
  • 1
  • 8
  • 18
  • 1
    The code in the question does apply some formatting (setting backcolor) but for your requirements, and probably many others, this is a great solution as well. I am sure I have a few places where I can apply this (companies outside of our intranet where we can't just uninstall windows updates etc.) Thank you! – Taylor Brown Jul 21 '16 at 20:36
  • 1
    How did you change your page to output to .csv? As a test, I took an .xls that was outputted by my website, copied it, and renamed the extension to ".csv". Opening it showed all the HTML tags and it was messy looking. I'm looking at possibly using this .csv format as a solution to this blank Excel window problem when opening the document during export in Internet Explorer, but I'm unsure of how to change my existing Excel export to .csv and not have it look horrible. – clamum Jul 29 '16 at 20:20
  • @clamum I think you're right, just changing the content type output does not serve as a solution. I think there is quite a bit of code changes missing from this answer. Correct me if I am wrong. – Taylor Brown Aug 01 '16 at 18:11
  • 1
    @taybriz That was my conclusion after doing my little copy-paste-rename test before. Maybe the OP can hopefully update us. I gave my manager a couple options for updating our website to fix this issue and I advised him that we should rewrite the Excel export using the EPPlus open source library. It'll take me several days, estimated, but I think it'll fix it. – clamum Aug 02 '16 at 15:32
2

Try doing following:

Replace "attachment" from

Response.AddHeader("content-disposition","attachment;filename=GridViewExport.xls")

with "inline"

Response.AddHeader("content-disposition","inline;filename=GridViewExport.xls")

It may help!

Content-Disposition:What are the differences between "inline" and "attachment"?

Community
  • 1
  • 1
1

Just thought I'd mention there is a third solution:

Add the site generating the .xls to your Trusted Sites list. I had to add about a dozen websites via GPO, because our CIO is refusing to roll back the KB... =(

Raniel66
  • 9
  • 2
1

Per Raniel66's suggestion, if the excel opens as blank even after adding the site to your Trusted Sites list, you may try/suggest below work around.

Open excel workbook even though it is grayed out/blank, then click on ‘View’ tab in the excel and then click on 'full screen/maximize screen icon' as shown in screen shot below. This is what worked for me.

enter image description here

KRM
  • 965
  • 1
  • 10
  • 24
-1

Having same issue. An update blocked this exporting to excel activity and not all of my world wide users have permissions to unblock. It affects 2010 and 2013 .xls. My thought it to address the Content type. Have you tried switching your Content Type from application/vnd.ms-excel to application/vnd.openxmlformats-officedocument.spreadsheetml.sheet and using xlsx instead?

CSEM
  • 1