how to export excel without EnableEventValidation=“false” because of lock issues in excel

柔情痞子 提交于 2019-12-19 11:25:01

问题


I have a code for exporting excel on button click:

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 = @"<style> .textmode { } </style>";
                Response.Write(style);
                Response.Output.Write(sw.ToString());
                Response.Flush();
                Response.End();
            }
        }
        catch(Exception exp)
        {
            string st = exp.Message;
        }
    }

I was getting error:

RegisterForEventValidation can only be called during Render();

and what i did is:

enableEventValidation="false" on the page_name.aspx page

The export to excel worked fine after this on windows 7.

The issue came on windows 8, when i downloaded the excel on a windows 8 system's browser, though the file did downloaded completely, but on opening the file, it showed up with a error message that the file is corrupted, and i suppose it is because of disabling event validation.

when i checked the properties of that file, then it was locked up due to security reasons. I unlocked it from there, and i could open up the file then.

so is there any way to tackle the error

RegisterForEventValidation can only be called during Render();

without enableEventValidation="false" or tell me if i am missing something.

来源:https://stackoverflow.com/questions/40629644/how-to-export-excel-without-enableeventvalidation-false-because-of-lock-issues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!