方法一:

public void ToExcel(){

//第一步:获取模版物理路径

string file_1 = Server.MapPath("/Content/Excel/downExcel.xls");

//第二步: 写入客户端

System.IO.FileInfo filet = new System.IO.FileInfo(file_1);

Response.Clear();

Response.Charset = "GB2312";

Response.ContentEncoding = System.Text.Encoding.UTF8;

// 添加头信息,为"文件下载/另存为"对话框指定默认文件名

if (file_0.IndexOf(".xlsx") > 0){

Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode("订单详情.xlsx"));

}

else if (file_0.IndexOf(".xls") > 0){

Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode("订单详情.xls"));

}

// 添加头信息,指定文件大小,让浏览器能够显示下载进度

Response.AddHeader("Content-Length", filet.Length.ToString());

// 指定返回的是一个不能被客户端读取的流,必须被下载

Response.ContentType = "application/ms-excel";

// 把文件流发送到客户端

Response.WriteFile(filet.FullName);

// 停止页面的执行

Response.End();

}

方法二:

public void  excelModel(){

//第一步:获取模版物理路径

string path= Server.MapPath("/Content/Excel/downExcel.xls");

//第二步:写入客户端

FileStream fs = new FileStream(path, FileMode.Open);

byte[] bytes = new byte[(int)fs.Length];

fs.Read(bytes, 0, bytes.Length);

fs.Close();

Response.Charset = "UTF-8";

Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");

Response.ContentType = "application/octet-stream";

Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));

Response.BinaryWrite(bytes);

Response.Flush();

Response.End();

return new EmptyResult();

}

最新文章

  1. OSG 3D场景渲染编程概述
  2. 深入剖析tomcat之一个简单的servlet容器
  3. iOS之UICollectionView详解
  4. (原创)JAVA多线程二线程池
  5. Matlab学习笔记 figure函数
  6. checkstyle配置文件说明
  7. Autofac全面解析系列(版本:3.5) – [使用篇(推荐篇):2.解析获取]
  8. Service(一)----->简单计算
  9. spring读取prperties配置文件(1)
  10. # 欢迎使用Markdown编辑器写博客
  11. while求1+2+3+4+5+6....100的和
  12. 【Oracle RAC】Linux系统Oracle11gR2 RAC安装配置详细过程V3.1(图文并茂)
  13. Eclipse各个版本区别
  14. 笔记:Javascript 会提升变量声明
  15. 测试教程网.unittest教程.5. 实例: 找出所有是弱密码的用户
  16. mysql 5.6 binlog组提交实现原理(转载)
  17. SpringMvc 启动原理源码分析
  18. BIOS、MBR、UEFI和GPT关系
  19. 『cs231n』线性分类器损失函数
  20. 发现视口(窗口)自适应的新大陆!!vw、vh

热门文章

  1. pl/sql 过程 函数(写一个过程,输入部门编号,在控制台打印这个部门的名称,总人数,平均工资(基本工资+奖金))
  2. LeetCode 73. 矩阵置零(Set Matrix Zeroes)
  3. c 使用lua 示例
  4. koa 基础(十)原生node.js 在 koa 中获取表单提交的数据
  5. 20 Django REST Framework 更改PUT/PATCH/DELETE的传参字段,默认为pk
  6. AWS EC2 外网不能访问的坑
  7. Python3命名规范
  8. linux常用命令(12)head命令
  9. 使用robotframework做接口测试4——搞定接口签名及密码加密
  10. office web apps安装部署,配置https,负载均衡(四)安装office web apps相关软件