将GridView转储到Excel文件从Internet上下载/打开的方法最近被新的Windows更新中断。从互联网上打开excel文件打开一个空白excel窗口
我的代码使用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中的组策略/设置/用户配置。
这只能由用户在管道的另一端被应用,而不是人人都愿意采取这种方式。对我而言,这是一种解决方法,而不是解决方案。 – Paul
@Paul老实说,我发现的唯一真正的解决方案是不再使用这块代码。如果任何人都可以改变这个代码并使其工作,我会接受该答案。 –
@paul我将此作为Q/A风格提交以回答我自己的问题以分享我的知识。我编辑了这个问题,目的是为了更好地描述我的情况以及我在用户和Intranet环境中运行的代码的访问类型。这对我来说是一个解决方案。 –