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

    即可

    记录编程的点滴,体会学习的乐趣
  • 相关阅读:
    数据分析day04
    数据分析day03
    数据分析 day02
    Index of /android/repository
    WPF ListView 数据懒加载
    WPF TextBlock 文字超长截断并显示省略符号
    android adb socket 通信
    android 获取 cpu 频率信息
    Android 联系人导入导出(VCard格式)
    Android Contact 导入导出 vcf格式(不依赖第三方库)
  • 原文地址:https://www.cnblogs.com/AduBlog/p/14692626.html
Copyright © 2011-2022 走看看