加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net 导出,文件下载

发布时间:2020-12-16 07:11:16 所属栏目:asp.Net 来源:网络整理
导读:--- asp.net? MVC 导出excel? ?https://www.cnblogs.com/zhangchunyu/articles/5805008.html ---------- asp.net web导出,不用保存临时文件 NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();//添加一个sheetNPOI.SS.UserM

--- asp.net? MVC 导出excel? ?https://www.cnblogs.com/zhangchunyu/articles/5805008.html
---------- asp.net web导出,不用保存临时文件

NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
//添加一个sheet
NPOI.SS.UserModel.ISheet sheet1 = book.CreateSheet("第一个sheet");
//给sheet1添加第一行的头部标题
NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow(0);
row1.CreateCell(0).SetCellValue("I2D");
row1.CreateCell(1).SetCellValue("用户姓名");
row1.CreateCell(2).SetCellValue("电话");
row1.CreateCell(3).SetCellValue("注册时间");
row1.CreateCell(4).SetCellValue("邀请人ID");
row1.CreateCell(5).SetCellValue("邀请人名称");
row1.CreateCell(6).SetCellValue("邀请人电话");
row1.CreateCell(7).SetCellValue("总积分");
row1.CreateCell(8).SetCellValue("已使用积分");
row1.CreateCell(9).SetCellValue("可用积分");
//将数据逐步写入sheet1各个行
for (int i = 0; i < 11; i++)
{
}

//System.Web.HttpContext.Current.Response.ContentType = 
"application/vnd.ms-excel";
////通知浏览器下载文件而不是打开
context.Response.AddHeader("Content-Disposition","attachment; filename=" + 
HttpUtility.UrlEncode("测试期初数据.xls",System.Text.Encoding.UTF8));
using (MemoryStream ms = new MemoryStream())
{
//将工作簿的内容放到内存流中 
book.Write(ms);
//将内存流转换成字节数组发送到客户端 
System.Web.HttpContext.Current.Response.BinaryWrite(ms.ToArray());
System.Web.HttpContext.Current.Response.End();
book = null;
}

 

  ---文件下载

  string fileName = dt.Rows[0][0].ToString() + "期初数据.xlsx";//客户端保存的文件名
        string filePath = context.Server.MapPath("/Upload/uploadBeginData/" + OUID + ".xlsx");//路径
        if (!string.IsNullOrEmpty(filePath) && System.IO.File.Exists(filePath))
        {
            //以字符流的形式下载文件
            System.IO.FileInfo file = new System.IO.FileInfo(filePath);
            context.Response.Clear();
            context.Response.Charset = "UTF-8";
            context.Response.ContentEncoding = System.Text.Encoding.UTF8;
            context.Response.AddHeader("Content-Type","application/octet-stream");
            // 添加头信息,为"文件下载/另存为"对话框指定默认文件名,设定编码为UTF8,防止中文文件名出现乱码
            context.Response.AddHeader("Content-Disposition","attachment; filename=" + System.Web.HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8));
            // 添加头信息,指定文件大小,让浏览器能够显示下载进度
            context.Response.AddHeader("Content-Length",file.Length.ToString());
            //// 指定返回的是一个不能被客户端读取的流,必须被下载
            context.Response.ContentType = "application/ms-excel";
            // 把文件流发送到客户端
            context.Response.WriteFile(file.FullName);
            // 停止页面的执行
            context.Response.End();
        }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读