Ajax 导出Excel 方式

本文介绍四种实现Excel文件导出的方法:使用iframe加载GET请求、利用form表单POST数据、通过click对象触发下载以及服务端响应内容类型实现。适用于前后端开发人员学习。

1.使用iframe 加载 使用get方式

<iframe id="comdownshow" height="0" width="0" frameborder="0" scrolling="no"></iframe>

2.使用from表单组装 使用post方式

jQuery.download = function (url, method, Name) {
jQuery('<form action="' + url + '" method="' + (method || 'post') + '">' +
'<input type="text" name="AddTimeStrat" value=""/>' +
'<input type="text" name="AddTimeEnd" value=""/>' +
'<input type="text" name="Name" value="' + Name + '"/>' +
'</form>')
.appendTo('body').submit().remove();
};

3.click对象触发

function Getexcel() {

//$("#comdownshow").attr("src", "/ModuleContext/EventAccount/Handler/EventAccountManage.ashx?type=ZHZXTJDOWN&AddTimeStrat=" + $("#txtAddTimeStrat").val() + "&AddTimeEnd=" + $("#txtAddTimeEnd").val() + "&Name=" + $("#txtName").val() + "&t=" + new Date().getTime());

$.download("/ModuleContext/EventAccount/Handler/EventAccountManage.ashx?type=ZHZXTJDOWN","post","城管科");
}

4.服务端代码ashx

string fileName = System.Web.HttpUtility.UrlEncode(name + ".xls");
context.Response.ContentType = "application/vnd.ms-excel";
context.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", fileName));
context.Response.Clear();
context.Response.BinaryWrite(ExportStaticDept(list).GetBuffer());

//ExportStaticDept 生成excel => public MemoryStream ExportStaticDept .....

转载于:https://www.cnblogs.com/linsu/p/7418361.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值