asp.net - 从 Internet 打开 excel 文件会打开一个空白的 excel 窗口

标签 asp.net vb.net excel intranet

一种将 GridView 转储到 Excel 文件以从 Internet 下载/打开的方法最近被新的 Windows 更新破坏。

我的代码使用 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 Sub

Excel (2013) 将打开一个空白窗口,没有任何警告或消息说明任何内容被阻止的原因,并且没有接受打开文件的选项。

我的代码在 Intranet 站点上运行,并且我可以访问 Windows 中的组策略/设置/用户配置。

最佳答案

解决方案一

1) 打开 Excel 转到文件选项

2) 点击信任中心 -> 信任中心设置

3) 转到 protected View 。有 3 个选项显示全部点击。取消选中第一个选项——“为来自 Internet 的文件启用 protected View ”。在某些情况下,如以下评论中所述,第一个和第二个选项都需要取消选中(感谢@mosheb)

解决方案二

卸载这些 Windows 更新:

  • Windows 更新 KB3115262 (Excel 2013)
  • Windows 更新 KB3115130 (Excel 2010)
  • 关于asp.net - 从 Internet 打开 excel 文件会打开一个空白的 excel 窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38463226/

    相关文章:

    c# - 如何获得已经发布的 SAML 断言?

    asp.net - 包尝试添加对 GAC 中未找到的 System.Runtime 的引用

    asp.net - 作为单例的 Web 服务是否会导致不同用户出现问题?

    php - 当我将查询结果导出到 Excel CSV 时,始终显示 HTML 代码

    c# - 解决错误 "The ConnectionString property has not been initialized."?

    c# - ASP.NET 中的 MySQL 异常

    c++ - vb.net 和 c++/clr 数组长度

    vb.net - 添加全局函数

    vba - 修改用户表单以在多个工作表上工作

    c# - EPPlus - 导出带有前导单引号的字符串值