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("........")

    即可

    记录编程的点滴,体会学习的乐趣
  • 相关阅读:
    moment获取天的23时59分59秒可以用moment().endOf(String),以及获取天的0时0分0秒可以用moment().startOf('day')
    vue 去除输入框首位的空格
    管道
    事件广播
    iview在子组件中调用父组件的方法
    ZOJ 3430 Detect the Virus(AC自动机)
    HDU 3065 病毒侵袭持续中(AC自动机)
    HDU 2896 病毒侵袭(AC自动机)
    HDU 2222 Keywords Search(AC自动机)
    shell常用命令
  • 原文地址:https://www.cnblogs.com/AduBlog/p/14692626.html
Copyright © 2011-2022 走看看