zoukankan      html  css  js  c++  java
  • .net 利用Response导出自定义Excel

          List<U> objList = new List<U>();
                   objList = BLL.GetInfo();//读取数据                      
                   
                    StringBuilder sb = new StringBuilder();
                    sb.Append("<style type=\"text/css\">");
                    sb.Append("<!--");
                    sb.Append(".text");
                    sb.Append("{mso-style-parent:style0;");
                    sb.Append("font-size:10.0pt;");
                    sb.Append("font-family:\"Arial Unicode MS\", sans-serif;");
                    sb.Append("mso-font-charset:0;");
                    sb.Append(@"mso-number-format:\@;");
                    sb.Append("text-align:center;");
                    sb.Append("border:.5pt solid black;");
                    sb.Append("white-space:normal;}");
                    sb.Append("-->");
                    sb.Append("</style>");
                    sb.Append("<table cellspacing=\"0\" rules=\"all\" border=\"1\" style=\"border-collapse:collapse;\">");
                    sb.Append("<tr align=\"Center\" style=\"font-weight:bold;\">");
                    sb.Append("<td>ID</td><td>用户名</td><td>真实姓名</td><td>省份</td><td>注册时间</td");
                    sb.Append("</tr>");
                    foreach (U item in objList)
                    {
                        sb.Append("<tr align=\"Center\"><td>" + item.id + "</td><td>" + item.uName + "</td><td>" + item.rName + "</td><td>" + item.proId + "</td><td>" + item.regDate + "</td></tr>");
                    }
                    sb.Append("</table>");
    
                    System.Web.HttpContext.Current.Response.Clear();
                    System.Web.HttpContext.Current.Response.Charset = "GB2312";
                    // System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
                    // 添加头信息,为"文件下载/另存为"对话框指定默认文件名 
                    System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=myU.xls");
                    // 添加头信息,指定文件大小,让浏览器能够显示下载进度 
                    // System.Web.HttpContext.Current.Response.AddHeader("Content-Length",sb.ToString());
    
                    // 指定返回的是一个不能被客户端读取的流,必须被下载 
                    System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";
    
                    // 把文件流发送到客户端 
                    System.Web.HttpContext.Current.Response.Write(sb.ToString());
                    // 停止页面的执行 
    
                    System.Web.HttpContext.Current.Response.End();         
    
  • 相关阅读:
    分布式文件系统-glusterfs
    Centos7防火墙使用
    Centos7使用Python3
    ldap认证服务的搭建
    kafka使用
    Python基本数据类型
    Linux 内核参数优化
    MHA+atlas(数据库的高可用与读写分离)
    插槽(slot)
    常用组件通信方式
  • 原文地址:https://www.cnblogs.com/wfwup/p/1651399.html
Copyright © 2011-2022 走看看