コード例 #1
0
        /// <summary>
        /// 获取所有用户信息
        /// </summary>
        private void ExportToExcel()
        {
            Response.Clear();
            Response.ContentEncoding = Encoding.UTF8;
            string fileName = string.Format("Users[{0:yyyyMMdd}].xls", DateTime.Now);

            if (Request.UserAgent.ToLower().IndexOf("msie") > -1)
            {
                fileName = HttpUtility.UrlPathEncode(fileName);
            }
            if (Request.UserAgent.ToLower().IndexOf("firefox") > -1)
            {
                Response.AddHeader("Content-Type", "application/vnd.ms-excel");
                Response.AddHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
            }
            else
            {
                Response.AddHeader("Content-Type", "application/vnd.ms-excel");
                Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
            }
            Dictionary <int, HashSet <int> > userSofts;
            List <Soft> allSofts;
            List <User> users = new URBasicInfoService().GetReportUsers(out userSofts, out allSofts);

            Response.Write(@"<table border=""1"" cellpadding=""0"" cellspacing=""0"">");
            Response.Write(@"<tr style=""text-align:center;font-weight:bold;""><td>ID</td><td>用户名</td><td>真实姓名</td><td>类型</td><td>邮箱</td><td>部门</td><td>最后登录时间</td><td>状态</td>");
            foreach (Soft soft in allSofts)
            {
                Response.Write(string.Format("<td>{0}</td>", soft.Name));
            }
            Response.Write("</tr>");
            foreach (User user in users)
            {
                Response.Write(string.Format(@"<tr style=""text-align:center;""><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td>{5}</td><td>{6:yyyy-MM-dd HH:mm:ss}</td><td>{7}</td>"
                                             , user.ID
                                             , user.Account
                                             , user.TrueName
                                             , URBasicInfoService.GetUserTypeDescipt(user.AccountType)
                                             , user.Email
                                             , user.Department
                                             , user.LastLoginTime
                                             , user.Status == StatusOptions.Valid ? "启用" : "禁用"));
                foreach (Soft soft in allSofts)
                {
                    if (userSofts.ContainsKey(user.ID) && userSofts[user.ID].Contains(soft.ID))
                    {
                        Response.Write("<td>√</td>");
                    }
                    else
                    {
                        Response.Write("<td></td>");
                    }
                }
                Response.Write("</tr>");
            }
            Response.Write("</table>");
            Response.Flush();
        }