ZHJA_HJGL/HJGL_ZH/FineUIPro.Web/YLRQ/HardProessManage/HardProessTrust.aspx.cs

1019 lines
48 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using NPOI.XSSF.UserModel;
using System.IO;
namespace FineUIPro.Web.YLRQ.HardProessManage
{
public partial class HardProessTrust : PageBase
{
/// <summary>
/// 硬度委托主键
/// </summary>
public string HardProessTrustId { get { return (string)ViewState["HardProessTrustId"]; } set { ViewState["HardProessTrustId"] = value; } }
#region
/// <summary>
/// 加载页面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.ddlPageSize.SelectedValue = this.Grid1.PageSize.ToString();
////施工号
//this.drpProjectId.DataTextField = "ProjectCode";
//this.drpProjectId.DataValueField = "ProjectId";
//this.drpProjectId.DataSource = Base_ProjectService.GetOnProjectListByUserId(this.CurrUser.UserId, "2");
//this.drpProjectId.DataBind();
//Funs.FineUIPleaseSelect(this.drpProjectId);
//this.drpProjectId.SelectedValue = this.CurrUser.LoginProjectId;
BridProjectGrid();
this.HardProessTrustId = string.Empty;
this.InitTreeMenu();//加载树
}
}
#endregion
/// <summary>
/// 绑定项目
/// </summary>
public void BridProjectGrid()
{
GridProject.DataSource = BLL.Base_ProjectService.GetYlrqProjectListByUserId(this.CurrUser.UserId, "2");
GridProject.DataBind();
drpProject.Value = this.CurrUser.LoginProjectId;
}
/// <summary>
/// 施工号下拉框
/// </summary>
protected void drpProject_TextChanged(object sender, EventArgs e)
{
this.InitTreeMenu();
this.tvControlItem.SelectedNodeID = drpProject.Value;
this.BindGrid();
}
#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);
List<Base_Project> projects = BLL.Base_ProjectService.GetOnProjectListByUserId(this.CurrUser.UserId, "2");
if (this.drpProject.Values.Length > 0)
{
projects = projects.Where(x => this.drpProject.Values.Contains(x.ProjectId)).ToList();
}
List<PV_CH_HardProessTrust> trustLists = new List<PV_CH_HardProessTrust>(); ///硬度委托单
if (this.rblPrint.SelectedValue == "2") //全部
{
trustLists = Funs.DB.PV_CH_HardProessTrust.Where(p => p.HardProessTrustCode.Contains(txtSearchNo.Text.Trim())).ToList();
}
else if (this.rblPrint.SelectedValue == "1") //已打印
{
trustLists = Funs.DB.PV_CH_HardProessTrust.Where(p => p.HardProessTrustCode.Contains(txtSearchNo.Text.Trim()) && p.IsPrint == true).ToList();
}
else if (this.rblPrint.SelectedValue == "0") //未打印
{
trustLists = Funs.DB.PV_CH_HardProessTrust.Where(p => p.HardProessTrustCode.Contains(txtSearchNo.Text.Trim()) && (p.IsPrint == null || p.IsPrint == false)).ToList();
}
foreach (var item in projects)
{
TreeNode rootUnitNode = new TreeNode();//定义根节点
rootUnitNode.NodeID = item.ProjectId;
if ((from x in trustLists where x.ProjectId == item.ProjectId && (x.IsPrint == null || x.IsPrint == false) select x).Count() > 0) //存在未打印的委托单
{
rootUnitNode.Text = $"<font color='#FF7575'>{item.ProjectCode},{item.EquipmentName},{item.ProductNum}</font>";
}
else
{
rootUnitNode.Text = $"{item.ProjectCode},{item.EquipmentName},{item.ProductNum}";
}
rootUnitNode.ToolTip = item.ProjectName;
rootUnitNode.CommandName = "项目名称";
rootUnitNode.EnableClickEvent = true;
rootUnitNode.EnableExpandEvent = true;
rootNode.Nodes.Add(rootUnitNode);
if (!string.IsNullOrEmpty(this.txtSearchNo.Text.Trim()))
{
var projectTrustLists = (from x in trustLists where x.ProjectId == item.ProjectId orderby x.CreateDate descending select x).ToList();
if (projectTrustLists.Count > 0)
{
rootUnitNode.Expanded = true;
this.BindNodes(rootUnitNode, projectTrustLists);
}
}
else
{
TreeNode tn = new TreeNode();
tn.NodeID = "temp";
tn.Text = "正在加载...";
rootUnitNode.Nodes.Add(tn);
}
}
}
/// <summary>
/// 绑定树节点
/// </summary>
private void BindNodes(TreeNode node, List<PV_CH_HardProessTrust> trustList)
{
foreach (var item in trustList)
{
TreeNode newNode = new TreeNode();
if (item.IsPrint == true)
{
newNode.Text = item.HardProessTrustCode;
}
else
{
newNode.Text = "<font color='#FF7575'>" + item.HardProessTrustCode + "</font>";
}
newNode.NodeID = item.HardProessTrustId;
newNode.ToolTip = item.HardProessTrustCode;
newNode.CommandName = "委托单号";
newNode.EnableClickEvent = true;
node.Nodes.Add(newNode);
}
}
/// <summary>
/// 数加载
/// </summary>
protected void tvControlItem_NodeExpand(object sender, TreeNodeEventArgs e)
{
if (e.Node.Nodes != null)
{
e.Node.Nodes.Clear();
}
List<PV_CH_HardProessTrust> trustLists = new List<PV_CH_HardProessTrust>(); ///硬度委托单
if (this.rblPrint.SelectedValue == "2") //全部
{
trustLists = Funs.DB.PV_CH_HardProessTrust.Where(x => x.HardProessTrustCode.Contains(this.txtSearchNo.Text.Trim()) && x.ProjectId == e.Node.NodeID).OrderBy(p => p.CreateDate).ToList();
}
else if (this.rblPrint.SelectedValue == "1") //已打印
{
trustLists = Funs.DB.PV_CH_HardProessTrust.Where(x => x.HardProessTrustCode.Contains(this.txtSearchNo.Text.Trim()) && x.ProjectId == e.Node.NodeID && x.IsPrint == true).OrderBy(p => p.CreateDate).ToList();
}
else if (this.rblPrint.SelectedValue == "0") //未打印
{
trustLists = Funs.DB.PV_CH_HardProessTrust.Where(x => x.HardProessTrustCode.Contains(this.txtSearchNo.Text.Trim()) && x.ProjectId == e.Node.NodeID && (x.IsPrint == null || x.IsPrint == false)).OrderBy(p => p.CreateDate).ToList();
}
this.BindNodes(e.Node, trustLists);
}
/// <summary>
/// 点击TreeView
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void tvControlItem_NodeCommand(object sender, TreeCommandEventArgs e)
{
this.HardProessTrustId = tvControlItem.SelectedNodeID;
this.BindGrid();
}
#endregion
#region
/// <summary>
/// 数据绑定
/// </summary>
private void BindGrid()
{
string strSql = string.Empty;
List<SqlParameter> listStr = new List<SqlParameter>();
this.SetTextTemp();
//页面输入提交信息
this.PageInfoLoad();
string projectIds = Base_ProjectService.GetStrOnProjectIds(this.CurrUser.UserId, "2");
if (this.tvControlItem.SelectedNode != null)
{
if (this.tvControlItem.SelectedNode.CommandName == "委托单号")
{
strSql = @"SELECT * FROM dbo.PV_View_CH_HardProessTrustItem AS Trust WHERE Trust.ProjectId= @ProjectId AND Trust.HardProessTrustId=@HardProessTrustId and IsNeedHardTest=1";
var trust = Funs.DB.PV_CH_HardProessTrust.FirstOrDefault(e => e.HardProessTrustId == this.HardProessTrustId);
listStr.Add(new SqlParameter("@ProjectId", trust != null ? trust.ProjectId : this.CurrUser.LoginProjectId));
listStr.Add(new SqlParameter("@HardProessTrustId", this.HardProessTrustId));
}
else if (this.tvControlItem.SelectedNode.CommandName == "项目名称")
{
strSql = @"SELECT * FROM dbo.PV_View_CH_HardProessTrustItem AS Trust WHERE Trust.ProjectId= @ProjectId and Trust.HardProessTrustId is not null and IsNeedHardTest=1";
var trust = Funs.DB.PV_CH_HardProessTrust.FirstOrDefault(e => e.HardProessTrustId == this.HardProessTrustId);
listStr.Add(new SqlParameter("@ProjectId", trust != null ? trust.ProjectId : this.CurrUser.LoginProjectId));
}
else //选择项目根节点或没有选择节点
{
strSql = @"SELECT * FROM dbo.PV_View_CH_HardProessTrustItem AS Trust WHERE Trust.HardProessTrustId is not null AND CHARINDEX(Trust.ProjectId,@ProjectId)>0 and IsNeedHardTest=1";
listStr.Add(new SqlParameter("@ProjectId", projectIds));
}
if (!string.IsNullOrEmpty(this.txtWeldingCode.Text.Trim()))
{
strSql += @" and Trust.WeldingCode like '%'+@WeldingCode+'%' ";
listStr.Add(new SqlParameter("@WeldingCode", this.txtWeldingCode.Text.Trim()));
}
SqlParameter[] parameter = listStr.ToArray();
DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
// 2.获取当前分页数据
Grid1.RecordCount = tb.Rows.Count;
var table = this.GetPagedDataTable(Grid1, tb);
Grid1.DataSource = table;
Grid1.DataBind();
}
}
/// <summary>
/// 查询
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void TextBox_TextChanged(object sender, EventArgs e)
{
this.BindGrid();
}
#region
/// <summary>
/// 加载页面输入提交信息
/// </summary>
private void PageInfoLoad()
{
//this.btnEdit.Hidden = true;
this.btnDelete.Hidden = true;
this.btnPrint.Hidden = true;
var trust = Funs.DB.PV_CH_HardProessTrust.FirstOrDefault(e => e.HardProessTrustId == this.HardProessTrustId);
if (trust != null)
{
this.txtHardProessTrustCode.Text = trust.HardProessTrustCode;
if (!string.IsNullOrEmpty(trust.TrustUnitId))
{
var unit = BLL.Base_UnitService.GetUnit(trust.TrustUnitId);
if (unit != null)
{
this.drpCH_TrustUnit.Text = unit.UnitName;
}
}
this.txtProjectName.Text = trust.ProjectName;
this.txtConstructionNo.Text = trust.ConstructionNo;
if (!string.IsNullOrEmpty(trust.TrstManId))
{
var hardMan = BLL.Sys_UserService.GetUsersByUserId(trust.TrstManId);
if (hardMan != null)
{
this.drpCH_TrustMan.Text = hardMan.UserName;
}
}
//this.btnEdit.Hidden = false;
if (trust.IsPrint == true)
{
this.cbIsPrint.Checked = true;
//this.btnEdit.Hidden = true;
}
else
{
this.cbIsPrint.Checked = false;
}
this.btnPrint.Hidden = false;
this.cbIsPrint.Hidden = false;
this.btnDelete.Hidden = false;
}
}
#endregion
/// <summary>
/// 情况
/// </summary>
private void SetTextTemp()
{
this.txtHardProessTrustCode.Text = string.Empty;
this.drpCH_TrustUnit.Text = string.Empty;
this.txtProjectName.Text = string.Empty;
this.txtConstructionNo.Text = string.Empty;
this.drpCH_TrustMan.Text = string.Empty;
this.cbIsPrint.Checked = false;
}
#endregion
#region
/// <summary>
/// 页索引改变事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
{
BindGrid();
}
/// <summary>
/// 排序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Grid1_Sort(object sender, GridSortEventArgs e)
{
BindGrid();
}
/// <summary>
/// 分页选择下拉改变事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
{
Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
BindGrid();
}
#endregion
#region
/// <summary>
/// 增加硬度委托
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnNew_Click(object sender, EventArgs e)
{
if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.PV_HardProessTrust, Const.BtnAdd))
{
if (this.tvControlItem.SelectedNode != null && this.tvControlItem.SelectedNode.CommandName == "项目名称")
{
this.SetTextTemp();
string window = String.Format("HardProessTrustEdit.aspx?HardProessTrustId={0}&ProjectId={1}", string.Empty, this.tvControlItem.SelectedNodeID, "新增 - ");
PageContext.RegisterStartupScript(Window1.GetSaveStateReference(this.hdTrustID.ClientID)
+ Window1.GetShowReference(window));
}
else
{
ShowNotify("请先选择项目!", MessageBoxIcon.Warning);
}
}
else
{
ShowNotify("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
}
}
/// <summary>
/// 编辑硬度委托
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnEdit_Click(object sender, EventArgs e)
{
if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.PV_HardProessTrust, Const.BtnEdit))
{
var trustManage = Funs.DB.PV_CH_HardProessTrust.FirstOrDefault(p => p.HardProessTrustId == this.HardProessTrustId);
if (trustManage != null)
{
string window = String.Format("HardProessTrustEdit.aspx?HardProessTrustId={0}", this.HardProessTrustId, "编辑 - ");
PageContext.RegisterStartupScript(Window1.GetSaveStateReference(this.hdTrustID.ClientID)
+ Window1.GetShowReference(window));
}
else
{
ShowNotify("请选择要修改的硬度委托记录!", MessageBoxIcon.Warning);
}
}
else
{
ShowNotify("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
}
}
/// <summary>
/// 删除硬度委托
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnDelete_Click(object sender, EventArgs e)
{
if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.PV_HardProessTrust, Const.BtnDelete))
{
var trustManage = Funs.DB.PV_CH_HardProessTrust.FirstOrDefault(p => p.HardProessTrustId == this.HardProessTrustId);
if (trustManage != null)
{
if (trustManage.IsPrint == true)
{
ShowNotify("硬度委托已打印,无法删除!", MessageBoxIcon.Warning);
return;
}
//更新热处理结果反馈表
var hotResults = Funs.DB.PV_CH_HotProessResult.Where(p => p.HardProessTrustId == trustManage.HardProessTrustId);
if (hotResults.Count() > 0)
{
foreach (var hotResult in hotResults)
{
hotResult.IsNeedHardTest = false;
hotResult.HardProessTrustId = null;
hotResult.CompleteDate = null;
Funs.DB.SubmitChanges();
}
}
//删除硬度结果反馈表
var items = from x in Funs.DB.PV_CH_HardProessResult where x.HardProessTrustId == this.HardProessTrustId select x;
if (items != null)
{
var hotids = items.Select(p => p.HotId).Distinct();
var hotwelds = Funs.DB.PV_View_HotWeldInformation.Where(p => hotids.Contains(p.HotId));
var weldIds = hotwelds.Select(p => p.WeldingId).ToList();
var welds = Funs.DB.PV_WeldInformation.Where(p => weldIds.Contains(p.WeldingId)).ToList();
foreach (var itemId in weldIds)
{
var itemWeld = welds.FirstOrDefault(p => p.WeldingId == itemId);
if (itemWeld != null)
{
itemWeld.HardResultDate = null;
itemWeld.HardProessResultOK = null;
Funs.DB.SubmitChanges();
}
}
}
Funs.DB.PV_CH_HardProessResult.DeleteAllOnSubmit(items);
var itemProessItem = from x in Funs.DB.PV_HardProessItem where x.HardProessTrustId == HardProessTrustId select x;
if (itemProessItem != null)
{
Funs.DB.PV_HardProessItem.DeleteAllOnSubmit(itemProessItem);
}
Funs.DB.PV_CH_HardProessTrust.DeleteOnSubmit(trustManage);
Funs.DB.SubmitChanges();
Alert.ShowInTop("删除成功!", MessageBoxIcon.Success);
this.InitTreeMenu();
SetTextTemp();
this.Grid1.DataSource = null;
this.Grid1.DataBind();
}
else
{
ShowNotify("请选择要删除的硬度委托记录!", MessageBoxIcon.Warning);
}
}
else
{
ShowNotify("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
}
}
#endregion
#region
/// <summary>
/// 关闭弹出窗口
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Window1_Close(object sender, WindowCloseEventArgs e)
{
this.InitTreeMenu();
this.HardProessTrustId = this.hdTrustID.Text;
this.tvControlItem.SelectedNodeID = this.HardProessTrustId;
this.BindGrid();
this.hdTrustID.Text = string.Empty;
}
/// <summary>
/// 查询
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Tree_TextChanged(object sender, EventArgs e)
{
this.InitTreeMenu();
}
/// <summary>
/// 是否打印
/// </summary>
protected void cbIsPrint_CheckedChanged(object sender, CheckedEventArgs e)
{
if (this.tvControlItem.SelectedNode.CommandName == "委托单号")
{
Model.PV_CH_HardProessTrust trust = Funs.DB.PV_CH_HardProessTrust.FirstOrDefault(p => p.HardProessTrustId == this.HardProessTrustId);
if (trust != null)
{
trust.IsPrint = this.cbIsPrint.Checked;
Funs.DB.SubmitChanges();
}
}
}
/// <summary>
/// 是否打印搜索
/// </summary>
protected void rblPrint_SelectedIndexChanged(object sender, EventArgs e)
{
this.InitTreeMenu();
SetTextTemp();
}
/// <summary>
/// 行内按钮事件
/// </summary>
protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
{
if (Grid1.SelectedRowIndexArray.Length == 0)
{
Alert.ShowInTop("最少选中一行!", MessageBoxIcon.Warning);
return;
}
if (e.CommandName == "Delete")
{
string hardProessItemId = Grid1.DataKeys[e.RowIndex][0].ToString();
//删除反馈结果信息
var itemProessItem = Funs.DB.PV_HardProessItem.FirstOrDefault(x => x.HardProessItemId == hardProessItemId);
if (itemProessItem != null)
{
Funs.DB.PV_HardProessItem.DeleteOnSubmit(itemProessItem);
//删除硬度结果反馈表
var proess = Funs.DB.PV_CH_HardProessResult.FirstOrDefault(p => p.HardProessItemId == hardProessItemId);
if (proess != null)
{
Funs.DB.PV_CH_HardProessResult.DeleteOnSubmit(proess);
}
//更新热处理结果反馈表
var hotResult = Funs.DB.PV_CH_HotProessResult.FirstOrDefault(p => p.HotProessTrustItemId == itemProessItem.HotProessTrustItemId);
if (hotResult != null)
{
hotResult.IsNeedHardTest = false;
hotResult.HardProessTrustId = null;
hotResult.CompleteDate = null;
}
Funs.DB.SubmitChanges();
this.Grid1.DataBind();
Alert.ShowInTop("删除成功!", MessageBoxIcon.Success);
}
}
}
#endregion
#region
/// <summary>
/// 硬度委托表打印
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnPrint_Click(object sender, EventArgs e)
{
if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.PV_HardProessTrust, Const.BtnPrint))
{
if (string.IsNullOrWhiteSpace(this.HardProessTrustId))
{
Alert.ShowInTop("请选择委托单号!", MessageBoxIcon.Warning);
return;
}
string rootPath = Server.MapPath("~/") + Const.ExcelUrl;
//导出文件
string filePath = rootPath + DateTime.Now.ToString("yyyyMMddhhmmss") + "\\";
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
string ReportFileName = filePath + "out1.xlsx";
if (Grid1.Rows.Count > 0)
{
int rowIndex = 0;
XSSFWorkbook hssfworkbook = new XSSFWorkbook();
XSSFSheet ws = (XSSFSheet)hssfworkbook.CreateSheet("硬度检测委托单");
#region
ws.SetColumnWidth(0, 9 * 256);
ws.SetColumnWidth(1, 9 * 256);
ws.SetColumnWidth(2, 9 * 256);
ws.SetColumnWidth(3, 9 * 256);
ws.SetColumnWidth(4, 9 * 256);
ws.SetColumnWidth(5, 9 * 256);
ws.SetColumnWidth(6, 9 * 256);
ws.SetColumnWidth(7, 9 * 256);
ws.SetColumnWidth(8, 9 * 256);
#endregion
//获取委托单信息
var trust = (from a in Funs.DB.PV_CH_HardProessTrust
join b in Funs.DB.Base_Unit on a.TrustUnitId equals b.UnitId into temp
join c in Funs.DB.Base_Project on a.ProjectId equals c.ProjectId into tempP
//join d in Funs.DB.Weld_WeldInfo on a.ConstructionNo
from t in temp.DefaultIfEmpty()
from p in tempP.DefaultIfEmpty()
where a.HardProessTrustId == this.HardProessTrustId
select new
{
a.HardProessTrustId,
p.ProjectName,
a.HardProessTrustCode,
t.UnitName,
p.ProjectCode,
a.CreateDate
}).FirstOrDefault();
if (trust == null)
{
Alert.ShowInTop("委托单信息错误!", MessageBoxIcon.Warning);
return;
}
//更新打印状态
var trustModel = Funs.DB.PV_CH_HardProessTrust.FirstOrDefault(p => p.HardProessTrustId == this.HardProessTrustId);
if (trustModel != null)
{
trustModel.IsPrint = true;
Funs.DB.SubmitChanges();
}
//获取委托明细信息
var trustItems = Funs.DB.PV_View_CH_HardProessTrustItem.Where(p => p.HardProessTrustId == this.HardProessTrustId).OrderBy(p => p.WeldingCode).ToList();
var tbNum = trustItems.Count;
CellRangeAddress region;
var pageNum =
tbNum < 15 ? 1
: Math.Ceiling((float)(tbNum - 15) / 15) + 1;
//尾部增加行
var endaddNum = 1;
#region
//头部样式大文字居中文字加粗
ICellStyle titleMaxBoldStyle = CommonPrint.SetExcelStyle(hssfworkbook, BorderStyle.None, BorderStyle.None, BorderStyle.None, BorderStyle.None, VerticalAlignment.Center, HorizontalAlignment.Center, 16, true, true);
//头部样式居中无边框
ICellStyle titNbCenterStyle = CommonPrint.SetExcelStyle(hssfworkbook, BorderStyle.None, BorderStyle.None, BorderStyle.None, BorderStyle.None, VerticalAlignment.Center, HorizontalAlignment.Center, 10, true);
//头部样式靠右无边框
ICellStyle titriNbCenterStyle = CommonPrint.SetExcelStyle(hssfworkbook, BorderStyle.None, BorderStyle.None, BorderStyle.None, BorderStyle.None, VerticalAlignment.Center, HorizontalAlignment.Right, 10, true);
//头部样式靠左无边框
ICellStyle titleNbCenterStyle = CommonPrint.SetExcelStyle(hssfworkbook, BorderStyle.None, BorderStyle.None, BorderStyle.None, BorderStyle.None, VerticalAlignment.Center, HorizontalAlignment.Left, 10, true);
//头部样式靠左上左右边框
ICellStyle titleToStyle = CommonPrint.SetExcelStyle(hssfworkbook, BorderStyle.None, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, VerticalAlignment.Center, HorizontalAlignment.Left, 10, true);
//头部样式靠左下左右边框
ICellStyle titleBoStyle = CommonPrint.SetExcelStyle(hssfworkbook, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.None, VerticalAlignment.Center, HorizontalAlignment.Left, 10, true);
//头部样式靠右下左右边框
ICellStyle titleRiBoStyle = CommonPrint.SetExcelStyle(hssfworkbook, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.None, VerticalAlignment.Center, HorizontalAlignment.Right, 10, true);
//头部样式靠左左右边框
ICellStyle titlelfStyle = CommonPrint.SetExcelStyle(hssfworkbook, BorderStyle.None, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.None, VerticalAlignment.Center, HorizontalAlignment.Left, 10, true);
//头部样式靠右左右边框
ICellStyle titrilfStyle = CommonPrint.SetExcelStyle(hssfworkbook, BorderStyle.None, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.None, VerticalAlignment.Center, HorizontalAlignment.Right, 10, true);
//头部样式居中
ICellStyle titleStyle = CommonPrint.SetExcelStyle(hssfworkbook, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, VerticalAlignment.Center, HorizontalAlignment.Center, 10, true);
//公共样式
ICellStyle style = CommonPrint.SetExcelStyle(hssfworkbook, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, VerticalAlignment.Center, HorizontalAlignment.Center, 10, true);
//页码样式
ICellStyle pageStyle = CommonPrint.SetExcelStyle(hssfworkbook, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, VerticalAlignment.Center, HorizontalAlignment.Center, 7, true);
//文字靠左
ICellStyle leftStyle = CommonPrint.SetExcelStyle(hssfworkbook, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, VerticalAlignment.Center, HorizontalAlignment.Left, 10, true);
#endregion
//循环页
for (int i = 1; i <= pageNum; i++)
{
#region
//行1
ws = ExcelCreateRowTitle(ws, hssfworkbook, titriNbCenterStyle, rowIndex, rowIndex, 0, 8, 30);
region = new CellRangeAddress(rowIndex, rowIndex, 0, 8);
ws.AddMergedRegion(region);
ws.GetRow(rowIndex).GetCell(0).SetCellValue("JA-T6.37.0171.2022");
ws.GetRow(rowIndex).GetCell(0).CellStyle = titriNbCenterStyle;
//行2
ws = ExcelCreateRowTitle(ws, hssfworkbook, titleMaxBoldStyle, rowIndex + 1, rowIndex + 2, 0, 8, 35);
region = new CellRangeAddress(rowIndex + 1, rowIndex + 1, 0, 8);
ws.AddMergedRegion(region);
ws.GetRow(rowIndex + 1).GetCell(0).SetCellValue("镇 海 石 化 建 安 工 程 股 份 有 限 公 司");
ws.GetRow(rowIndex + 1).GetCell(0).CellStyle = titleMaxBoldStyle;
//行3
region = new CellRangeAddress(rowIndex + 2, rowIndex + 2, 0, 8);
ws.AddMergedRegion(region);
ws.GetRow(rowIndex + 2).GetCell(0).SetCellValue("硬 度 检 测 委 托 单");
ws.GetRow(rowIndex + 2).GetCell(0).CellStyle = titleMaxBoldStyle;
//行4
ws = ExcelCreateRowTitle(ws, hssfworkbook, titNbCenterStyle, rowIndex + 3, rowIndex + 3, 0, 8);
region = new CellRangeAddress(rowIndex + 3, rowIndex + 3, 0, 8);
ws.AddMergedRegion(region);
ws.GetRow(rowIndex + 3).GetCell(0).SetCellValue("共 页 第 页");
ws.GetRow(rowIndex + 3).GetCell(0).CellStyle = titNbCenterStyle;
//行5
ws = ExcelCreateRowTitle(ws, hssfworkbook, titleStyle, rowIndex + 4, rowIndex + 6, 0, 8);
region = new CellRangeAddress(rowIndex + 4, rowIndex + 4, 0, 1);
ws.AddMergedRegion(region);
ws.GetRow(rowIndex + 4).GetCell(0).SetCellValue("工程名称");
region = new CellRangeAddress(rowIndex + 4, rowIndex + 4, 2, 4);
ws.AddMergedRegion(region);
ws.GetRow(rowIndex + 4).GetCell(2).SetCellValue(trust.ProjectName);
region = new CellRangeAddress(rowIndex + 4, rowIndex + 4, 5, 6);
ws.AddMergedRegion(region);
ws.GetRow(rowIndex + 4).GetCell(5).SetCellValue("施工号");
region = new CellRangeAddress(rowIndex + 4, rowIndex + 4, 7, 8);
ws.AddMergedRegion(region);
ws.GetRow(rowIndex + 4).GetCell(7).SetCellValue(trust.ProjectCode);
//行6
region = new CellRangeAddress(rowIndex + 5, rowIndex + 5, 0, 1);
ws.AddMergedRegion(region);
ws.GetRow(rowIndex + 5).GetCell(0).SetCellValue("委托单位 ");
region = new CellRangeAddress(rowIndex + 5, rowIndex + 5, 2, 4);
ws.AddMergedRegion(region);
ws.GetRow(rowIndex + 5).GetCell(2).SetCellValue(trust.UnitName);
region = new CellRangeAddress(rowIndex + 5, rowIndex + 5, 5, 6);
ws.AddMergedRegion(region);
ws.GetRow(rowIndex + 5).GetCell(5).SetCellValue("硬度合格标准");
region = new CellRangeAddress(rowIndex + 5, rowIndex + 5, 7, 8);
ws.AddMergedRegion(region);
var hardstandrads = trustItems.GroupBy(p => p.HardQuaStandard);
if (hardstandrads.Count() > 0)
{
ws.GetRow(rowIndex + 5).GetCell(7).SetCellValue(string.Join(",", hardstandrads.Select(s => s.Key)));
}
else
{
ws.GetRow(rowIndex + 5).GetCell(7).SetCellValue("");
}
//行7
region = new CellRangeAddress(rowIndex + 6, rowIndex + 6, 0, 1);
ws.AddMergedRegion(region);
ws.GetRow(rowIndex + 6).GetCell(0).SetCellValue("委托日期 ");
region = new CellRangeAddress(rowIndex + 6, rowIndex + 6, 2, 4);
ws.AddMergedRegion(region);
ws.GetRow(rowIndex + 6).GetCell(2).SetCellValue(trust.CreateDate != null ? trust.CreateDate.Value.ToString("yyyy-MM-dd") : string.Empty);
region = new CellRangeAddress(rowIndex + 6, rowIndex + 6, 5, 6);
ws.AddMergedRegion(region);
ws.GetRow(rowIndex + 6).GetCell(5).SetCellValue("要求完成日期");
region = new CellRangeAddress(rowIndex + 6, rowIndex + 6, 7, 8);
ws.AddMergedRegion(region);
ws.GetRow(rowIndex + 6).GetCell(7).SetCellValue("");
//行8
ws = ExcelCreateRowTitle(ws, hssfworkbook, titleToStyle, rowIndex + 7, rowIndex + 7, 0, 8, 30);
region = new CellRangeAddress(rowIndex + 7, rowIndex + 7, 0, 8);
ws.AddMergedRegion(region);
ws.GetRow(rowIndex + 7).GetCell(0).SetCellValue("下列焊缝已经热处理完毕,请安排硬度检测。");
//行9
ws = ExcelCreateRowTitle(ws, hssfworkbook, titlelfStyle, rowIndex + 8, rowIndex + 8, 0, 8, 30);
region = new CellRangeAddress(rowIndex + 8, rowIndex + 8, 0, 8);
ws.AddMergedRegion(region);
ws.GetRow(rowIndex + 8).GetCell(0).SetCellValue("");
//行10
ws = ExcelCreateRowTitle(ws, hssfworkbook, titleRiBoStyle, rowIndex + 9, rowIndex + 9, 0, 8, 30);
region = new CellRangeAddress(rowIndex + 9, rowIndex + 9, 0, 8);
ws.AddMergedRegion(region);
ws.GetRow(rowIndex + 9).GetCell(0).SetCellValue("热处理单位施工员: 年 月 日");
//行11
ws = ExcelCreateRowTitle(ws, hssfworkbook, titleStyle, rowIndex + 10, rowIndex + 10, 0, 8);
ws.GetRow(rowIndex + 10).GetCell(0).SetCellValue("序号");
region = new CellRangeAddress(rowIndex + 10, rowIndex + 10, 1, 2);
ws.AddMergedRegion(region);
ws.GetRow(rowIndex + 10).GetCell(1).SetCellValue("项目名称或管线号");
ws.GetRow(rowIndex + 10).GetCell(3).SetCellValue("焊缝编号");
ws.GetRow(rowIndex + 10).GetCell(4).SetCellValue("规格");
region = new CellRangeAddress(rowIndex + 10, rowIndex + 10, 5, 6);
ws.AddMergedRegion(region);
ws.GetRow(rowIndex + 10).GetCell(5).SetCellValue("材质");
ws.GetRow(rowIndex + 10).GetCell(7).SetCellValue("合格标准");
ws.GetRow(rowIndex + 10).GetCell(8).SetCellValue("备注");
#endregion
//分页开始和结束
var dStart = i;
var dEnd = 15;
//创建行开始和结束
var tStart = rowIndex + 11;
var tEnd = rowIndex + 25;
#region
//创建数据行和列
ws = ExcelCreateRow(ws, hssfworkbook, tStart, tEnd, style, 0, 8);
//获取当前页数据
if (trustItems.Count > 0)
{
var pageTb = PageList(trustItems, out int total, dStart, dEnd);
int j = 0;
foreach (var item in pageTb)
{
var dataRow = tStart + j;
ws.GetRow(dataRow).GetCell(0).SetCellValue(j + 1);
ws.GetRow(dataRow).GetCell(1).SetCellValue(trust.ProjectName);
ws.GetRow(dataRow).GetCell(3).SetCellValue(item.WeldingCode);
ws.GetRow(dataRow).GetCell(4).SetCellValue(item.JointDesc);
ws.GetRow(dataRow).GetCell(5).SetCellValue(item.STE_Code);
ws.GetRow(dataRow).GetCell(7).SetCellValue("");
ws.GetRow(dataRow).GetCell(8).SetCellValue(item.Remark);
j++;
}
}
#endregion
#region
//尾部行1~3
ws = ExcelCreateRowTitle(ws, hssfworkbook, titlelfStyle, tEnd + 1, tEnd + 3, 0, 8);
region = new CellRangeAddress(tEnd + 1, tEnd + 1, 0, 8);
ws.GetRow(tEnd + 1).GetCell(0).SetCellValue("上述焊缝经硬度检测,以下焊口不合格(其余焊口均合格):");
ws.AddMergedRegion(region);
region = new CellRangeAddress(tEnd + 2, tEnd + 2, 0, 8);
ws.AddMergedRegion(region);
region = new CellRangeAddress(tEnd + 3, tEnd + 3, 0, 8);
ws.AddMergedRegion(region);
//尾部行4
ws = ExcelCreateRowTitle(ws, hssfworkbook, titrilfStyle, tEnd + 4, tEnd + 4, 0, 8);
region = new CellRangeAddress(tEnd + 4, tEnd + 4, 0, 8);
ws.AddMergedRegion(region);
ws.GetRow(tEnd + 4).GetCell(0).SetCellValue("理化试验员: 年 月 日");
//尾部行5
ws = ExcelCreateRowTitle(ws, hssfworkbook, titleBoStyle, tEnd + 5, tEnd + 5, 0, 8);
region = new CellRangeAddress(tEnd + 5, tEnd + 5, 0, 8);
ws.AddMergedRegion(region);
//尾部行9
ws = ExcelCreateRowTitle(ws, hssfworkbook, titriNbCenterStyle, tEnd + 6, tEnd + 6, 0, 8);
region = new CellRangeAddress(tEnd + 6, tEnd + 6, 0, 8);
ws.AddMergedRegion(region);
ws.GetRow(tEnd + 6).GetCell(0).SetCellValue("注:本单一式三份,一份自存,二份交质量技术部");
#endregion
rowIndex = tEnd + 1 + endaddNum;
}
ws.SetMargin(MarginType.LeftMargin, 1);
ws.SetMargin(MarginType.RightMargin, 0.5);
//ws.SetMargin(MarginType.BottomMargin, 1);
//ws.SetMargin(MarginType.TopMargin, 1);
//ws.SetMargin(MarginType.FooterMargin, 0.5);
//ws.SetMargin(MarginType.HeaderMargin, 0.5);
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();
}
}
else
{
ShowNotify("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning);
}
}
#endregion
#region
/// <summary>
/// 数据行和列
/// </summary>
/// <returns></returns>
private XSSFSheet ExcelCreateRow(XSSFSheet ws, XSSFWorkbook hssfworkbook, int sRows, int eRows, ICellStyle style, int cStart, int cEnd)
{
CellRangeAddress region;
for (int i = sRows; i <= eRows; i++)
{
ws.CreateRow(i);
ws.GetRow(i).HeightInPoints = 21.8f;
for (int j = cStart; j <= cEnd; j++)
{
ws.GetRow(i).CreateCell(j);
ws.GetRow(i).GetCell(j).CellStyle = style;
}
region = new CellRangeAddress(i, i, 1, 2);
ws.AddMergedRegion(region);
region = new CellRangeAddress(i, i, 5, 6);
ws.AddMergedRegion(region);
}
return ws;
}
/// <summary>
/// 创建头部
/// </summary>
/// <returns></returns>
private XSSFSheet ExcelCreateRowTitle(XSSFSheet ws, XSSFWorkbook hssfworkbook, ICellStyle style, int sRows, int eRows, int cStart, int cEnd, float height = 21.8f)
{
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 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;
}
/// <summary>
/// 分页
/// </summary>
/// <returns></returns>
public static List<T> PageList<T>(List<T> list, out int total, int pageIndex = 1, int pageSize = 10)
{
total = 0;
pageIndex = pageIndex - 1;
if (list == null || list.Count == 0)
return new List<T>();
total = list.Count;
int startIndex = pageIndex * pageSize;
if (startIndex + pageSize > list.Count)
{
pageSize = list.Count - startIndex;
}
return list.GetRange(startIndex, pageSize);
}
/// <summary>
/// 得到热处理类型
/// </summary>
/// <returns></returns>
protected string ConvertProessTypes(object ProessTypes)
{
string proessTypes = string.Empty;
if (ProessTypes != null)
{
proessTypes = BLL.HJGL_PW_JointInfoService.ConvertProessTypes(ProessTypes.ToString());
}
return proessTypes;
}
#endregion
}
}