• excel导出功能第二种方法


      public static void Export(DataTable Dt, string FileName, HttpResponse Response, bool AppendHeader)
            {
                StringBuilder sb = new StringBuilder();
                int countOfColumns = Dt.Columns.Count;
                int countOfRows = Dt.Rows.Count;
                if (AppendHeader)
                {
                    for (int indexOfCl = 0; indexOfCl < countOfColumns - 1; indexOfCl++)
                    {
                        sb.Append(Dt.Columns[indexOfCl] + "\t");
                    }
                    if (countOfColumns > 0)
                    {
                        sb.Append(Dt.Columns[countOfColumns - 1] + "\n");
                    }
                }
                for (int indexOfRow = 0; indexOfRow < countOfRows; indexOfRow++)
                {
                    for (int indexOfCl = 0; indexOfCl < countOfColumns; indexOfCl++)
                    {
                        sb.Append(Dt.Rows[indexOfRow][indexOfCl] + "\t");
                    }
                    sb.Append("\n");
                }
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.ContentType = "application/octet-stream";
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(Convert.ToString(FileName), System.Text.Encoding.UTF8));
                Response.Write(sb.ToString());
                Response.Flush();
                Response.End();
            }
    
     var data1 = from f in siteData.已投稿列表
                                    where (BeginTime.Value.Trim() != "" && EndTime.Value.Trim() != "") ?
                                    (f.SubmittedDate >= Convert.ToDateTime(BeginTime.Value.Trim()))
                                    && f.SubmittedDate <= Convert.ToDateTime(EndTime.Value.Trim()).AddDays(1) : 1 == 1
                                    where f.Department == UserDept
                                    group f by f.Department into g
                                    where keyword != "" ? g.Key != null ? g.Key.Contains(keyword) : 3 == 3 : 2 == 2
                                    orderby g.Count() descending
                                    select new
                                    {
                                        类别 = g.Key == null ? "无部门" : g.Key,
                                        文章次数 = g.Count(),
                                        已采纳数 = g.Count(o => o.HandleState == "实际采用")
                                    };
                        if (data1 != null)
                        {
                            DataTable dt = new DataTable();
                            dt.Columns.Add("部门", typeof(string));
                            dt.Columns.Add("投稿数量", typeof(string));
                            dt.Columns.Add("已采纳数", typeof(string));
                            dt.Rows.Add(new object[] { "部门", "投稿数量", "已采纳数" });
                            for (int i = 0; i < data1.Count(); i++)
                            {
                                dt.Rows.Add(new object[] { data1.ElementAt(i).类别, data1.ElementAt(i).文章次数, data1.ElementAt(i).已采纳数 });
    
                            }
                            CoolFuncs.Export(dt, "部门投稿统计.xls", Response, false);
                        }
    
  • 相关阅读:
    properties,yml 文件读取 pom.xml 文件变量
    Docker实战编写Dockerfile
    在SpringBoot中实现异步事件驱动
    HttpClient封装工具类
    oracle临时表的两种方式
    关于cxGrid选中行操作关联数据集的一种方法
    安全释放 TreeView的DATA!
    行字段值拼接成字符串
    delphi中遍历枚举类型的方法
    C#将XML字符串转换成实体对象,并去除cdata
  • 原文地址:https://www.cnblogs.com/double2012/p/2872649.html
Copyright © 2020-2023  润新知