我想做一个onclick函数,它下载一个excel文件,当用户单击一个按钮时也会创建该文件,所以我找到了用于onclick的这个函数。
public void CreateExcel(object sender, EventArgs e)
{
DataTable dt;
dt = Database.Get_All_Approval_Table_Grouped
string attachment = "attachment; filename=example.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/vnd.ms-excel";
string tab = "";
foreach (DataColumn dc in dt.Columns)
{
Response.Write(tab + dc.ColumnName);
tab = "\t";
}
Response.Write("\n");
int i;
foreach (DataRow dr in dt.Rows)
{
tab = "";
for (i = 0; i < dt.Columns.Count; i++)
{
Response.Write(tab + dr[i].ToString());
tab = "\t";
}
Response.Write("\n");
}
Response.End();
}以下是我收到的答复:
Line: 885
Error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed.发布于 2015-06-15 20:17:00
错误是因为您没有下载Excel文件,而是在为ASP.net WebForms页面提供服务。
你是modifying the response out from under WebForms。
而且,那不是Excel
即使您解决了这个问题(通过使用.ashx处理程序提供原始内容),Excel也会拒绝它。
您的服务器指示内容类型的是application/vnd.ms-excel。
Response.ContentType = "application/vnd.ms-excel";只不过它不是文件的。当Excel试图解压缩.xlsx文件时,它将失败,并发出安全警告,提示内容与指示的类型不匹配。
https://stackoverflow.com/questions/30852284
复制相似问题