Excel 数据导出

Web  controller

  /// <summary>
/// 导出数据
/// </summary>
/// <param name="UserID"></param>
/// <param name="UserName"></param>
/// <param name="UserStatus"></param>
/// <param name="Gender"></param>
/// <param name="Mobile"></param>
/// <param name="bAddTime"></param>
/// <param name="eAddTime"></param>
/// <param name="bLastLoginTime"></param>
/// <param name="eLastLoginTime"></param>
public void CreateExcel(string UserID, string UserName, string UserStatus, string Gender, string Mobile, string bAddTime, string eAddTime, string bLastLoginTime, string eLastLoginTime)
{ SearchItem.UserSearchItem item = new SearchItem.UserSearchItem();
if (UserName != null && !UserName.Equals(""))
item.UserName = UserName;
if (UserID != null && !UserID.Equals("") && UserID != "undefined")
item.UserID = int.Parse(UserID);
//*******
if (UserStatus != null && !UserStatus.Equals("") && !UserStatus.Equals("null"))
item.UserStatus = int.Parse(UserStatus);
if (Gender != null && !Gender.Equals("") && !Gender.Equals("null"))
item.Gender = int.Parse(Gender);
if (Mobile != null && !Mobile.Equals(""))
item.Mobile = Mobile;
//************
if (bAddTime != "null" && bAddTime != null && !bAddTime.Equals("") && bAddTime != "undefined")
item.bAddTime = Convert.ToDateTime(bAddTime);
if (eAddTime != null && !eAddTime.Equals("") && eAddTime != "undefined")
item.eAddTime = Convert.ToDateTime(eAddTime);
if (bLastLoginTime != null && !bLastLoginTime.Equals("") && bLastLoginTime != "undefined")
item.bLastLoginTime = Convert.ToDateTime(bLastLoginTime);
if (eLastLoginTime != null && !eLastLoginTime.Equals("") && eLastLoginTime != "undefined")
item.eLastLoginTime = Convert.ToDateTime(eLastLoginTime);
//if (Status != null && !Status.Equals("") && Status != "null")
// item.Status = Convert.ToInt32(Status); Dictionary<string, string> columnInfo = new Dictionary<string, string>();
columnInfo.Add("UserID", "编号");
columnInfo.Add("UserName", "用户名");
columnInfo.Add("TrueName", "真实姓名");
columnInfo.Add("Gender", "性别");
columnInfo.Add("Email", "邮箱");
columnInfo.Add("Mobile", "手机");
columnInfo.Add("Area", "地域");
columnInfo.Add("UserStatus", "状态");
columnInfo.Add("AddTime", "注册时间");
columnInfo.Add("RegisterIP", "注册IP");
columnInfo.Add("LastLoginTime", "最后登录时间");
columnInfo.Add("LastLoginIP", "最后登录IP"); var objList = new UserInfoDAL().GetListExcel(item);
var time = DateTime.Now;
var strtime = time.ToString("yyyyMMddHHmm");
string FileName = "用户列表导出" + strtime + ".xls";
//生成EXCEL的HTML
string excelStr = "";
Type myType = objList[].GetType();
//根据反射从传递进来的属性名信息得到要显示的属性
List<System.Reflection.PropertyInfo> myPro = new List<System.Reflection.PropertyInfo>();
foreach (string cName in columnInfo.Keys)
{
System.Reflection.PropertyInfo p = myType.GetProperty(cName);
if (p != null)
{
myPro.Add(p);
excelStr += columnInfo[cName] + "\t";
}
}
//如果没有找到可用的属性则结束
//if (myPro.Count == 0) { return; }
excelStr += "\n";
foreach (UserInfo obj in objList)
{
foreach (System.Reflection.PropertyInfo p in myPro)
{
var d = p.GetValue(obj, null);
if (p.Name == "Gender" && p.GetValue(obj, null) != null)
{
var status = p.GetValue(obj, null).ToString();
string sta = "";
if (status == "")
sta = "女";
else if (status == "")
sta = "男";
else
sta = "未定义";
excelStr += sta + "\t";
}
else if (p.Name == "UserStatus" && p.GetValue(obj, null) != null)
{
var status = p.GetValue(obj, null).ToString();
string sta = "";
if (status == "")
sta = "正常";
else if (status == "")
sta = "禁止";
else
sta = "未定义";
excelStr += sta + "\t";
}
else
excelStr += p.GetValue(obj, null) + "\t";
}
excelStr += "\n";
}
//输出EXCEL
HttpResponse rs = System.Web.HttpContext.Current.Response;
rs.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
rs.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
rs.ContentType = "application/ms-excel";
rs.Write(excelStr);
rs.End(); }

UserInfoDAL()

 public List<UserInfo> GetListExcel(SearchItem.UserSearchItem item)
{
/*
var order = extitem.Sort + " " + extitem.Dir; var search = item.UserStatus < 0 ? QueryBuilder.Create<UserInfo>()
.Like(c => c.UserName, item.UserName)
.Like(c => c.Mobile, item.Mobile)
.Equals(c => c.UserID, item.UserID)
.Equals(c => c.Gender, item.Gender)
.Between(c => c.AddTime, item.bAddTime.HasValue ? item.bAddTime.Value : DateTime.MinValue.AddYears(1990), item.eAddTime.HasValue ? item.eAddTime.Value.AddDays(1) : DateTime.MaxValue)
.Between(c => c.LastLoginTime, item.bLastLoginTime.HasValue ? item.bLastLoginTime.Value : DateTime.MinValue.AddYears(1990), item.eLastLoginTime.HasValue ? item.eLastLoginTime.Value.AddDays(1) : DateTime.MaxValue)
.LessThan(c => c.UserStatus, 0) :
QueryBuilder.Create<UserInfo>()
.Like(c => c.UserName, item.UserName)
.Like(c => c.Mobile, item.Mobile)
.Equals(c => c.Gender, item.Gender)
.Equals(c => c.UserStatus, item.UserStatus)
.Between(c => c.AddTime, item.bAddTime.HasValue ? item.bAddTime.Value : DateTime.MinValue.AddYears(1990), item.eAddTime.HasValue ? item.eAddTime.Value.AddDays(1) : DateTime.MaxValue)
.Between(c => c.LastLoginTime, item.bLastLoginTime.HasValue ? item.bLastLoginTime.Value : DateTime.MinValue.AddYears(1990), item.eLastLoginTime.HasValue ? item.eLastLoginTime.Value.AddDays(1) : DateTime.MaxValue)
.Equals(c => c.UserID, item.UserID);
*/ var search = item.UserStatus < ? QueryBuilder.Create<UserInfo>()
.Like(c => c.UserName, item.UserName)
.Like(c => c.Mobile, item.Mobile)
.Equals(c => c.UserID, item.UserID)
.Equals(c => c.Gender, item.Gender)
.Between(c => c.AddTime, item.bAddTime.HasValue ? item.bAddTime.Value : DateTime.MinValue.AddYears(), item.eAddTime.HasValue ? item.eAddTime.Value.AddDays() : DateTime.MaxValue)
.Between(c => c.LastLoginTime, item.bLastLoginTime.HasValue ? item.bLastLoginTime.Value : DateTime.MinValue.AddYears(), item.eLastLoginTime.HasValue ? item.eLastLoginTime.Value.AddDays() : DateTime.MaxValue)
.LessThan(c => c.UserStatus, ) :
QueryBuilder.Create<UserInfo>()
.Like(c => c.UserName, item.UserName)
.Like(c => c.Mobile, item.Mobile)
.Equals(c => c.Gender, item.Gender)
.Equals(c => c.UserStatus, item.UserStatus)
.Between(c => c.AddTime, item.bAddTime.HasValue ? item.bAddTime.Value : DateTime.MinValue.AddYears(), item.eAddTime.HasValue ? item.eAddTime.Value.AddDays() : DateTime.MaxValue)
.Between(c => c.LastLoginTime, item.bLastLoginTime.HasValue ? item.bLastLoginTime.Value : DateTime.MinValue.AddYears(), item.eLastLoginTime.HasValue ? item.eLastLoginTime.Value.AddDays() : DateTime.MaxValue)
.Equals(c => c.UserID, item.UserID);
//int OrderStatus = -4;
//if (!int.TryParse(osi.OrderStatus, out OrderStatus))
//{
// search.In(c => c.OrderStatus, OrderStatus);
//}
var list = (from o in Udc.tb_UserInfo
select new UserInfo
{
//OrderID = t.OrderID,
//OrderTime = t.OrderTime,
//UserName = Udc.tb_UserInfo.SingleOrDefault(r => r.UserID.Equals(t.UserID)).UserName,
UserID = o.UserID,
UserName = o.UserName,
Email = o.Email,
Gender = o.Gender,
Mobile = o.Mobile,
Provice = o.Provice,
City = o.City,
Prefecture = o.Prefecture,
Area = o.Provice + o.City,
UserImage = o.UserImage,
LearnIndex = o.LearnIndex,
TeachIndex = o.TeachIndex,
GoodComments = o.GoodComments,
UserStatus = o.UserStatus,
TrueName = o.TrueName,
IdentyNum = o.IdentyNum,
Introduce = o.Introduce,
ActiveInfo = o.ActiveInfo,
BuyGoodComments = o.BuyGoodComments,
Attention = o.Attention,
Extend = o.Extend,//**************
Img = o.Img,
ReferrerID = o.ReferrerID,//************
AddTime = o.AddTime,
StrGrade = o.Grade,
U_School = o.School,
PassWord = o.Password,
Birthday = o.birthday,
RegisterIP = o.RegisterIP,
LastLoginIP = o.LastLoginIP,
LastLoginTime = o.LastLoginTime }).Where(search.Expression).ToList();//.ToPagedList(esi.Start / esi.Limit + 1, esi.Limit);
return list;
}
上一篇:Codeforces Round #260 (Div. 1) A - Boredom DP


下一篇:Keil 3光标问题 以及汉字问题