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.
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:
Solution 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))
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"?
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... =(
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.
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?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.