繁体   English   中英

从ashx文件导出到Excel

[英]Export to Excel from ashx file

我试图从jquery事件调用的.ashx文件导出到excel。 我的数据准备正确。 但系统不会提示我保存。 如果我使用asp.net click事件,它将很好地工作,但是从jquery来看,数据将返回到调用jquery事件。 出于无法解释的原因,我必须使用ashx代码。 我需要做什么?

context.Response.ClearHeaders();
context.Response.Clear();
context.Response.ClearContent();
string attachment = "attachment; filename='UserRolesPermissions.xls'";
context.Response.AddHeader("content-disposition", attachment);
context.Response.ContentType = "application/vnd.ms-excel";
context.Response.Write(excelData);
context.Response.Flush(); 
context.ApplicationInstance.CompleteRequest();

我已经尝试过使用Flush,End和CompleteRequest进行各种组合。 结果相同(错误结束)。

谢谢。

似乎您缺少content-length标头。 这可能会有所作为。

//make sure the data is a byte array
byte[] bin = excelData;

context.Response.ClearHeaders();
context.Response.Clear();
context.Response.Buffer = true;
context.Response.ContentType = "application/vnd.ms-excel";
context.Response.AddHeader("content-disposition", "attachment; filename=\"UserRolesPermissions.xls\"");

//this could help
context.Response.AddHeader("content-length", bin.Length.ToString());
context.Response.OutputStream.Write(bin, 0, bin.Length);

context.Response.Flush();
context.ApplicationInstance.CompleteRequest();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM