导出Excel

本文介绍了一种使用ASP.NET导出Excel文件的方法,通过设置HTTP响应头来触发浏览器的下载对话框,实现将服务器上的Excel文件直接提供给用户下载。此过程包括了对文件名进行编码以防出现乱码问题、设置响应类型为application/ms-excel以及通过二进制方式读取并发送文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       改代码段主要是弹出导出Excel的对话框:

       string headerEncodeStr = "GB2312";
        string contentEncodeStr = "GB2312";
        string filePath = Server.MapPath("../TemplateFiles/CarInformation.xls");
        string fileName = "物品信息";// "CarInformation";
       
        fileName = HttpUtility.UrlEncode(fileName);//防止导出文件名是乱码
        System.Web.HttpContext currContent = System.Web.HttpContext.Current;
        currContent.Response.HeaderEncoding = System.Text.Encoding.GetEncoding(headerEncodeStr);
        currContent.Response.AddHeader("Content-Disposition", "inline;attachment;filename=" + fileName + ".xls");
        currContent.Response.ContentEncoding = System.Text.Encoding.GetEncoding(contentEncodeStr);
        currContent.Response.Charset = contentEncodeStr;
        currContent.Response.ContentType = "application/ms-excel";
        currContent.Response.BinaryWrite(System.IO.File.ReadAllBytes(filePath)); //读取二进制文件输出
        currContent.Response.End();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值