问题
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