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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38463226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:59:25  来源:igfitidea点击:

Opening excel files from the internet opens a blank excel window

asp.netvb.netexcelintranet

提问by Taylor Brown

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

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

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

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

Excel (2013) 将打开一个空白窗口,没有任何关于为什么被阻止的警告或消息,也没有接受文件打开的选项。

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

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

回答by Taylor Brown

Solution 1

方案一

1) Open Excel Go to File Options

1) 打开 Excel 转到文件选项

2) Click Trust Center -> Trust Center Settings

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

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)

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

Solution 2

解决方案2

Uninstall these Windows Updates:

卸载这些 Windows 更新:

  • Windows Update KB3115262 (Excel 2013)
  • Windows Update KB3115130 (Excel 2010)
  • Windows 更新 KB3115262 (Excel 2013)
  • Windows 更新 KB3115130 (Excel 2010)

回答by Josh Bucklen

Solution 3

解决方案3

  • Go into the properties of the file (R click - properties)
  • Click 'Unblock'
  • Click 'Apply'
  • 进入文件的属性(R 单击 - 属性)
  • 点击“解除封锁”
  • 点击“申请”

回答by user713813

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.

不幸的是,更改安全选项不是一个选项,但事实证明,如果您导出为 CSV 而不是 XLS,那么文件将在 Excel 中打开,而忽略信任中心的内容。

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:

我们使用的是 Classic ASP,因此我们将页面从 HTML 表格格式导出为 CSV,并将标题和内容类型更改为:

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

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

并用以下方法换行: Response.Write (chr(10))

回答by Akash Agrawal

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"?

Content-Disposition:“内联”和“附件”有什么区别?

回答by KRM

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

根据Raniel66 的建议,如果即使在将站点添加到受信任站点列表后,excel 仍然打开为空白,您可以尝试/建议以下解决方法。

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.

打开 excel 工作簿,即使它是灰色/空白的,然后单击excel 中的“查看”选项卡,然后单击“全屏/最大化屏幕图标”,如下面的屏幕截图所示。这对我有用。

enter image description here

在此处输入图片说明

回答by Raniel66

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... =(

将生成 .xls 的站点添加到您的受信任站点列表。我不得不通过 GPO 添加大约十几个网站,因为我们的 CIO 拒绝回滚 KB... =(

回答by CSEM

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?

有同样的问题。更新阻止了此导出到 Excel 活动,并且并非我的所有全球用户都有权取消阻止。它会影响 2010 和 2013 .xls。我认为它可以解决内容类型。您是否尝试过将内容类型从 application/vnd.ms-excel 切换到 application/vnd.openxmlformats-officedocument.spreadsheetml.sheet 并改用 xlsx?