如何在没有EnableEventValidation =“false”的情况下导出excel,因为excel中存在锁定问题

我有一个代码,用于在按钮点击时导出excel:

protected void btn_Excel_Click(object sender, EventArgs e) { try { empData1.ShowHeader = true; bindGrid(); Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment;filename=doc_name.xls"); Response.Charset = ""; Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; using (StringWriter sw = new StringWriter()) { HtmlTextWriter hw = new HtmlTextWriter(sw); foreach (TableCell cell in empData1.HeaderRow.Cells) { cell.ForeColor = System.Drawing.ColorTranslator.FromHtml("#fafafa"); cell.BackColor = System.Drawing.ColorTranslator.FromHtml("#ff5d51"); } empData1.RenderControl(hw); string style = @" .textmode { } "; Response.Write(style); Response.Output.Write(sw.ToString()); Response.Flush(); Response.End(); } } catch(Exception exp) { string st = exp.Message; } } 

我收到了错误:

 RegisterForEventValidation can only be called during Render(); 

我做的是:

page_name.aspx页面上的enableEventValidation="false"

在Windows 7之后,对excel的导出工作正常。

问题发生在Windows 8上,当我在Windows 8系统的浏览器上下载excel时,虽然文件确实完全下载了,但在打开文件时,它显示错误消息,文件已损坏,我想它是因为禁用事件validation。

当我检查该文件的属性时,由于安全原因它被锁定。 我从那里解锁它,然后我可以打开文件。

有没有办法解决这个错误

 RegisterForEventValidation can only be called during Render(); 

没有enableEventValidation="false"或告诉我,如果我错过了什么。