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();         
    
  • 相关阅读:
    Abp通用配置模块的设计
    Windbg分析高内存占用问题
    IdentityServer4 知多少
    找不到对象,『空对象模式』来帮忙
    EF Core中避免贫血模型的三种行之有效的方法(翻译)
    Razor Page Library:开发独立通用RPL(内嵌wwwroot资源文件夹)
    .NET Core 源码导航(按程序集链接)
    ASP.NET Core Web App应用第三方Bootstrap模板
    给ASP.NET Core Web发布包做减法
    Azure Web连接到Azure MySql Db
  • 原文地址:https://www.cnblogs.com/wfwup/p/1651399.html
Copyright © 2011-2022 走看看