zoukankan      html  css  js  c++  java
  • 将list导出成Excel到浏览器

                System.Data.DataTable dtSource = ToDataTable<EmpExtension>(empList);
    
                Response.ContentType = "application/vnd.ms-excel";
                Response.ContentEncoding = Encoding.UTF8;
                Response.Charset = "";
                Response.AppendHeader("Content-Disposition",
                    "attachment;filename=" + strFileName);  //路径:HttpUtility.UrlEncode(strFileName, Encoding.UTF8)
    
                XlsDocument xls = new XlsDocument();
                Worksheet sheet = xls.Workbook.Worksheets.Add(strFileName);
    
    
                if (dtSource == null || dtSource.Rows.Count == 0) { return; }
                //XlsDocument xls = new XlsDocument();
                //Worksheet sheet = xls.Workbook.Worksheets.Add(sheetName);
    
                //填充表头  
                foreach (DataColumn col in dtSource.Columns)
                {
                    sheet.Cells.Add(1, col.Ordinal + 1, col.ColumnName);
                }
    
                //填充内容  
                for (int i = 0; i < dtSource.Rows.Count; i++)
                {
                    for (int j = 0; j < dtSource.Columns.Count; j++)
                    {
                        sheet.Cells.Add(i + 2, j + 1, dtSource.Rows[i][j].ToString());
                    }
                }
    
                using (MemoryStream ms = new MemoryStream())
                {
                    xls.Save(ms);
                    ms.Flush();
                    ms.Position = 0;
                    sheet = null;
                    xls = null;
                    HttpResponse response = System.Web.HttpContext.Current.Response;
                    response.Clear();
    
                    response.Charset = "UTF-8";
                    response.ContentType = "application/vnd.ms-excel";//"application/vnd.ms-excel";
                    System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment; filename=" + strFileName));
                    //System.Web.HttpContext.Current.Response.WriteFile(fi.FullName);
                    byte[] data = ms.ToArray();
                    System.Web.HttpContext.Current.Response.BinaryWrite(data);
    
                }

    这里使用到了MyXls

    然后前端直接使用

    window.open("........")

    即可

    记录编程的点滴,体会学习的乐趣
  • 相关阅读:
    实参和形参
    location对象
    区别 apply,call
    窗体之间的交互(window.opener)
    我的升级脚本总结
    Create elements
    history 对象
    函数参数的属性:callee
    发布app store流程
    【转】如何生成静态页面的五种方案
  • 原文地址:https://www.cnblogs.com/AduBlog/p/14692626.html
Copyright © 2011-2022 走看看