Asp.net
從 Internet 打開 excel 文件會打開一個空白的 excel 視窗
最近,新的 Windows 更新破壞了將 GridView 轉儲到 Excel 文件以從 Internet 下載/打開的方法。
我的程式碼使用 StringWriter、HTMLTextWriter 和 RenderControl 從 GridView 轉儲到 XLS 文件。使用來自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 SubExcel (2013) 將打開一個空白視窗,沒有任何警告或消息說明任何內容被阻止的原因,並且沒有接受打開文件的選項。
我的程式碼在 Intranet 站點上執行,並且我可以訪問 Windows 中的組策略/設置/使用者配置。
解決方案 1
打開 Excel 轉到文件選項
點擊信任中心 -> 信任中心設置
轉到受保護的視圖。有 3 個選項顯示全部點擊。取消選中第一個選項——“為來自 Internet 的文件啟用受保護的視圖”。在某些情況下,如以下評論中所述,需要取消選中第一個和第二個選項(感謝@mosheb)
解決方案 2
解除安裝這些 Windows 更新:
- Windows 更新 KB3115262 (Excel 2013)
- Windows 更新 KB3115130 (Excel 2010)