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;
}
Excel 数据导出的更多相关文章
- 【基于WinForm+Access局域网共享数据库的项目总结】之篇二:WinForm开发扇形图统计和Excel数据导出
篇一:WinForm开发总体概述与技术实现 篇二:WinForm开发扇形图统计和Excel数据导出 篇三:Access远程连接数据库和窗体打包部署 [小记]:最近基于WinForm+Access数据库 ...
- excel数据导出新妙招
之前在做项目的时候需要将数据库中的数据导出为excel表格一遍打印查阅,在网上找了很多插件也没有找到自己理想的好用的插件(也就是说没有找到令我满意的插件),最近在学习a标签的相关知识时理解到,a标签不 ...
- java实现Excel数据导出
java实现Excel数据导出: 目前,比较常用的实现Java导入.导出Excel的技术有两种Jakarta POI和Java Excel Jakarta POI 是一套用于访问微软格式文档的Java ...
- php做EXCEL数据导出导入开发的一些小问题
前两天刚刚做开发CRM系统项目,在做要做EXCEL导出导入功能,因为以前做.NET开发用的是NPOI,但可是没找到PHP版本的,所以就网搜找了个国外的开源PHPEXCEL , 一开始只是做了简单的导入 ...
- .Net之Nopi Excel数据导出和批量导入功能
一.介绍NPOI和编写demo的原因 1.Npoi是什么: 它是一个专门用于读写Microsoft Office二进制和OOXML文件格式的.NET库,我们使用它能够轻松的实现对应数据的导入,导出功能 ...
- python Excel数据导出
import pymysql,os,time,xlwtpymysql.install_as_MySQLdb() try: #创建一个excel工作簿,编码utf-8,表格中支持中文 wb=xlwt.W ...
- epplus excel数据导出(数据量有点大的情况) Web和Client
Asp.net MVC后台代码 public ActionResult Export() { OfficeOpenXml.ExcelPackage ep = new OfficeOpenXml.Exc ...
- PHPExcel 导入Excel数据 (导出下一篇我们继续讲解)
一:使用composer下载 phpoffice/phpexcel 或者直接下载安装包 composer require phpoffice/phpexcel 二 1:导入数据 原理:读取文件,获取文 ...
- 通过java反射实现的excel数据导出
Excel.java @SuppressWarnings("deprecation") public static <T> void ExportExcel(Strin ...
随机推荐
- 求一个int型整数的两种递减数之和(java)--2015华为机试题
题目描述: 给出一个整数(负数使用其绝对值),输出这个整数中的两种递减数(1.最大递减数:2.递减数中各位数之和最大的数)之和. 递减数:一个数字的递减数是指相邻的数位从大到小排列的数字,不包含相邻的 ...
- iOS开发之自定义UITableView的cell
系统默认的UITableViewCell的每行都有横线(分隔符),就算没有数据也是如此,有时候我们想只在有数据的地方有下划线,可以去除下划线,然后在awarkFromNid方法中使用addsubvie ...
- 判断是否支持WebP
PC端,触屏版: 前端JS方案——利用img标签加载一张base64的WebP图片,在img标签的onload事件中判断该图片是否具有宽高的属性,若有表示支持webP,若没有表示不支持webP.后台判 ...
- Unity 3D Framework Designing(4)——设计可复用的SubView和SubViewModel(Part 2)
在我们设计和开发应用程序时,经常要用到控件.比如开发一个客户端WinForm应用程序时,微软就为我们提供了若干控件,这些控件为我们提供了可被定制的属性和事件.属性可以更改它的外观,比如背景色,标题等, ...
- Jmeter-添加检查点
JMeter里面的检查点通过添加断言来完成. 检查用户名和密码参数化的文件user.dat有没有正确调用,添加断言,可以在结果树中查看结果. 1.添加响应断言,右键点击HTTP请求"ts1后 ...
- crontab的定时任务不能自动执行,但是手动执行脚本一直能成功
crontab 问题小记: 环境变量问题, 养成良好的习惯, 在脚本开头export PATH 原因是 crontab 执行定时任务时,用的不是系统环境变量,而是自己的环境变量,可以把 echo $P ...
- SVG动画-基础篇
参考资料: http://www.w3school.com.cn/svg/index.asp https://msdn.microsoft.com/zh-cn/library/gg193979 简介 ...
- 【转】使用VS开发 Node.js指南
参考:https://www.visualstudio.com/features/node-js-vs 这篇文章主要介绍了使用VS开发 Node.js的方法,主要是使用NTVS(Node.js Too ...
- LoonAndroid自动检测输入框 --- Author: rose && lvyerose@163.com
LoonAndroid框架,同时给我们提供了一套自动检测输入规则的工具,用起来很是方便,下面介绍一下这个东东的使用方法(注意,该说明是基于项目已经集成了LoonAndroid框架而言,如果您未集成该框 ...
- Xmpp实现简单聊天系列 --- ①openfire部署
1. 下载最新的openfire安装文件 官方下载站点:http://www.igniterealtime.org/downloads/index.jsp#openfire 2. 下载完成后,执行你的 ...