From ccf6ac836de36eb88447844ba5d8655ce5ef1147 Mon Sep 17 00:00:00 2001 From: wangqing <945983254@qq.com> Date: Thu, 13 Jun 2024 23:53:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=88=E5=B9=B6=E8=A1=8C=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HJGL/FineUIPro.Web/FineUIPro.Web.csproj | 1 + .../TestPackageManageAudit.aspx.cs | 308 +++++++++++------- HJGL/FineUIPro.Web/common/DataTableEx.cs | 288 ++++++++++++++++ 3 files changed, 481 insertions(+), 116 deletions(-) create mode 100644 HJGL/FineUIPro.Web/common/DataTableEx.cs diff --git a/HJGL/FineUIPro.Web/FineUIPro.Web.csproj b/HJGL/FineUIPro.Web/FineUIPro.Web.csproj index deb1e63..9f303df 100644 --- a/HJGL/FineUIPro.Web/FineUIPro.Web.csproj +++ b/HJGL/FineUIPro.Web/FineUIPro.Web.csproj @@ -4185,6 +4185,7 @@ UnitView.aspx + EditTeamGroup.aspx ASPXCodeBehind diff --git a/HJGL/FineUIPro.Web/WeldingProcess/TestPackageManage/TestPackageManageAudit.aspx.cs b/HJGL/FineUIPro.Web/WeldingProcess/TestPackageManage/TestPackageManageAudit.aspx.cs index 8b0ff1a..e90050c 100644 --- a/HJGL/FineUIPro.Web/WeldingProcess/TestPackageManage/TestPackageManageAudit.aspx.cs +++ b/HJGL/FineUIPro.Web/WeldingProcess/TestPackageManage/TestPackageManageAudit.aspx.cs @@ -7,6 +7,7 @@ using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; +using System.Reflection; using System.Runtime.InteropServices; using System.Web; using System.Web.UI; @@ -3995,85 +3996,85 @@ namespace FineUIPro.Web.WeldingProcess.TestPackageManage { var info = GetTestPackageInfo(this.tvControlItem.SelectedNodeID); - ////模拟数据 - //string sql = "SELECT '' AS totalNum,'' TestPackageNo,'' PipelineId,'模拟数据' PipelineCode,'' Specification,'' MaterialCode,'' WelderCode,'' FNum,'' NdeNum,'' FNdeNum,'' NdeCode from Sys_User union all SELECT '' AS totalNum,'' TestPackageNo,'' PipelineId,'模拟数据' PipelineCode,'' Specification,'' MaterialCode,'' WelderCode,'' FNum,'' NdeNum,'' FNdeNum,'' NdeCode from Sys_User union all SELECT '' AS totalNum,'' TestPackageNo,'' PipelineId,'模拟数据' PipelineCode,'' Specification,'' MaterialCode,'' WelderCode,'' FNum,'' NdeNum,'' FNdeNum,'' NdeCode from Sys_User"; + //模拟数据 + string sql = "SELECT top 5 'AA' PipelineCode,'BB' MaterialCode,'CC' Specification,'DD' jointCount,'EE' FjointCount,'W1' WelderCode,'3' WelderNum,'1' NdeNum,'11' FNdeNum,'2' NdeReportNo FROM Sys_User union all SELECT top 6 'FF' PipelineCode,'GG' MaterialCode,'HH' Specification,'II' jointCount,'JJ' FjointCount,'W2' WelderCode,'3' WelderNum,'1' NdeNum,'2' FNdeNum,'D2' NdeReportNo FROM Sys_User union all SELECT top 7 'KK' PipelineCode,'LL' MaterialCode,'MM' Specification,'NN' jointCount,'OO' FjointCount,'W3' WelderCode,'3' WelderNum,'1' NdeNum,'2' FNdeNum,'D3' NdeReportNo FROM Sys_User union all SELECT top 1 '1' PipelineCode,'2' MaterialCode,'3' Specification,'4' jointCount,'5' FjointCount,'6' WelderCode,'3' WelderNum,'1' NdeNum,'2' FNdeNum,'D3' NdeReportNo FROM Sys_User"; - string sql = @"use HJGLDB_ZJBSF + // string sql = @"use HJGLDB_ZJBSF -select * from ( -select d.PipelineCode, -(select MaterialCode from Base_Material as m where d.MainMaterialId=m.MaterialId) as MaterialCode , -d.Specification, -(select count(1) from Pipeline_WeldJoint as joint where joint.PipelineId=a.PipelineId) as jointCount, --当前管线的焊口总数 - ( case when a.isAll=1 then - (select count(1) from Pipeline_WeldJoint as joint where joint.PipelineId=a.PipelineId and joint.JointAttribute='固定F') - else - (select count(1) from Pipeline_WeldJoint as joint where joint.PipelineId=a.PipelineId and joint.JointAttribute='固定' and CHARINDEX(','+joint.WeldJointCode+',',','+a.WeldJonintCode+'')>0) + //select * from ( + //select d.PipelineCode, + //(select MaterialCode from Base_Material as m where d.MainMaterialId=m.MaterialId) as MaterialCode , + //d.Specification, + //(select count(1) from Pipeline_WeldJoint as joint where joint.PipelineId=a.PipelineId) as jointCount, --当前管线的焊口总数 + // ( case when a.isAll=1 then + // (select count(1) from Pipeline_WeldJoint as joint where joint.PipelineId=a.PipelineId and joint.JointAttribute='固定F') + // else + // (select count(1) from Pipeline_WeldJoint as joint where joint.PipelineId=a.PipelineId and joint.JointAttribute='固定' and CHARINDEX(','+joint.WeldJointCode+',',','+a.WeldJonintCode+'')>0) - end ) as FjointCount, --当前管线的焊口的固定口总数 -c.WelderCode, --焊工号 - ( case when a.isAll=1 then - (select count(1) from View_Pipeline_WeldJoint as vjoint where vjoint.PipelineId=a.PipelineId and vjoint.WelderCode=c.WelderCode) - else - (select count(1) from View_Pipeline_WeldJoint as vjoint where vjoint.PipelineId=a.PipelineId - and CHARINDEX(','+vjoint.WeldJointCode+',',','+a.WeldJonintCode+'')>0 and vjoint.WelderCode=c.WelderCode) - end ) as WelderNum, - (case when a.isAll=1 then - (select count(1) from Batch_NDEItem as nde inner join Batch_BatchTrustItem as trust - ON trust.TrustBatchItemId = nde.TrustBatchItemId - inner join View_Pipeline_WeldJoint as joint - ON joint.WeldJointId=trust.WeldJointId - where joint.PipelineId=a.PipelineId and joint.WelderCode=c.WelderCode) - else - ( - select count(1) from Batch_NDEItem as nde inner join Batch_BatchTrustItem as trust - ON trust.TrustBatchItemId = nde.TrustBatchItemId - inner join View_Pipeline_WeldJoint as joint - ON joint.WeldJointId=trust.WeldJointId - where joint.PipelineId=a.PipelineId and CHARINDEX(','+joint.WeldJointCode+',',','+a.WeldJonintCode+'')>0 and joint.WelderCode=c.WelderCode - ) - end - ) as NdeNum, --检测口数量 - (case when a.isAll=1 then - (select count(1) from Batch_NDEItem as nde inner join Batch_BatchTrustItem as trust - ON trust.TrustBatchItemId = nde.TrustBatchItemId - inner join View_Pipeline_WeldJoint as joint - ON joint.WeldJointId=trust.WeldJointId - where joint.PipelineId=a.PipelineId and joint.JointAttribute='固定F' and joint.WelderCode=c.WelderCode) - else - ( - select count(1) from Batch_NDEItem as nde inner join Batch_BatchTrustItem as trust - ON trust.TrustBatchItemId = nde.TrustBatchItemId - inner join View_Pipeline_WeldJoint as joint - ON joint.WeldJointId=trust.WeldJointId - where joint.PipelineId=a.PipelineId and CHARINDEX(','+joint.WeldJointCode+',',','+a.WeldJonintCode+'')>0 and joint.JointAttribute='固定F' and joint.WelderCode=c.WelderCode - ) - end - ) as FNdeNum , --检测固定口数量 - ( - case when a.isAll=1 then - (stuff((select ','+nde.NDEReportNo from Batch_NDEItem as nde inner join Batch_BatchTrustItem as trust - ON trust.TrustBatchItemId = nde.TrustBatchItemId - inner join View_Pipeline_WeldJoint as joint - ON joint.WeldJointId=trust.WeldJointId - where joint.PipelineId=a.PipelineId and joint.WelderCode=c.WelderCode FOR xml path ('') ),1,1,'')) - else - (stuff((select ','+nde.NDEReportNo from Batch_NDEItem as nde inner join Batch_BatchTrustItem as trust - ON trust.TrustBatchItemId = nde.TrustBatchItemId - inner join View_Pipeline_WeldJoint as joint - ON joint.WeldJointId=trust.WeldJointId - where joint.PipelineId=a.PipelineId and CHARINDEX(','+joint.WeldJointCode+',',','+a.WeldJonintCode+'')>0 and joint.WelderCode=c.WelderCode FOR xml path ( '' ) ),1,1,'') ) - end) as NdeReportNo -from -PTP_PipelineList as a inner join -PTP_TestPackage as b on a.PTP_ID=b.PTP_ID -inner join View_Pipeline_WeldJoint as c -on c.PipelineId=a.PipelineId -inner join Pipeline_Pipeline as d -on c.PipelineId=d.PipelineId -where b.PTP_ID=@PTPID and b.ProjectId=@projectId -) as t -group by t.PipelineCode,t.MaterialCode,t.Specification,t.jointCount,t.FjointCount,t.WelderCode,t.WelderNum,t.NdeNum,t.FNdeNum,t.NdeReportNo"; + // end ) as FjointCount, --当前管线的焊口的固定口总数 + //c.WelderCode, --焊工号 + // ( case when a.isAll=1 then + // (select count(1) from View_Pipeline_WeldJoint as vjoint where vjoint.PipelineId=a.PipelineId and vjoint.WelderCode=c.WelderCode) + // else + // (select count(1) from View_Pipeline_WeldJoint as vjoint where vjoint.PipelineId=a.PipelineId + // and CHARINDEX(','+vjoint.WeldJointCode+',',','+a.WeldJonintCode+'')>0 and vjoint.WelderCode=c.WelderCode) + // end ) as WelderNum, + // (case when a.isAll=1 then + // (select count(1) from Batch_NDEItem as nde inner join Batch_BatchTrustItem as trust + // ON trust.TrustBatchItemId = nde.TrustBatchItemId + // inner join View_Pipeline_WeldJoint as joint + // ON joint.WeldJointId=trust.WeldJointId + // where joint.PipelineId=a.PipelineId and joint.WelderCode=c.WelderCode) + // else + // ( + // select count(1) from Batch_NDEItem as nde inner join Batch_BatchTrustItem as trust + // ON trust.TrustBatchItemId = nde.TrustBatchItemId + // inner join View_Pipeline_WeldJoint as joint + // ON joint.WeldJointId=trust.WeldJointId + // where joint.PipelineId=a.PipelineId and CHARINDEX(','+joint.WeldJointCode+',',','+a.WeldJonintCode+'')>0 and joint.WelderCode=c.WelderCode + // ) + // end + // ) as NdeNum, --检测口数量 + // (case when a.isAll=1 then + // (select count(1) from Batch_NDEItem as nde inner join Batch_BatchTrustItem as trust + // ON trust.TrustBatchItemId = nde.TrustBatchItemId + // inner join View_Pipeline_WeldJoint as joint + // ON joint.WeldJointId=trust.WeldJointId + // where joint.PipelineId=a.PipelineId and joint.JointAttribute='固定F' and joint.WelderCode=c.WelderCode) + // else + // ( + // select count(1) from Batch_NDEItem as nde inner join Batch_BatchTrustItem as trust + // ON trust.TrustBatchItemId = nde.TrustBatchItemId + // inner join View_Pipeline_WeldJoint as joint + // ON joint.WeldJointId=trust.WeldJointId + // where joint.PipelineId=a.PipelineId and CHARINDEX(','+joint.WeldJointCode+',',','+a.WeldJonintCode+'')>0 and joint.JointAttribute='固定F' and joint.WelderCode=c.WelderCode + // ) + // end + // ) as FNdeNum , --检测固定口数量 + // ( + // case when a.isAll=1 then + // (stuff((select ','+nde.NDEReportNo from Batch_NDEItem as nde inner join Batch_BatchTrustItem as trust + // ON trust.TrustBatchItemId = nde.TrustBatchItemId + // inner join View_Pipeline_WeldJoint as joint + // ON joint.WeldJointId=trust.WeldJointId + // where joint.PipelineId=a.PipelineId and joint.WelderCode=c.WelderCode FOR xml path ('') ),1,1,'')) + // else + // (stuff((select ','+nde.NDEReportNo from Batch_NDEItem as nde inner join Batch_BatchTrustItem as trust + // ON trust.TrustBatchItemId = nde.TrustBatchItemId + // inner join View_Pipeline_WeldJoint as joint + // ON joint.WeldJointId=trust.WeldJointId + // where joint.PipelineId=a.PipelineId and CHARINDEX(','+joint.WeldJointCode+',',','+a.WeldJonintCode+'')>0 and joint.WelderCode=c.WelderCode FOR xml path ( '' ) ),1,1,'') ) + // end) as NdeReportNo + //from + //PTP_PipelineList as a inner join + //PTP_TestPackage as b on a.PTP_ID=b.PTP_ID + //inner join View_Pipeline_WeldJoint as c + //on c.PipelineId=a.PipelineId + //inner join Pipeline_Pipeline as d + //on c.PipelineId=d.PipelineId + //where b.PTP_ID=@PTPID and b.ProjectId=@projectId + //) as t + //group by t.PipelineCode,t.MaterialCode,t.Specification,t.jointCount,t.FjointCount,t.WelderCode,t.WelderNum,t.NdeNum,t.FNdeNum,t.NdeReportNo"; SqlParameter[] parms = { @@ -4245,10 +4246,14 @@ group by t.PipelineCode,t.MaterialCode,t.Specification,t.jointCount,t.FjointCoun for (int k = rowIndex + 7; k < rowIndex + 16; k++) { ws.GetRow(k).Height = 18 * 20; - ws.AddMergedRegion(new CellRangeAddress(k, k, 0, 1)); - ws.AddMergedRegion(new CellRangeAddress(k, k, 3, 4)); - ws.AddMergedRegion(new CellRangeAddress(k, k, 5, 6)); - ws.AddMergedRegion(new CellRangeAddress(k, k, 7, 8)); + //小计行 + if (k == rowIndex + 15) + { + ws.AddMergedRegion(new CellRangeAddress(k, k, 0, 1)); + ws.AddMergedRegion(new CellRangeAddress(k, k, 3, 4)); + ws.AddMergedRegion(new CellRangeAddress(k, k, 5, 6)); + ws.AddMergedRegion(new CellRangeAddress(k, k, 7, 8)); + } ws.AddMergedRegion(new CellRangeAddress(k, k, 9, 10)); ws.AddMergedRegion(new CellRangeAddress(k, k, 11, 12)); ws.AddMergedRegion(new CellRangeAddress(k, k, 13, 14)); @@ -4260,6 +4265,13 @@ group by t.PipelineCode,t.MaterialCode,t.Specification,t.jointCount,t.FjointCoun var pageTb = GetPageToTable(tb, dStart, dEnd); int j = 0; + var pagelist = DataTableEx.ToList(pageTb); + + int oneNum = 1; + int twoNum = 1; + int threeNum = 1; + int fourNum = 1; + int fiveNum = 1; for (int k = 0; k < 8; k++) { int dataIndex = dataTit + j; @@ -4267,10 +4279,83 @@ group by t.PipelineCode,t.MaterialCode,t.Specification,t.jointCount,t.FjointCoun { DataRow dr = pageTb.Rows[k]; ws.GetRow(dataIndex).GetCell(0).SetCellValue(dr["PipelineCode"].ToString()); + var PipelineCodeNum = pagelist.Count(x => x.PipelineCode == dr["PipelineCode"].ToString()); + if (PipelineCodeNum > 1) + { + if (oneNum == 1) + { + oneNum += PipelineCodeNum; + ws.AddMergedRegion(new CellRangeAddress(dataIndex, dataIndex + PipelineCodeNum - 1, 0, 1)); + } + oneNum -= 1; + } + else + { + ws.AddMergedRegion(new CellRangeAddress(dataIndex, dataIndex, 0, 1)); + } + + ws.GetRow(dataIndex).GetCell(2).SetCellValue(dr["MaterialCode"].ToString()); + var MaterialCodeNum = pagelist.Count(x => x.MaterialCode == dr["MaterialCode"].ToString()); + if (MaterialCodeNum > 1) + { + if (twoNum == 1) + { + twoNum += MaterialCodeNum; + ws.AddMergedRegion(new CellRangeAddress(dataIndex, dataIndex + MaterialCodeNum - 1, 2, 2)); + } + twoNum -= 1; + } + ws.GetRow(dataIndex).GetCell(3).SetCellValue(dr["Specification"].ToString()); + var SpecificationNum = pagelist.Count(x => x.Specification == dr["Specification"].ToString()); + if (SpecificationNum > 1) + { + if (threeNum == 1) + { + threeNum += SpecificationNum; + ws.AddMergedRegion(new CellRangeAddress(dataIndex, dataIndex + SpecificationNum - 1, 3, 4)); + } + threeNum -= 1; + } + else + { + ws.AddMergedRegion(new CellRangeAddress(dataIndex, dataIndex, 3, 4)); + } + ws.GetRow(dataIndex).GetCell(5).SetCellValue(dr["jointCount"].ToString()); - ws.GetRow(dataIndex).GetCell(7).SetCellValue(dr["fjointCount"].ToString()); + var jointCountNum = pagelist.Count(x => x.jointCount == dr["jointCount"].ToString()); + if (jointCountNum > 1) + { + if (fourNum == 1) + { + fourNum += jointCountNum; + ws.AddMergedRegion(new CellRangeAddress(dataIndex, dataIndex + jointCountNum - 1, 5, 6)); + } + fourNum -= 1; + } + else + { + ws.AddMergedRegion(new CellRangeAddress(dataIndex, dataIndex, 5, 6)); + } + + ws.GetRow(dataIndex).GetCell(7).SetCellValue(dr["FjointCount"].ToString()); + var FjointCountNum = pagelist.Count(x => x.FjointCount == dr["FjointCount"].ToString()); + if (FjointCountNum > 1) + { + if (fiveNum == 1) + { + fiveNum += FjointCountNum; + ws.AddMergedRegion(new CellRangeAddress(dataIndex, dataIndex + FjointCountNum - 1, 7, 8)); + } + fiveNum -= 1; + } + else + { + ws.AddMergedRegion(new CellRangeAddress(dataIndex, dataIndex, 7, 8)); + } + + ws.GetRow(dataIndex).GetCell(9).SetCellValue(dr["WelderCode"].ToString()); ws.GetRow(dataIndex).GetCell(11).SetCellValue(dr["WelderNum"].ToString()); ws.GetRow(dataIndex).GetCell(13).SetCellValue(dr["NdeNum"].ToString()); @@ -4289,43 +4374,16 @@ group by t.PipelineCode,t.MaterialCode,t.Specification,t.jointCount,t.FjointCoun } ws.GetRow(dataIndex).GetCell(20).SetCellValue(dr["NdeReportNo"].ToString()); } + else + { + ws.AddMergedRegion(new CellRangeAddress(dataIndex, dataIndex, 0, 1)); + ws.AddMergedRegion(new CellRangeAddress(dataIndex, dataIndex, 3, 4)); + ws.AddMergedRegion(new CellRangeAddress(dataIndex, dataIndex, 5, 6)); + ws.AddMergedRegion(new CellRangeAddress(dataIndex, dataIndex, 7, 8)); + } j++; } - - //foreach (DataRow dr in pageTb.Rows) - //{ - // int dataIndex = dataTit + j; - - // ws.GetRow(dataIndex).GetCell(0).SetCellValue(dr["PipelineCode"].ToString()); - // ws.GetRow(dataIndex).GetCell(2).SetCellValue(dr["MaterialCode"].ToString()); - // ws.GetRow(dataIndex).GetCell(3).SetCellValue(dr["Specification"].ToString()); - // ws.GetRow(dataIndex).GetCell(5).SetCellValue(dr["totalNum"].ToString()); - // ws.GetRow(dataIndex).GetCell(7).SetCellValue(dr["Fnum"].ToString()); - // ws.GetRow(dataIndex).GetCell(9).SetCellValue(dr["WelderCode"].ToString()); - // ws.GetRow(dataIndex).GetCell(11).SetCellValue(dr["Fnum"].ToString()); - // ws.GetRow(dataIndex).GetCell(13).SetCellValue(dr["NdeNum"].ToString()); - // ws.GetRow(dataIndex).GetCell(15).SetCellValue(dr["FNdeNum"].ToString()); - // string a = string.IsNullOrEmpty(dr["NdeNum"].ToString()) ? "0" : dr["NdeNum"].ToString(); - // string b = string.IsNullOrEmpty(dr["Fnum"].ToString()) ? "0" : dr["Fnum"].ToString(); - - // if (b == "0") - // { - // ws.GetRow(dataIndex).GetCell(17).SetCellValue("0%"); - // } - // else - // { - // decimal rate = Math.Round((decimal.Parse(a) / decimal.Parse(b)), 2); - // ws.GetRow(dataIndex).GetCell(17).SetCellValue($"{rate * 100}%"); - // } - // ws.GetRow(dataIndex).GetCell(20).SetCellValue(dr["NdeCode"].ToString()); - // //模拟数据 - // j++; - //} - - - - ws.GetRow(rowIndex + 15).GetCell(0).SetCellValue("小计\r\nTotal"); ws.GetRow(rowIndex + 15).Height = 30 * 20; @@ -4409,7 +4467,7 @@ group by t.PipelineCode,t.MaterialCode,t.Specification,t.jointCount,t.FjointCoun #endregion - rowIndex = rowIndex + 23; + rowIndex = rowIndex + 24; } ws.PrintSetup.Landscape = true; @@ -7787,6 +7845,24 @@ group by t.PipelineCode,t.MaterialCode,t.Specification,t.jointCount,t.FjointCoun pict.Resize(w, h); } + + /// + /// 22 实体 + /// + private class template22Dto + { + public string PipelineCode { get; set; } + public string MaterialCode { get; set; } + public string Specification { get; set; } + public string jointCount { get; set; } + public string FjointCount { get; set; } + public string WelderCode { get; set; } + public string WelderNum { get; set; } + public string NdeNum { get; set; } + public string FNdeNum { get; set; } + public string NdeReportNo { get; set; } + } + #endregion } #endregion diff --git a/HJGL/FineUIPro.Web/common/DataTableEx.cs b/HJGL/FineUIPro.Web/common/DataTableEx.cs new file mode 100644 index 0000000..7bc25a7 --- /dev/null +++ b/HJGL/FineUIPro.Web/common/DataTableEx.cs @@ -0,0 +1,288 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using System.Reflection; +using System.Web; + +/// +/// DataTableEx 的摘要说明 +/// +public static class DataTableEx +{ + public static List ToList(this DataTable dt) where T : new() + { + List ts = new List(); + foreach (DataRow dr in dt.Rows) + { + T t = new T(); + foreach (var c in dt.Columns) + { + object value = dr[c.ToString()]; + if (value != DBNull.Value) + { + var p = t.GetType().GetProperty(c.ToString()); + if (p != null) + { + p.SetValue(t, ConvertHelper.ChangeType(value, p.PropertyType), null); + } + } + } + ts.Add(t); + } + return ts; + } + + + public static List ConvertTo(DataTable datatable) where T : new() + { + var temp = new List(); + try + { + var columnsNames = (from DataColumn dataColumn in datatable.Columns select dataColumn.ColumnName).ToList(); + temp = datatable.AsEnumerable().ToList().ConvertAll(row => GetObject(row, columnsNames)); + return temp; + } + catch + { + return temp; + } + } + + + private static T GetObject(DataRow row, List columnsName) where T : new() + { + T obj = new T(); + try + { + string columnname = ""; + PropertyInfo[] Properties = typeof(T).GetProperties(); + foreach (PropertyInfo objProperty in Properties) + { + columnname = columnsName.Find(name => name.ToLower() == objProperty.Name.ToLower()); + if (!string.IsNullOrEmpty(columnname)) + { + var value = row[columnname]; + if (!string.IsNullOrEmpty(value.ToString())) + { + Type type; + type = Nullable.GetUnderlyingType(objProperty.PropertyType) ?? objProperty.PropertyType; + objProperty.SetValue(obj, + type == value.GetType() + ? Convert.ChangeType(value, type) + : System.Enum.ToObject(type, value), null); + } + } + } + return obj; + } + catch (Exception exception) + { + return obj; + } + } + + + /// + /// 单字段值 + /// + /// + public static List ToList(this DataTable dt) + { + List ts = new List(); + foreach (DataRow dr in dt.Rows) + { + foreach (var c in dt.Columns) + { + object value = dr[c.ToString()]; + if (value != DBNull.Value) + { + ts.Add(value.ToString()); + } + } + } + return ts; + } + + + public static T ToData(this DataTable dt) where T : new() + { + if (dt.Rows.Count > 1) + { + throw new Exception(""); + } + List ts = new List(); + foreach (DataRow dr in dt.Rows) + { + T t = new T(); + foreach (var c in dt.Columns) + { + object value = dr[c.ToString()]; + if (value != DBNull.Value) + { + var p = t.GetType().GetProperty(c.ToString()); + if (p != null) + { + p.SetValue(t, ConvertHelper.ChangeType(value, p.PropertyType), null); + } + } + } + return t; + } + return default(T); + } + + public static void FillData(this DataTable dt, ref T t) where T : new() + { + if (dt.Rows.Count > 1) + { + throw new Exception(""); + } + foreach (DataRow dr in dt.Rows) + { + + foreach (var c in dt.Columns) + { + object value = dr[c.ToString()]; + if (value != DBNull.Value) + { + var p = t.GetType().GetProperty(c.ToString()); + if (p != null) + { + p.SetValue(t, ConvertHelper.ChangeType(value, p.PropertyType), null); + } + } + } + } + } + + /// + /// list转化为table + /// + /// + /// + /// + public static DataTable ListToDataTable(List entitys) + { + + //检查实体集合不能为空 + if (entitys == null || entitys.Count < 1) + { + return new DataTable(); + } + + //取出第一个实体的所有Propertie + Type entityType = entitys[0].GetType(); + PropertyInfo[] entityProperties = entityType.GetProperties(); + + //生成DataTable的structure + //生产代码中,应将生成的DataTable结构Cache起来,此处略 + DataTable dt = new DataTable("dt"); + for (int i = 0; i < entityProperties.Length; i++) + { + //dt.Columns.Add(entityProperties[i].Name, entityProperties[i].PropertyType); + dt.Columns.Add(entityProperties[i].Name); + } + + //将所有entity添加到DataTable中 + foreach (object entity in entitys) + { + //检查所有的的实体都为同一类型 + if (entity.GetType() != entityType) + { + throw new Exception("要转换的集合元素类型不一致"); + } + object[] entityValues = new object[entityProperties.Length]; + for (int i = 0; i < entityProperties.Length; i++) + { + entityValues[i] = entityProperties[i].GetValue(entity, null); + + } + dt.Rows.Add(entityValues); + } + return dt; + } + + /// + /// 查询指定条数分页 + /// + /// + public static DataTable GetPageToTable(DataTable dt, int StartNum, int EndNum) + { + //0页代表每页数据,直接返回 + if (EndNum == 0) return dt; + //数据源为空返回空DataTable + if (dt == null) return new DataTable(); + + DataTable newdt = dt.Copy(); + newdt.Clear();//copy dt的框架 + + if (StartNum >= dt.Rows.Count) + return newdt;//源数据记录数小于等于要显示的记录,直接返回dt + + if (EndNum > dt.Rows.Count) + EndNum = dt.Rows.Count; + for (int i = StartNum; i <= EndNum - 1; i++) + { + DataRow newdr = newdt.NewRow(); + DataRow dr = dt.Rows[i]; + foreach (DataColumn column in dt.Columns) + { + newdr[column.ColumnName] = dr[column.ColumnName]; + } + newdt.Rows.Add(newdr); + } + return newdt; + } +} +public static class ConvertHelper +{ + public static object ChangeType(object obj, Type conversionType) + { + return ChangeType(obj, conversionType, System.Threading.Thread.CurrentThread.CurrentCulture); + } + + public static object ChangeType(object obj, Type conversionType, IFormatProvider provider) + { + + #region Nullable + Type nullableType = Nullable.GetUnderlyingType(conversionType); + if (nullableType != null) + { + if (obj == null) + { + return null; + } + return Convert.ChangeType(obj, nullableType, provider); + } + #endregion + if (typeof(System.Enum).IsAssignableFrom(conversionType)) + { + return Enum.Parse(conversionType, obj.ToString()); + } + return Convert.ChangeType(obj, conversionType, provider); + } + + + /// + /// 分页 + /// + /// + public static List PageList(List list, out int total, int pageIndex = 1, int pageSize = 10) + { + pageIndex = pageIndex - 1; + total = 0; + if (list == null || list.Count == 0) + return list; + total = list.Count; + int startIndex = pageIndex * pageSize; + if (startIndex + pageSize > list.Count) + { + pageSize = list.Count - startIndex; + } + + if (pageSize <= 0) return new List(); + + return list.GetRange(startIndex, pageSize); + } +} \ No newline at end of file