This commit is contained in:
佘春生 2024-06-14 09:02:43 +08:00
commit 08a48b7e3c
3 changed files with 481 additions and 116 deletions

View File

@ -4185,6 +4185,7 @@
<DependentUpon>UnitView.aspx</DependentUpon> <DependentUpon>UnitView.aspx</DependentUpon>
</Compile> </Compile>
<Compile Include="common\CommonPrint.cs" /> <Compile Include="common\CommonPrint.cs" />
<Compile Include="common\DataTableEx.cs" />
<Compile Include="common\ProjectSet\EditTeamGroup.aspx.cs"> <Compile Include="common\ProjectSet\EditTeamGroup.aspx.cs">
<DependentUpon>EditTeamGroup.aspx</DependentUpon> <DependentUpon>EditTeamGroup.aspx</DependentUpon>
<SubType>ASPXCodeBehind</SubType> <SubType>ASPXCodeBehind</SubType>

View File

@ -7,6 +7,7 @@ using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Web; using System.Web;
using System.Web.UI; using System.Web.UI;
@ -3995,85 +3996,85 @@ namespace FineUIPro.Web.WeldingProcess.TestPackageManage
{ {
var info = GetTestPackageInfo(this.tvControlItem.SelectedNodeID); 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 * from (
select d.PipelineCode, //select d.PipelineCode,
(select MaterialCode from Base_Material as m where d.MainMaterialId=m.MaterialId) as MaterialCode , //(select MaterialCode from Base_Material as m where d.MainMaterialId=m.MaterialId) as MaterialCode ,
d.Specification, //d.Specification,
(select count(1) from Pipeline_WeldJoint as joint where joint.PipelineId=a.PipelineId) as jointCount, --线 //(select count(1) from Pipeline_WeldJoint as joint where joint.PipelineId=a.PipelineId) as jointCount, --当前管线的焊口总
( case when a.isAll=1 then // ( case when a.isAll=1 then
(select count(1) from Pipeline_WeldJoint as joint where joint.PipelineId=a.PipelineId and joint.JointAttribute='F') // (select count(1) from Pipeline_WeldJoint as joint where joint.PipelineId=a.PipelineId and joint.JointAttribute='固定F')
else // 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 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, --线 // end ) as FjointCount, --当前管线的焊口的固定口总
c.WelderCode, -- //c.WelderCode, --焊工
( case when a.isAll=1 then // ( 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) // (select count(1) from View_Pipeline_WeldJoint as vjoint where vjoint.PipelineId=a.PipelineId and vjoint.WelderCode=c.WelderCode)
else // else
(select count(1) from View_Pipeline_WeldJoint as vjoint where vjoint.PipelineId=a.PipelineId // (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) // and CHARINDEX(','+vjoint.WeldJointCode+',',','+a.WeldJonintCode+'')>0 and vjoint.WelderCode=c.WelderCode)
end ) as WelderNum, // end ) as WelderNum,
(case when a.isAll=1 then // (case when a.isAll=1 then
(select count(1) from Batch_NDEItem as nde inner join Batch_BatchTrustItem as trust // (select count(1) from Batch_NDEItem as nde inner join Batch_BatchTrustItem as trust
ON trust.TrustBatchItemId = nde.TrustBatchItemId // ON trust.TrustBatchItemId = nde.TrustBatchItemId
inner join View_Pipeline_WeldJoint as joint // inner join View_Pipeline_WeldJoint as joint
ON joint.WeldJointId=trust.WeldJointId // ON joint.WeldJointId=trust.WeldJointId
where joint.PipelineId=a.PipelineId and joint.WelderCode=c.WelderCode) // where joint.PipelineId=a.PipelineId and joint.WelderCode=c.WelderCode)
else // else
( // (
select count(1) from Batch_NDEItem as nde inner join Batch_BatchTrustItem as trust // select count(1) from Batch_NDEItem as nde inner join Batch_BatchTrustItem as trust
ON trust.TrustBatchItemId = nde.TrustBatchItemId // ON trust.TrustBatchItemId = nde.TrustBatchItemId
inner join View_Pipeline_WeldJoint as joint // inner join View_Pipeline_WeldJoint as joint
ON joint.WeldJointId=trust.WeldJointId // ON joint.WeldJointId=trust.WeldJointId
where joint.PipelineId=a.PipelineId and CHARINDEX(','+joint.WeldJointCode+',',','+a.WeldJonintCode+'')>0 and joint.WelderCode=c.WelderCode // where joint.PipelineId=a.PipelineId and CHARINDEX(','+joint.WeldJointCode+',',','+a.WeldJonintCode+'')>0 and joint.WelderCode=c.WelderCode
) // )
end // end
) as NdeNum, -- // ) as NdeNum, --检测口数
(case when a.isAll=1 then // (case when a.isAll=1 then
(select count(1) from Batch_NDEItem as nde inner join Batch_BatchTrustItem as trust // (select count(1) from Batch_NDEItem as nde inner join Batch_BatchTrustItem as trust
ON trust.TrustBatchItemId = nde.TrustBatchItemId // ON trust.TrustBatchItemId = nde.TrustBatchItemId
inner join View_Pipeline_WeldJoint as joint // inner join View_Pipeline_WeldJoint as joint
ON joint.WeldJointId=trust.WeldJointId // ON joint.WeldJointId=trust.WeldJointId
where joint.PipelineId=a.PipelineId and joint.JointAttribute='F' and joint.WelderCode=c.WelderCode) // where joint.PipelineId=a.PipelineId and joint.JointAttribute='固定F' and joint.WelderCode=c.WelderCode)
else // else
( // (
select count(1) from Batch_NDEItem as nde inner join Batch_BatchTrustItem as trust // select count(1) from Batch_NDEItem as nde inner join Batch_BatchTrustItem as trust
ON trust.TrustBatchItemId = nde.TrustBatchItemId // ON trust.TrustBatchItemId = nde.TrustBatchItemId
inner join View_Pipeline_WeldJoint as joint // inner join View_Pipeline_WeldJoint as joint
ON joint.WeldJointId=trust.WeldJointId // 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 // where joint.PipelineId=a.PipelineId and CHARINDEX(','+joint.WeldJointCode+',',','+a.WeldJonintCode+'')>0 and joint.JointAttribute='固定F' and joint.WelderCode=c.WelderCode
) // )
end // end
) as FNdeNum , -- // ) as FNdeNum , --检测固定口数
( // (
case when a.isAll=1 then // case when a.isAll=1 then
(stuff((select ','+nde.NDEReportNo from Batch_NDEItem as nde inner join Batch_BatchTrustItem as trust // (stuff((select ','+nde.NDEReportNo from Batch_NDEItem as nde inner join Batch_BatchTrustItem as trust
ON trust.TrustBatchItemId = nde.TrustBatchItemId // ON trust.TrustBatchItemId = nde.TrustBatchItemId
inner join View_Pipeline_WeldJoint as joint // inner join View_Pipeline_WeldJoint as joint
ON joint.WeldJointId=trust.WeldJointId // ON joint.WeldJointId=trust.WeldJointId
where joint.PipelineId=a.PipelineId and joint.WelderCode=c.WelderCode FOR xml path ('') ),1,1,'')) // where joint.PipelineId=a.PipelineId and joint.WelderCode=c.WelderCode FOR xml path ('') ),1,1,''))
else // else
(stuff((select ','+nde.NDEReportNo from Batch_NDEItem as nde inner join Batch_BatchTrustItem as trust // (stuff((select ','+nde.NDEReportNo from Batch_NDEItem as nde inner join Batch_BatchTrustItem as trust
ON trust.TrustBatchItemId = nde.TrustBatchItemId // ON trust.TrustBatchItemId = nde.TrustBatchItemId
inner join View_Pipeline_WeldJoint as joint // inner join View_Pipeline_WeldJoint as joint
ON joint.WeldJointId=trust.WeldJointId // 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,'') ) // 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 // end) as NdeReportNo
from //from
PTP_PipelineList as a inner join //PTP_PipelineList as a inner join
PTP_TestPackage as b on a.PTP_ID=b.PTP_ID //PTP_TestPackage as b on a.PTP_ID=b.PTP_ID
inner join View_Pipeline_WeldJoint as c //inner join View_Pipeline_WeldJoint as c
on c.PipelineId=a.PipelineId //on c.PipelineId=a.PipelineId
inner join Pipeline_Pipeline as d //inner join Pipeline_Pipeline as d
on c.PipelineId=d.PipelineId //on c.PipelineId=d.PipelineId
where b.PTP_ID=@PTPID and b.ProjectId=@projectId //where b.PTP_ID=@PTPID and b.ProjectId=@projectId
) as t //) as t
group by t.PipelineCode,t.MaterialCode,t.Specification,t.jointCount,t.FjointCount,t.WelderCode,t.WelderNum,t.NdeNum,t.FNdeNum,t.NdeReportNo"; //group by t.PipelineCode,t.MaterialCode,t.Specification,t.jointCount,t.FjointCount,t.WelderCode,t.WelderNum,t.NdeNum,t.FNdeNum,t.NdeReportNo";
SqlParameter[] parms = 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++) for (int k = rowIndex + 7; k < rowIndex + 16; k++)
{ {
ws.GetRow(k).Height = 18 * 20; ws.GetRow(k).Height = 18 * 20;
//小计行
if (k == rowIndex + 15)
{
ws.AddMergedRegion(new CellRangeAddress(k, k, 0, 1)); ws.AddMergedRegion(new CellRangeAddress(k, k, 0, 1));
ws.AddMergedRegion(new CellRangeAddress(k, k, 3, 4)); ws.AddMergedRegion(new CellRangeAddress(k, k, 3, 4));
ws.AddMergedRegion(new CellRangeAddress(k, k, 5, 6)); ws.AddMergedRegion(new CellRangeAddress(k, k, 5, 6));
ws.AddMergedRegion(new CellRangeAddress(k, k, 7, 8)); ws.AddMergedRegion(new CellRangeAddress(k, k, 7, 8));
}
ws.AddMergedRegion(new CellRangeAddress(k, k, 9, 10)); ws.AddMergedRegion(new CellRangeAddress(k, k, 9, 10));
ws.AddMergedRegion(new CellRangeAddress(k, k, 11, 12)); ws.AddMergedRegion(new CellRangeAddress(k, k, 11, 12));
ws.AddMergedRegion(new CellRangeAddress(k, k, 13, 14)); 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); var pageTb = GetPageToTable(tb, dStart, dEnd);
int j = 0; int j = 0;
var pagelist = DataTableEx.ToList<template22Dto>(pageTb);
int oneNum = 1;
int twoNum = 1;
int threeNum = 1;
int fourNum = 1;
int fiveNum = 1;
for (int k = 0; k < 8; k++) for (int k = 0; k < 8; k++)
{ {
int dataIndex = dataTit + j; 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]; DataRow dr = pageTb.Rows[k];
ws.GetRow(dataIndex).GetCell(0).SetCellValue(dr["PipelineCode"].ToString()); 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()); 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()); 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(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(9).SetCellValue(dr["WelderCode"].ToString());
ws.GetRow(dataIndex).GetCell(11).SetCellValue(dr["WelderNum"].ToString()); ws.GetRow(dataIndex).GetCell(11).SetCellValue(dr["WelderNum"].ToString());
ws.GetRow(dataIndex).GetCell(13).SetCellValue(dr["NdeNum"].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()); 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++; 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).GetCell(0).SetCellValue("小计\r\nTotal");
ws.GetRow(rowIndex + 15).Height = 30 * 20; ws.GetRow(rowIndex + 15).Height = 30 * 20;
@ -4409,7 +4467,7 @@ group by t.PipelineCode,t.MaterialCode,t.Specification,t.jointCount,t.FjointCoun
#endregion #endregion
rowIndex = rowIndex + 23; rowIndex = rowIndex + 24;
} }
ws.PrintSetup.Landscape = true; ws.PrintSetup.Landscape = true;
@ -7787,6 +7845,24 @@ group by t.PipelineCode,t.MaterialCode,t.Specification,t.jointCount,t.FjointCoun
pict.Resize(w, h); pict.Resize(w, h);
} }
/// <summary>
/// 22 实体
/// </summary>
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
} }
#endregion #endregion

View File

@ -0,0 +1,288 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Web;
/// <summary>
/// DataTableEx 的摘要说明
/// </summary>
public static class DataTableEx
{
public static List<T> ToList<T>(this DataTable dt) where T : new()
{
List<T> ts = new List<T>();
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<T> ConvertTo<T>(DataTable datatable) where T : new()
{
var temp = new List<T>();
try
{
var columnsNames = (from DataColumn dataColumn in datatable.Columns select dataColumn.ColumnName).ToList();
temp = datatable.AsEnumerable().ToList().ConvertAll<T>(row => GetObject<T>(row, columnsNames));
return temp;
}
catch
{
return temp;
}
}
private static T GetObject<T>(DataRow row, List<string> 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;
}
}
/// <summary>
/// 单字段值
/// </summary>
/// <returns></returns>
public static List<string> ToList(this DataTable dt)
{
List<string> ts = new List<string>();
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<T>(this DataTable dt) where T : new()
{
if (dt.Rows.Count > 1)
{
throw new Exception("");
}
List<T> ts = new List<T>();
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<T>(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);
}
}
}
}
}
/// <summary>
/// list转化为table
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="entitys"></param>
/// <returns></returns>
public static DataTable ListToDataTable<T>(List<T> 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;
}
/// <summary>
/// 查询指定条数分页
/// </summary>
/// <returns></returns>
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);
}
/// <summary>
/// 分页
/// </summary>
/// <returns></returns>
public static List<T> PageList<T>(List<T> 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<T>();
return list.GetRange(startIndex, pageSize);
}
}