460 lines
		
	
	
		
			19 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			460 lines
		
	
	
		
			19 KiB
		
	
	
	
		
			C#
		
	
	
	
using BLL;
 | 
						|
using Model;
 | 
						|
using System;
 | 
						|
using System.Collections.Generic;
 | 
						|
using System.Data.SqlClient;
 | 
						|
using System.Data;
 | 
						|
using System.Linq;
 | 
						|
using System.Web;
 | 
						|
using System.Web.UI;
 | 
						|
using System.Reflection;
 | 
						|
using NPOI.SS.UserModel;
 | 
						|
using NPOI.SS.Util;
 | 
						|
using NPOI.XSSF.UserModel;
 | 
						|
using System.IO;
 | 
						|
using Newtonsoft.Json.Linq;
 | 
						|
 | 
						|
namespace FineUIPro.Web.TestRun.Report
 | 
						|
{
 | 
						|
    public partial class FourDecisionSchedule : PageBase
 | 
						|
    {
 | 
						|
        /// <summary>
 | 
						|
        /// 点击树状主键
 | 
						|
        /// </summary>
 | 
						|
        public string TreePreRunId { get { return (string)ViewState["TreePreRunId"]; } set { ViewState["TreePreRunId"] = value; } }
 | 
						|
        /// <summary>
 | 
						|
        /// 子系统主键
 | 
						|
        /// </summary>
 | 
						|
        public List<string> SubSystemIds { get { return (List<string>)ViewState["SubSystemIds"]; } set { ViewState["SubSystemIds"] = value; } }
 | 
						|
        protected void Page_Load(object sender, EventArgs e)
 | 
						|
        {
 | 
						|
            if (!IsPostBack)
 | 
						|
            {
 | 
						|
                if (SubSystemIds == null) SubSystemIds = new List<string>();
 | 
						|
                this.InitTreeMenu();//加载树
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        #region 加载树
 | 
						|
 | 
						|
 | 
						|
        /// <summary>
 | 
						|
        /// 加载树
 | 
						|
        /// </summary>
 | 
						|
        private void InitTreeMenu()
 | 
						|
        {
 | 
						|
            this.tvControlItem.Nodes.Clear();
 | 
						|
            TreeNode rootNode = new TreeNode();
 | 
						|
            rootNode.Text = "系统划分";
 | 
						|
            rootNode.NodeID = "0";
 | 
						|
            rootNode.Expanded = true;
 | 
						|
            rootNode.ToolTip = "";
 | 
						|
            rootNode.EnableClickEvent = true;
 | 
						|
            this.tvControlItem.Nodes.Add(rootNode);
 | 
						|
            var allPreRunLs = Funs.DB.PreRun_SysDevice.Where(p => p.ProjectId == this.CurrUser.LoginProjectId).ToList();
 | 
						|
            var onePreRunLs = allPreRunLs.Where(p => p.PreRunLevel == 1).OrderBy(x => x.Sort);
 | 
						|
            foreach (var item in onePreRunLs)
 | 
						|
            {
 | 
						|
                TreeNode rootUnitNode = new TreeNode();//定义根节点
 | 
						|
                rootUnitNode.NodeID = item.PreRunId;
 | 
						|
                rootUnitNode.Text = item.PreRunName;
 | 
						|
                rootUnitNode.ToolTip = item.PreRunName;
 | 
						|
                rootUnitNode.CommandName = "";
 | 
						|
                rootUnitNode.EnableClickEvent = true;
 | 
						|
                rootUnitNode.EnableExpandEvent = true;
 | 
						|
                rootNode.Nodes.Add(rootUnitNode);
 | 
						|
                rootUnitNode.Expanded = true;
 | 
						|
                var otherPreRunls = allPreRunLs.Where(p => p.PreRunLevel != 1).ToList();
 | 
						|
                this.BindNodes(rootUnitNode, otherPreRunls, item.PreRunId);
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        /// <summary>
 | 
						|
        ///  绑定树节点
 | 
						|
        /// </summary>
 | 
						|
        private void BindNodes(TreeNode node, List<PreRun_SysDevice> list, string parentId)
 | 
						|
        {
 | 
						|
 | 
						|
            var itemList = list.Where(p => p.ParentId == parentId).OrderBy(x => x.Sort).ToList();
 | 
						|
            if (itemList.Count > 0)
 | 
						|
            {
 | 
						|
                foreach (var item in itemList)
 | 
						|
                {
 | 
						|
                    TreeNode newNode = new TreeNode();
 | 
						|
                    newNode.Text = item.PreRunName;
 | 
						|
                    newNode.NodeID = item.PreRunId;
 | 
						|
                    newNode.ToolTip = item.PreRunName;
 | 
						|
                    newNode.CommandName = "";
 | 
						|
                    newNode.EnableClickEvent = true;
 | 
						|
                    node.Nodes.Add(newNode);
 | 
						|
                    BindNodes(newNode, list, item.PreRunId);
 | 
						|
                }
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        /// <summary>
 | 
						|
        /// 数加载
 | 
						|
        /// </summary>
 | 
						|
        protected void tvControlItem_NodeExpand(object sender, TreeNodeEventArgs e)
 | 
						|
        {
 | 
						|
            if (e.Node.Nodes != null)
 | 
						|
            {
 | 
						|
                e.Node.Nodes.Clear();
 | 
						|
            }
 | 
						|
            var allPreRunLs = Funs.DB.PreRun_SysDevice.Where(p => p.ProjectId == this.CurrUser.LoginProjectId).ToList();
 | 
						|
            this.BindNodes(e.Node, allPreRunLs, e.NodeID);
 | 
						|
        }
 | 
						|
 | 
						|
        /// <summary>
 | 
						|
        /// 点击TreeView
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="sender"></param>
 | 
						|
        /// <param name="e"></param>
 | 
						|
        protected void tvControlItem_NodeCommand(object sender, TreeCommandEventArgs e)
 | 
						|
        {
 | 
						|
            var devices = new List<PreRun_SysDevice>();
 | 
						|
            this.TreePreRunId = tvControlItem.SelectedNodeID != "0" ? tvControlItem.SelectedNodeID : string.Empty;
 | 
						|
            if (!string.IsNullOrWhiteSpace(this.TreePreRunId))
 | 
						|
            {
 | 
						|
                var model = Funs.DB.PreRun_SysDevice.FirstOrDefault(x => x.PreRunId == this.TreePreRunId);
 | 
						|
                if (model.PreRunLevel == 4)
 | 
						|
                {
 | 
						|
                    this.SubSystemIds = new List<string>() { model.PreRunId };
 | 
						|
                }
 | 
						|
                else if (model.PreRunLevel == 3)
 | 
						|
                {
 | 
						|
                    devices = Funs.DB.PreRun_SysDevice.Where(x => x.ProjectId == this.CurrUser.LoginProjectId && x.PreRunLevel == 4 && x.SystemId == model.PreRunId).ToList();
 | 
						|
                    if (devices.Count > 0)
 | 
						|
                    {
 | 
						|
                        this.SubSystemIds = devices.ConvertAll(x => x.PreRunId);
 | 
						|
                    }
 | 
						|
                }
 | 
						|
                else if (model.PreRunLevel == 2)
 | 
						|
                {
 | 
						|
                    devices = Funs.DB.PreRun_SysDevice.Where(x => x.ProjectId == this.CurrUser.LoginProjectId && x.PreRunLevel == 4 && x.ProcessesId == model.PreRunId).ToList();
 | 
						|
                    if (devices.Count > 0)
 | 
						|
                    {
 | 
						|
                        this.SubSystemIds = devices.ConvertAll(x => x.PreRunId);
 | 
						|
                    }
 | 
						|
                }
 | 
						|
                else if (model.PreRunLevel == 1)
 | 
						|
                {
 | 
						|
                    devices = Funs.DB.PreRun_SysDevice.Where(x => x.ProjectId == this.CurrUser.LoginProjectId && x.PreRunLevel == 4 && x.InstallationId == model.PreRunId).ToList();
 | 
						|
                    if (devices.Count > 0)
 | 
						|
                    {
 | 
						|
                        this.SubSystemIds = devices.ConvertAll(x => x.PreRunId);
 | 
						|
                    }
 | 
						|
                }
 | 
						|
            }
 | 
						|
            else
 | 
						|
            {
 | 
						|
                devices = Funs.DB.PreRun_SysDevice.Where(x => x.ProjectId == this.CurrUser.LoginProjectId && x.PreRunLevel == 4).ToList();
 | 
						|
                if (devices.Count > 0)
 | 
						|
                {
 | 
						|
                    this.SubSystemIds = devices.ConvertAll(x => x.PreRunId);
 | 
						|
                }
 | 
						|
            }
 | 
						|
            FourDecisionBrid();
 | 
						|
        }
 | 
						|
 | 
						|
        #endregion
 | 
						|
 | 
						|
        #region 数据绑定/导出
 | 
						|
 | 
						|
        /// <summary>
 | 
						|
        /// 三查四定绑定
 | 
						|
        /// </summary>
 | 
						|
        public void FourDecisionBrid()
 | 
						|
        {
 | 
						|
            var result = new List<FourDecisionScheduleBridDto>();
 | 
						|
            if (this.SubSystemIds.Count > 0)
 | 
						|
            {
 | 
						|
                result = GetFourDecisionSchedules();
 | 
						|
            }
 | 
						|
            GridFourDecision.DataSource = result;
 | 
						|
            GridFourDecision.DataBind();
 | 
						|
            JObject summary = new JObject();
 | 
						|
            summary.Add("AllNum", result.Sum(x => x.AllNum));
 | 
						|
            summary.Add("NoCloseNum", result.Sum(x => x.NoCloseNum));
 | 
						|
            summary.Add("CloseNum", result.Sum(x => x.CloseNum));
 | 
						|
            GridFourDecision.SummaryData = summary;
 | 
						|
        }
 | 
						|
 | 
						|
        /// <summary>
 | 
						|
        /// 导出
 | 
						|
        /// </summary>
 | 
						|
        protected void btnScsdExport_Click(object sender, EventArgs e)
 | 
						|
        {
 | 
						|
            string rootPath = Server.MapPath("~/") + Const.ExcelUrl;
 | 
						|
            //导出文件
 | 
						|
            string filePath = rootPath + DateTime.Now.ToString("yyyyMMddhhmmss") + "\\";
 | 
						|
            if (!Directory.Exists(filePath))
 | 
						|
            {
 | 
						|
                Directory.CreateDirectory(filePath);
 | 
						|
            }
 | 
						|
            string ReportFileName = filePath + "三查四定进度.xlsx";
 | 
						|
 | 
						|
            var result = GetFourDecisionSchedules();
 | 
						|
 | 
						|
            if (result.Count > 0)
 | 
						|
            {
 | 
						|
                XSSFWorkbook hssfworkbook = new XSSFWorkbook();
 | 
						|
                XSSFSheet ws = (XSSFSheet)hssfworkbook.CreateSheet("三查四定进度");
 | 
						|
 | 
						|
                #region 列宽
 | 
						|
 | 
						|
                ws.SetColumnWidth(0, (30 * 256));
 | 
						|
                ws.SetColumnWidth(1, (20 * 256));
 | 
						|
                ws.SetColumnWidth(2, (20 * 256));
 | 
						|
                ws.SetColumnWidth(3, (20 * 256));
 | 
						|
                ws.SetColumnWidth(4, (20 * 256));
 | 
						|
                ws.SetColumnWidth(5, (20 * 256));
 | 
						|
                ws.SetColumnWidth(6, (20 * 256));
 | 
						|
                ws.SetColumnWidth(7, (20 * 256));
 | 
						|
 | 
						|
                #endregion
 | 
						|
 | 
						|
                #region 样式
 | 
						|
                //公共样式
 | 
						|
                ICellStyle style = SetExcelStyle(hssfworkbook, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, VerticalAlignment.Center, HorizontalAlignment.Center, 10.5, true);
 | 
						|
                //公共样式加粗
 | 
						|
                ICellStyle styleBold = SetExcelStyle(hssfworkbook, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, VerticalAlignment.Center, HorizontalAlignment.Center, 10.5, true, true);
 | 
						|
 | 
						|
                #endregion
 | 
						|
 | 
						|
                #region 头部
 | 
						|
 | 
						|
                ws = ExcelCreateRowTitle(ws, hssfworkbook, style, 0, 0, 0, 7);
 | 
						|
                //行1
 | 
						|
                ws.GetRow(0).GetCell(0).CellStyle = styleBold;
 | 
						|
                ws.GetRow(0).GetCell(0).SetCellValue("级别");
 | 
						|
                ws.GetRow(0).GetCell(1).CellStyle = styleBold;
 | 
						|
                ws.GetRow(0).GetCell(1).SetCellValue("装置名称");
 | 
						|
                ws.GetRow(0).GetCell(2).CellStyle = styleBold;
 | 
						|
                ws.GetRow(0).GetCell(2).SetCellValue("工序名称");
 | 
						|
                ws.GetRow(0).GetCell(3).CellStyle = styleBold;
 | 
						|
                ws.GetRow(0).GetCell(3).SetCellValue("系统名称");
 | 
						|
                ws.GetRow(0).GetCell(4).CellStyle = styleBold;
 | 
						|
                ws.GetRow(0).GetCell(4).SetCellValue("子系统名称");
 | 
						|
                ws.GetRow(0).GetCell(5).CellStyle = styleBold;
 | 
						|
                ws.GetRow(0).GetCell(5).SetCellValue("尾项数量");
 | 
						|
                ws.GetRow(0).GetCell(6).CellStyle = styleBold;
 | 
						|
                ws.GetRow(0).GetCell(6).SetCellValue("未关闭尾项数量");
 | 
						|
                ws.GetRow(0).GetCell(7).CellStyle = styleBold;
 | 
						|
                ws.GetRow(0).GetCell(7).SetCellValue("已关闭尾项数量");
 | 
						|
 | 
						|
                #endregion
 | 
						|
 | 
						|
                #region 表格
 | 
						|
 | 
						|
                var start = 1;
 | 
						|
                var end = result.Count;
 | 
						|
                ws = ExcelCreateRowTitle(ws, hssfworkbook, style, start, end, 0, 7);
 | 
						|
 | 
						|
                //数据
 | 
						|
                var dataIndex = 1;
 | 
						|
                foreach (var item in result)
 | 
						|
                {
 | 
						|
                    ws.GetRow(dataIndex).GetCell(0).SetCellValue(item.Level);
 | 
						|
                    ws.GetRow(dataIndex).GetCell(1).SetCellValue(item.InstallationName);
 | 
						|
                    ws.GetRow(dataIndex).GetCell(2).SetCellValue(item.ProcessesName);
 | 
						|
                    ws.GetRow(dataIndex).GetCell(3).SetCellValue(item.SystemName);
 | 
						|
                    ws.GetRow(dataIndex).GetCell(4).SetCellValue(item.SubsystemName);
 | 
						|
                    ws.GetRow(dataIndex).GetCell(5).SetCellValue(item.AllNum);
 | 
						|
                    ws.GetRow(dataIndex).GetCell(6).SetCellValue(item.NoCloseNum);
 | 
						|
                    ws.GetRow(dataIndex).GetCell(7).SetCellValue(item.CloseNum);
 | 
						|
                    dataIndex++;
 | 
						|
                }
 | 
						|
 | 
						|
                #endregion
 | 
						|
 | 
						|
                #region 尾部
 | 
						|
 | 
						|
                ws = ExcelCreateRowTitle(ws, hssfworkbook, style, end + 1, end + 1, 0, 7);
 | 
						|
                var region = new CellRangeAddress(end + 1, end + 1, 0, 4);
 | 
						|
                ws.AddMergedRegion(region);
 | 
						|
                ws.GetRow(end + 1).GetCell(0).SetCellValue("合计");
 | 
						|
                ws.GetRow(end + 1).GetCell(5).SetCellValue(result.Count > 0 ? result.Sum(x => x.AllNum).ToString() : "0");
 | 
						|
                ws.GetRow(end + 1).GetCell(6).SetCellValue(result.Count > 0 ? result.Sum(x => x.NoCloseNum).ToString() : "0");
 | 
						|
                ws.GetRow(end + 1).GetCell(7).SetCellValue(result.Count > 0 ? result.Sum(x => x.CloseNum).ToString() : "0");
 | 
						|
 | 
						|
                #endregion
 | 
						|
 | 
						|
                ws.PrintSetup.Landscape = false;
 | 
						|
                ws.PrintSetup.PaperSize = 9;
 | 
						|
                ws.ForceFormulaRecalculation = true;
 | 
						|
                using (FileStream filess = File.OpenWrite(ReportFileName))
 | 
						|
                {
 | 
						|
                    hssfworkbook.Write(filess);
 | 
						|
                }
 | 
						|
                FileInfo filet = new FileInfo(ReportFileName);
 | 
						|
                Response.Clear();
 | 
						|
                Response.Charset = "GB2312";
 | 
						|
                Response.ContentEncoding = System.Text.Encoding.UTF8;
 | 
						|
                // 添加头信息,为"文件下载/另存为"对话框指定默认文件名
 | 
						|
                Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode("三查四定进度.xlsx"));
 | 
						|
                // 添加头信息,指定文件大小,让浏览器能够显示下载进度
 | 
						|
                Response.AddHeader("Content-Length", filet.Length.ToString());
 | 
						|
                // 指定返回的是一个不能被客户端读取的流,必须被下载
 | 
						|
                Response.ContentType = "application/ms-excel";
 | 
						|
                // 把文件流发送到客户端
 | 
						|
                Response.WriteFile(filet.FullName);
 | 
						|
                // 停止页面的执行
 | 
						|
                Response.End();
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        #endregion
 | 
						|
 | 
						|
        #region 私有方法
 | 
						|
 | 
						|
        /// <summary>
 | 
						|
        /// 创建头部
 | 
						|
        /// </summary>
 | 
						|
        /// <returns></returns>
 | 
						|
        private XSSFSheet ExcelCreateRowTitle(XSSFSheet ws, XSSFWorkbook hssfworkbook, ICellStyle style, int sRows, int eRows, int cStart, int cEnd, float height = 21)
 | 
						|
        {
 | 
						|
            for (int i = sRows; i <= eRows; i++)
 | 
						|
            {
 | 
						|
                ws.CreateRow(i);
 | 
						|
                ws.GetRow(i).HeightInPoints = height;
 | 
						|
                for (int j = cStart; j <= cEnd; j++)
 | 
						|
                {
 | 
						|
                    ws.GetRow(i).CreateCell(j);
 | 
						|
                    ws.GetRow(i).CreateCell(j).CellStyle = style;
 | 
						|
                }
 | 
						|
            }
 | 
						|
            return ws;
 | 
						|
        }
 | 
						|
 | 
						|
        /// <summary>
 | 
						|
        /// 创建样式
 | 
						|
        /// </summary>
 | 
						|
        /// <returns></returns>
 | 
						|
        public static ICellStyle SetExcelStyle(XSSFWorkbook wb, BorderStyle Bottom, BorderStyle Left, BorderStyle Right, BorderStyle Top, VerticalAlignment VerAig, HorizontalAlignment HorAig, double FontSize, bool WrapText = true, bool Bold = false, string FontName = "宋体")
 | 
						|
        {
 | 
						|
            ICellStyle style = wb.CreateCellStyle();
 | 
						|
            style.BorderBottom = Bottom;
 | 
						|
            style.BorderLeft = Left;
 | 
						|
            style.BorderRight = Right;
 | 
						|
            style.BorderTop = Top;
 | 
						|
            style.VerticalAlignment = VerAig;
 | 
						|
            style.Alignment = HorAig;
 | 
						|
            IFont font = wb.CreateFont();
 | 
						|
            font.FontHeightInPoints = FontSize;
 | 
						|
            font.IsBold = Bold;
 | 
						|
            font.FontName = FontName;
 | 
						|
            style.SetFont(font);
 | 
						|
            style.WrapText = WrapText;
 | 
						|
            return style;
 | 
						|
        }
 | 
						|
 | 
						|
        /// <summary>
 | 
						|
        /// 获取三查四定统计
 | 
						|
        /// </summary>
 | 
						|
        /// <returns></returns>
 | 
						|
        private List<FourDecisionScheduleBridDto> GetFourDecisionSchedules()
 | 
						|
        {
 | 
						|
            var list = new List<FourDecisionScheduleBridDto>();
 | 
						|
            string allStr = $"select Level,COUNT(1) as AllNum,SUM(case ISNULL(a.DecisionIsClose,0) when 0 then 1 else 0 end) as NoCloseNum,SUM(case ISNULL(a.DecisionIsClose,0) when 1 then 1 else 0 end) as CloseNum from PreRun_SubThreeChecksFourDecision as a where a.SubSystemId in ('{string.Join("','", this.SubSystemIds)}') and a.ProjectId='{this.CurrUser.LoginProjectId}' group by a.Level  order by a.Level asc";
 | 
						|
            var dt = SQLHelper.GetDataTableRunText(allStr);
 | 
						|
            if (dt.Rows.Count > 0)
 | 
						|
            {
 | 
						|
                string deviceStr = $"select b.PreRunName as InstallationName,c.PreRunName as ProcessesName,d.PreRunName as SystemName,e.PreRunName as SubsystemName from PreRun_SysDevice as a left join PreRun_SysDevice as b on b.PreRunId=ISNULL(a.InstallationId,a.PreRunId) left join PreRun_SysDevice as c on c.PreRunId=a.ProcessesId left join PreRun_SysDevice as d on d.PreRunId=a.SystemId left join PreRun_SysDevice as e on e.PreRunId=a.SubsystemId where a.PreRunId='{this.TreePreRunId}'";
 | 
						|
                var driveDt = SQLHelper.GetDataTableRunText(deviceStr);
 | 
						|
 | 
						|
                list = DataTableToList<FourDecisionScheduleBridDto>(dt);
 | 
						|
 | 
						|
                foreach (var item in list)
 | 
						|
                {
 | 
						|
                    if (driveDt.Rows.Count > 0)
 | 
						|
                    {
 | 
						|
                        item.InstallationName = driveDt.Rows[0]["InstallationName"].ToString();
 | 
						|
                        item.ProcessesName = driveDt.Rows[0]["ProcessesName"].ToString();
 | 
						|
                        item.SystemName = driveDt.Rows[0]["SystemName"].ToString();
 | 
						|
                        item.SubsystemName = driveDt.Rows[0]["SubsystemName"].ToString();
 | 
						|
                    }
 | 
						|
                }
 | 
						|
            }
 | 
						|
 | 
						|
            return list;
 | 
						|
        }
 | 
						|
 | 
						|
        /// <summary>
 | 
						|
        /// 三查四定统计实体
 | 
						|
        /// </summary>
 | 
						|
        public class FourDecisionScheduleBridDto
 | 
						|
        {
 | 
						|
            /// <summary>
 | 
						|
            /// 级别
 | 
						|
            /// </summary>
 | 
						|
            public string Level { get; set; }
 | 
						|
            /// <summary>
 | 
						|
            /// 装置名称
 | 
						|
            /// </summary>
 | 
						|
            public string InstallationName { get; set; }
 | 
						|
            /// <summary>
 | 
						|
            /// 工序名称
 | 
						|
            /// </summary>
 | 
						|
            public string ProcessesName { get; set; }
 | 
						|
            /// <summary>
 | 
						|
            /// 系统名称
 | 
						|
            /// </summary>
 | 
						|
            public string SystemName { get; set; }
 | 
						|
            /// <summary>
 | 
						|
            /// 子系统名称
 | 
						|
            /// </summary>
 | 
						|
            public string SubsystemName { get; set; }
 | 
						|
            /// <summary>
 | 
						|
            /// 尾项数量
 | 
						|
            /// </summary>
 | 
						|
            public int AllNum { get; set; }
 | 
						|
            /// <summary>
 | 
						|
            /// 未关闭尾项数量
 | 
						|
            /// </summary>
 | 
						|
            public int NoCloseNum { get; set; }
 | 
						|
            /// <summary>
 | 
						|
            /// 已关闭尾项数量
 | 
						|
            /// </summary>
 | 
						|
            public int CloseNum { get; set; }
 | 
						|
        }
 | 
						|
 | 
						|
        #endregion
 | 
						|
 | 
						|
        #region 转换
 | 
						|
 | 
						|
        /// <summary>
 | 
						|
        /// DataTable转换List
 | 
						|
        /// </summary>
 | 
						|
        /// <returns></returns>
 | 
						|
        public List<T> DataTableToList<T>(DataTable table)
 | 
						|
        {
 | 
						|
            List<T> list = new List<T>();
 | 
						|
            T t = default(T);
 | 
						|
            PropertyInfo[] propertypes = null;
 | 
						|
            string tempName = string.Empty;
 | 
						|
            foreach (DataRow row in table.Rows)
 | 
						|
            {
 | 
						|
                t = Activator.CreateInstance<T>();
 | 
						|
                propertypes = t.GetType().GetProperties();
 | 
						|
                foreach (PropertyInfo pro in propertypes)
 | 
						|
                {
 | 
						|
                    tempName = pro.Name;
 | 
						|
                    if (table.Columns.Contains(tempName))
 | 
						|
                    {
 | 
						|
                        object value = row[tempName];
 | 
						|
                        if (!value.ToString().Equals(""))
 | 
						|
                        {
 | 
						|
                            pro.SetValue(t, value, null);
 | 
						|
                        }
 | 
						|
                    }
 | 
						|
                }
 | 
						|
                list.Add(t);
 | 
						|
            }
 | 
						|
            return list.Count == 0 ? new List<T>() : list;
 | 
						|
        }
 | 
						|
 | 
						|
        #endregion
 | 
						|
 | 
						|
    }
 | 
						|
} |