导出excel模版

方法一:

 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();

}  

posted @ 2017-03-21 22:17  雷雨天的雷胖子  阅读(164)  评论(0编辑  收藏  举报