using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.IO; using System.Linq; using System.Web; using BLL; using Model; using NPOI.HSSF.Util; using NPOI.SS.UserModel; using NPOI.SS.Util; using NPOI.XSSF.UserModel; namespace FineUIPro.Web.WeldingProcess.PMI { public partial class PMIDelegation : PageBase { #region 定义项 /// /// PMI委托主键 /// public string PMIDelegationId { get { return (string)ViewState["PMIDelegationId"]; } set { ViewState["PMIDelegationId"] = value; } } #endregion #region 加载页面 /// /// 加载页面 /// /// /// protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { this.ddlPageSize.SelectedValue = this.Grid1.PageSize.ToString(); this.PMIDelegationId = string.Empty; this.InitTreeMenu();//加载树 } } #endregion #region 加载树 /// /// 加载树 /// private void InitTreeMenu() { string projectName = string.Empty; var pro = BLL.Base_ProjectService.GetProjectByProjectId(this.CurrUser.LoginProjectId); if (pro != null) { this.tvControlItem.Nodes.Clear(); TreeNode rootNode = new TreeNode(); rootNode.Text = "[" + pro.ProjectCode + "]" + pro.ProjectName; rootNode.NodeID = "0"; rootNode.Expanded = true; rootNode.EnableClickEvent = true; this.tvControlItem.Nodes.Add(rootNode); List trustLists = new List(); ///PMI委托单 if (!string.IsNullOrEmpty(this.txtSearchNo.Text.Trim())) { trustLists = (from x in Funs.DB.PMI_Delegation where x.ProjectId == CurrUser.LoginProjectId && x.DelegationNo.Contains(this.txtSearchNo.Text.Trim()) orderby x.DelegationNo select x).ToList(); } else { trustLists = (from x in Funs.DB.PMI_Delegation where x.ProjectId == CurrUser.LoginProjectId orderby x.DelegationNo select x).ToList(); } this.BindNodes(rootNode, trustLists); } } #endregion #region 绑定树节点 /// /// 绑定树节点 /// /// private void BindNodes(TreeNode node, List trustList) { foreach (var item in trustList) { TreeNode newNode = new TreeNode(); newNode.Text = item.DelegationNo; newNode.NodeID = item.Id; newNode.ToolTip = item.DelegationNo; newNode.CommandName = Resources.Lan.RequestSheetNumber; newNode.EnableClickEvent = true; node.Nodes.Add(newNode); } } #endregion #region 点击TreeView /// /// 点击TreeView /// /// /// protected void tvControlItem_NodeCommand(object sender, TreeCommandEventArgs e) { if (this.tvControlItem.SelectedNodeID != "0") { this.PMIDelegationId = tvControlItem.SelectedNodeID; this.BindGrid(); } } #endregion #region DropDownList下拉选择事件 /// /// 项目下拉选择事件 /// /// /// protected void drpProjectId_SelectedIndexChanged(object sender, EventArgs e) { this.InitTreeMenu(); } #endregion #region 数据绑定 /// /// 数据绑定 /// private void BindGrid() { string strSql = string.Empty; List listStr = new List(); this.SetTextTemp(); this.PageInfoLoad(); ///页面输入提交信息 if (!string.IsNullOrEmpty(this.tvControlItem.SelectedNodeID)) { var result = Funs.DB.PMI_Delegation.FirstOrDefault(t => t.Id == this.tvControlItem.SelectedNodeID); if (result != null) { this.PMIDelegationId = result.Id; strSql = @"SELECT *FROM [View_PMI_DelegationDetails] WHERE ProjectId= @ProjectId AND PMIID=@PMIDelegationId "; listStr.Add(new SqlParameter("@ProjectId", result != null ? result.ProjectId : this.CurrUser.LoginProjectId)); listStr.Add(new SqlParameter("@PMIDelegationId", this.PMIDelegationId)); if (!string.IsNullOrEmpty(this.txtIsoNo.Text.Trim())) { strSql += @" and PipelineCode like '%'+@PipelineCode+'%' "; listStr.Add(new SqlParameter("@PipelineCode", this.txtIsoNo.Text.Trim())); } SqlParameter[] parameter = listStr.ToArray(); DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter); // 2.获取当前分页数据 //var table = this.GetPagedDataTable(Grid1, tb1); Grid1.RecordCount = tb.Rows.Count; //tb = GetFilteredTable(Grid1.FilteredData, tb); var table = this.GetPagedDataTable(Grid1, tb); Grid1.DataSource = table; Grid1.DataBind(); } } } /// /// 查询 /// /// /// protected void TextBox_TextChanged(object sender, EventArgs e) { this.BindGrid(); } #region 加载页面输入提交信息 /// /// 加载页面输入提交信息 /// private void PageInfoLoad() { var trust = Funs.DB.PMI_Delegation.FirstOrDefault(t => t.Id == this.PMIDelegationId); if (trust != null) { this.txtDelegationNo.Text = trust.DelegationNo; if (trust.DelegationDate.HasValue) { this.txtDelegationDate.Text = string.Format("{0:yyyy-MM-dd}", trust.DelegationDate); } if (!string.IsNullOrEmpty(trust.InstallationId)) { this.txtInstallationName.Text = BLL.Project_InstallationService.GetProject_InstallationNameByInstallationId(trust.InstallationId); } if (!string.IsNullOrEmpty(trust.UnitId)) { this.txtUnitName.Text = BLL.Base_UnitService.GetUnitNameByUnitId(trust.UnitId); } this.txtDetectionMethod.Text = "PMI光谱分析"; this.txtDetectionStandard.Text = trust.DetectionStandard; if (!string.IsNullOrEmpty(trust.Tabler)) { this.txtTabler.Text = BLL.Sys_UserService.GetUserNameByUserId(trust.Tabler); } this.txtRemark.Text = trust.Remark; } } #endregion #region 清空文本 /// /// 清空文本 /// private void SetTextTemp() { this.txtDelegationNo.Text = string.Empty; this.txtDelegationDate.Text = string.Empty; this.txtInstallationName.Text = string.Empty; this.txtUnitName.Text = string.Empty; this.txtDetectionMethod.Text = string.Empty; this.txtDetectionStandard.Text = string.Empty; this.txtTabler.Text = string.Empty; this.txtRemark.Text = string.Empty; } #endregion #endregion #region 分页排序 #region 页索引改变事件 /// /// 页索引改变事件 /// /// /// protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e) { BindGrid(); } #endregion #region 排序 /// /// 排序 /// /// /// protected void Grid1_Sort(object sender, GridSortEventArgs e) { BindGrid(); } #endregion #region 分页选择下拉改变事件 /// /// 分页选择下拉改变事件 /// /// /// protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e) { Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue); BindGrid(); } #endregion #endregion #region PMI委托 维护事件 /// /// 增加PMI委托 /// /// /// protected void btnNew_Click(object sender, EventArgs e) { if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.PMIDelegationMenuId, Const.BtnAdd)) { this.SetTextTemp(); string window = String.Format("PMIDelegationEdit.aspx?PMIDelegationId={0}", string.Empty, "新增 - "); PageContext.RegisterStartupScript(Window2.GetSaveStateReference(this.hdPMIDelegationId.ClientID) + Window2.GetShowReference(window)); } else { ShowNotify(Resources.Lan.NoPrivilegePrompt, MessageBoxIcon.Warning); } } #region 编辑PMI委托 /// /// 编辑PMI委托 /// /// /// protected void btnEdit_Click(object sender, EventArgs e) { if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.HotProessTrustMenuId, Const.BtnSave)) { var trustManage = Funs.DB.PMI_Delegation.FirstOrDefault(t => t.Id == this.PMIDelegationId); if (trustManage != null) { string openUrl = String.Format("PMIDelegationEdit.aspx?PMIDelegationId={0}", this.PMIDelegationId, "编辑 - "); PageContext.RegisterStartupScript(Window2.GetSaveStateReference(this.hdPMIDelegationId.ClientID) + Window2.GetShowReference(openUrl)); } else { ShowNotify("请选择要处理的PMI委托", MessageBoxIcon.Warning); } } else { ShowNotify(Resources.Lan.NoPrivilegePrompt, MessageBoxIcon.Warning); } } #endregion #region 删除PMI委托 /// /// 删除PMI委托 /// /// /// protected void btnDelete_Click(object sender, EventArgs e) { if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.PMIDelegationMenuId, Const.BtnDelete)) { var trustManage = Funs.DB.PMI_Delegation.FirstOrDefault(t => t.Id == this.PMIDelegationId); if (trustManage != null) { //删除主表数据 Funs.DB.PMI_Delegation.DeleteOnSubmit(trustManage); //删除 明细表 var details = Funs.DB.PMI_DelegationDetails.Where(t => t.PMIId == trustManage.Id).ToList(); Funs.DB.PMI_DelegationDetails.DeleteAllOnSubmit(details); Funs.DB.SubmitChanges(); Alert.ShowInTop(Resources.Lan.DeletedSuccessfully, MessageBoxIcon.Success); this.InitTreeMenu(); SetTextTemp(); this.Grid1.DataSource = null; this.Grid1.DataBind(); } else { ShowNotify(Resources.Lan.PleaseSelectDeleteHotProessTrust, MessageBoxIcon.Warning); } } else { ShowNotify(Resources.Lan.NoPrivilegePrompt, MessageBoxIcon.Warning); return; } } #endregion #endregion #region 关闭弹出窗口及刷新页面 /// /// 关闭弹出窗口 /// /// /// protected void Window2_Close(object sender, WindowCloseEventArgs e) { this.InitTreeMenu(); this.BindGrid(); } #region 查询 /// /// 查询 /// /// /// protected void Tree_TextChanged(object sender, EventArgs e) { this.InitTreeMenu(); } #endregion #endregion #region 获取按钮权限 /// /// 获取按钮权限 /// /// /// private bool GetButtonPower(string button) { return BLL.CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.DailyReportCompleteMenuId, button); } #endregion /// /// 打印 /// protected void btnExport_Click(object sender, EventArgs e) { if (string.IsNullOrWhiteSpace(this.tvControlItem.SelectedNodeID)) { 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 + "out.xlsx"; int rowIndex = 0; XSSFWorkbook hssfworkbook = new XSSFWorkbook(); XSSFSheet ws = (XSSFSheet)hssfworkbook.CreateSheet("无损检测委托单(PMI)"); #region 列宽 ws.SetColumnWidth(0, 6 * 256); ws.SetColumnWidth(1, 4 * 256); ws.SetColumnWidth(2, 6 * 256); ws.SetColumnWidth(3, 6 * 256); ws.SetColumnWidth(4, 6 * 256); ws.SetColumnWidth(5, 6 * 256); ws.SetColumnWidth(6, 9 * 256); ws.SetColumnWidth(7, 9 * 256); ws.SetColumnWidth(8, 5 * 256); ws.SetColumnWidth(9, 5 * 256); ws.SetColumnWidth(10, 5 * 256); ws.SetColumnWidth(11, 8 * 256); ws.SetColumnWidth(12, 9 * 256); ws.SetColumnWidth(13, 11 * 256); #endregion string pmiId = this.tvControlItem.SelectedNodeID; //头部 var pmititle = Funs.DB.PMI_Delegation.FirstOrDefault(x => x.Id == pmiId); //列表 List listStr = new List(); listStr.Add(new SqlParameter("@ProjectId", pmititle != null ? pmititle.ProjectId : this.CurrUser.LoginProjectId)); listStr.Add(new SqlParameter("@PMIID", this.PMIDelegationId)); SqlParameter[] parameter = listStr.ToArray(); var tb = SQLHelper.GetDataTableRunText("SELECT * FROM View_PMI_DelegationDetails WHERE ProjectId=@ProjectId AND PMIID=@PMIID", parameter); //模拟多页数据sql //var tb = SQLHelper.GetDataTableRunText("SELECT * FROM View_PMI_DelegationDetails WHERE ProjectId=@ProjectId AND PMIID=@PMIID union all SELECT * FROM View_PMI_DelegationDetails WHERE ProjectId=@ProjectId AND PMIID=@PMIID union all SELECT * FROM View_PMI_DelegationDetails WHERE ProjectId=@ProjectId AND PMIID=@PMIID union all SELECT * FROM View_PMI_DelegationDetails WHERE ProjectId=@ProjectId AND PMIID=@PMIID union all SELECT * FROM View_PMI_DelegationDetails WHERE ProjectId=@ProjectId AND PMIID=@PMIID union all SELECT * FROM View_PMI_DelegationDetails WHERE ProjectId=@ProjectId AND PMIID=@PMIID union all SELECT * FROM View_PMI_DelegationDetails WHERE ProjectId=@ProjectId AND PMIID=@PMIID union all SELECT * FROM View_PMI_DelegationDetails WHERE ProjectId=@ProjectId AND PMIID=@PMIID union all SELECT * FROM View_PMI_DelegationDetails WHERE ProjectId=@ProjectId AND PMIID=@PMIID union all SELECT * FROM View_PMI_DelegationDetails WHERE ProjectId=@ProjectId AND PMIID=@PMIID union all SELECT * FROM View_PMI_DelegationDetails WHERE ProjectId=@ProjectId AND PMIID=@PMIID union all SELECT * FROM View_PMI_DelegationDetails WHERE ProjectId=@ProjectId AND PMIID=@PMIID union all SELECT * FROM View_PMI_DelegationDetails WHERE ProjectId=@ProjectId AND PMIID=@PMIID union all SELECT * FROM View_PMI_DelegationDetails WHERE ProjectId=@ProjectId AND PMIID=@PMIID union all SELECT * FROM View_PMI_DelegationDetails WHERE ProjectId=@ProjectId AND PMIID=@PMIID union all SELECT * FROM View_PMI_DelegationDetails WHERE ProjectId=@ProjectId AND PMIID=@PMIID union all SELECT * FROM View_PMI_DelegationDetails WHERE ProjectId=@ProjectId AND PMIID=@PMIID union all SELECT * FROM View_PMI_DelegationDetails WHERE ProjectId=@ProjectId AND PMIID=@PMIID union all SELECT * FROM View_PMI_DelegationDetails WHERE ProjectId=@ProjectId AND PMIID=@PMIID union all SELECT * FROM View_PMI_DelegationDetails WHERE ProjectId=@ProjectId AND PMIID=@PMIID union all SELECT * FROM View_PMI_DelegationDetails WHERE ProjectId=@ProjectId AND PMIID=@PMIID union all SELECT * FROM View_PMI_DelegationDetails WHERE ProjectId=@ProjectId AND PMIID=@PMIID union all SELECT * FROM View_PMI_DelegationDetails WHERE ProjectId=@ProjectId AND PMIID=@PMIID union all SELECT * FROM View_PMI_DelegationDetails WHERE ProjectId=@ProjectId AND PMIID=@PMIID union all SELECT * FROM View_PMI_DelegationDetails WHERE ProjectId=@ProjectId AND PMIID=@PMIID", parameter); var style = CommonPrint.SetExcelStyle(hssfworkbook, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, VerticalAlignment.Center, HorizontalAlignment.Center, 10, true, false); var styleButton = CommonPrint.SetExcelStyle(hssfworkbook, BorderStyle.None, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, VerticalAlignment.Center, HorizontalAlignment.Center, 10, true, false); var styleTop = CommonPrint.SetExcelStyle(hssfworkbook, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.None, VerticalAlignment.Center, HorizontalAlignment.Center, 10, true, false); var styleNone = CommonPrint.SetExcelStyle(hssfworkbook, BorderStyle.None, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.None, VerticalAlignment.Center, HorizontalAlignment.Center, 10, true, false); if (tb.Rows.Count > 0 && pmititle != null) { var tbNum = tb.Rows.Count; var pageNum = tbNum < 15 ? 1 : Math.Ceiling((float)(tbNum - 14) / 14) + 1; //循环页 for (int i = 1; i <= pageNum; i++) { #region 头部 ws = WsExcelCreateRow(ws, hssfworkbook, rowIndex, rowIndex + 12, style, 0, 13, 1); //行0 CellRangeAddress region = new CellRangeAddress(rowIndex, rowIndex + 3, 0, 3); ws.AddMergedRegion(region); ws.GetRow(rowIndex).GetCell(0).SetCellValue("NDE-002"); ws.GetRow(rowIndex).GetCell(0).CellStyle = style; ws.GetRow(rowIndex).GetCell(1).CellStyle = style; ws.GetRow(rowIndex).GetCell(2).CellStyle = style; ws.GetRow(rowIndex).GetCell(3).CellStyle = style; region = new CellRangeAddress(rowIndex, rowIndex + 3, 4, 10); ws.AddMergedRegion(region); ws.GetRow(rowIndex).GetCell(4).SetCellValue("无损检测委托单\r\nPMI"); ws.GetRow(rowIndex).GetCell(4).CellStyle = CommonPrint.SetExcelStyle(hssfworkbook, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, VerticalAlignment.Center, HorizontalAlignment.Center, 16, true, true); ws.GetRow(rowIndex).GetCell(5).CellStyle = style; ws.GetRow(rowIndex).GetCell(6).CellStyle = style; ws.GetRow(rowIndex).GetCell(7).CellStyle = style; ws.GetRow(rowIndex).GetCell(8).CellStyle = style; ws.GetRow(rowIndex).GetCell(9).CellStyle = style; ws.GetRow(rowIndex).GetCell(10).CellStyle = style; region = new CellRangeAddress(rowIndex, rowIndex, 11, 13); ws.AddMergedRegion(region); ws.GetRow(rowIndex).GetCell(11).SetCellValue("工程名称:巴斯夫(广东)一体化项目专用化学品二区"); ws.GetRow(rowIndex).GetCell(11).CellStyle = CommonPrint.SetExcelStyle(hssfworkbook, BorderStyle.None, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, VerticalAlignment.Center, HorizontalAlignment.Justify, 10.5, true, false); ws.GetRow(rowIndex).GetCell(12).CellStyle = styleButton; ws.GetRow(rowIndex).GetCell(13).CellStyle = styleButton; //行1 region = new CellRangeAddress(rowIndex + 1, rowIndex + 1, 11, 13); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 1).GetCell(11).SetCellValue("BASF (Guangdong) Integrated Project Citral Cluster"); ws.GetRow(rowIndex + 1).GetCell(11).CellStyle = CommonPrint.SetExcelStyle(hssfworkbook, BorderStyle.None, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.None, VerticalAlignment.Center, HorizontalAlignment.Justify, 10.5, true, false); ws.GetRow(rowIndex + 1).GetCell(12).CellStyle = styleNone; ws.GetRow(rowIndex + 1).GetCell(13).CellStyle = styleNone; //行2 region = new CellRangeAddress(rowIndex + 2, rowIndex + 2, 11, 13); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 2).GetCell(11).SetCellValue("单元名称:"); ws.GetRow(rowIndex + 2).GetCell(11).CellStyle = CommonPrint.SetExcelStyle(hssfworkbook, BorderStyle.None, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.None, VerticalAlignment.Center, HorizontalAlignment.Justify, 10.5, true, false); ws.GetRow(rowIndex + 2).GetCell(12).CellStyle = styleNone; ws.GetRow(rowIndex + 2).GetCell(13).CellStyle = styleNone; //行3 region = new CellRangeAddress(rowIndex + 3, rowIndex + 3, 11, 13); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 3).GetCell(11).SetCellValue("Unit Name:"); ws.GetRow(rowIndex + 3).GetCell(11).CellStyle = CommonPrint.SetExcelStyle(hssfworkbook, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.None, VerticalAlignment.Center, HorizontalAlignment.Justify, 10.5, true, false); ws.GetRow(rowIndex + 3).GetCell(12).CellStyle = styleTop; ws.GetRow(rowIndex + 3).GetCell(13).CellStyle = styleTop; //行4 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, 6); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 4).GetCell(2).SetCellValue(""); region = new CellRangeAddress(rowIndex + 4, rowIndex + 4, 7, 8); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 4).GetCell(7).SetCellValue("委托编号"); region = new CellRangeAddress(rowIndex + 4, rowIndex + 4, 9, 13); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 4).GetCell(9).SetCellValue(""); //行5 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, 6); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 5).GetCell(2).SetCellValue(""); region = new CellRangeAddress(rowIndex + 5, rowIndex + 5, 7, 8); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 5).GetCell(7).SetCellValue("检测方法"); region = new CellRangeAddress(rowIndex + 5, rowIndex + 5, 9, 13); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 5).GetCell(9).SetCellValue(""); //行6 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, 6); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 6).GetCell(2).SetCellValue(""); region = new CellRangeAddress(rowIndex + 6, rowIndex + 6, 7, 8); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 6).GetCell(7).SetCellValue("表面状态"); region = new CellRangeAddress(rowIndex + 6, rowIndex + 6, 9, 13); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 6).GetCell(9).SetCellValue(""); //行7 region = new CellRangeAddress(rowIndex + 7, rowIndex + 7, 0, 1); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 7).GetCell(0).SetCellValue("检测标准"); region = new CellRangeAddress(rowIndex + 7, rowIndex + 7, 2, 6); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 7).GetCell(2).SetCellValue(""); region = new CellRangeAddress(rowIndex + 7, rowIndex + 7, 7, 8); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 7).GetCell(7).SetCellValue("检测比例"); region = new CellRangeAddress(rowIndex + 7, rowIndex + 7, 9, 13); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 7).GetCell(9).SetCellValue(""); //行8 region = new CellRangeAddress(rowIndex + 8, rowIndex + 8, 0, 1); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 8).GetCell(0).SetCellValue("转动口/固定口"); region = new CellRangeAddress(rowIndex + 8, rowIndex + 8, 2, 6); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 8).GetCell(2).SetCellValue(""); region = new CellRangeAddress(rowIndex + 8, rowIndex + 8, 7, 8); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 8).GetCell(7).SetCellValue("委托日期"); region = new CellRangeAddress(rowIndex + 8, rowIndex + 8, 9, 13); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 8).GetCell(9).SetCellValue(""); //行9 region = new CellRangeAddress(rowIndex + 9, rowIndex + 9, 0, 1); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 9).GetCell(0).SetCellValue("焊接方法"); region = new CellRangeAddress(rowIndex + 9, rowIndex + 9, 2, 6); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 9).GetCell(2).SetCellValue(""); region = new CellRangeAddress(rowIndex + 9, rowIndex + 9, 7, 8); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 9).GetCell(7).SetCellValue("压力管道分级"); region = new CellRangeAddress(rowIndex + 9, rowIndex + 9, 9, 13); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 9).GetCell(9).SetCellValue(""); //行10 region = new CellRangeAddress(rowIndex + 10, rowIndex + 10, 0, 1); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 10).GetCell(0).SetCellValue("检测时机"); region = new CellRangeAddress(rowIndex + 10, rowIndex + 10, 2, 6); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 10).GetCell(2).SetCellValue(""); region = new CellRangeAddress(rowIndex + 10, rowIndex + 10, 7, 8); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 10).GetCell(7).SetCellValue("被检产品标准"); region = new CellRangeAddress(rowIndex + 10, rowIndex + 10, 9, 13); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 10).GetCell(9).SetCellValue(""); //行12 region = new CellRangeAddress(rowIndex + 11, rowIndex + 11, 0, 2); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 11).GetCell(0).SetCellValue("检件编号/管线号"); region = new CellRangeAddress(rowIndex + 11, rowIndex + 11, 3, 4); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 11).GetCell(3).SetCellValue(""); region = new CellRangeAddress(rowIndex + 11, rowIndex + 11, 5, 6); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 11).GetCell(5).SetCellValue("焊口总数"); region = new CellRangeAddress(rowIndex + 11, rowIndex + 11, 7, 9); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 11).GetCell(7).SetCellValue(""); region = new CellRangeAddress(rowIndex + 11, rowIndex + 11, 10, 11); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 11).GetCell(10).SetCellValue("抽检数量"); region = new CellRangeAddress(rowIndex + 11, rowIndex + 11, 12, 13); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 11).GetCell(12).SetCellValue(""); //行13 ws.GetRow(rowIndex + 12).GetCell(0).SetCellValue("检测\r\n部位"); region = new CellRangeAddress(rowIndex + 12, rowIndex + 12, 1, 3); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 12).GetCell(1).SetCellValue("检件编号/管线号"); ws.GetRow(rowIndex + 12).GetCell(4).SetCellValue("焊口号"); region = new CellRangeAddress(rowIndex + 12, rowIndex + 12, 5, 6); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 12).GetCell(5).SetCellValue("焊工代号"); region = new CellRangeAddress(rowIndex + 12, rowIndex + 12, 7, 10); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 12).GetCell(7).SetCellValue("检件材质"); region = new CellRangeAddress(rowIndex + 12, rowIndex + 12, 11, 12); ws.AddMergedRegion(region); ws.GetRow(rowIndex + 12).GetCell(11).SetCellValue("检件规格(mm)"); ws.GetRow(rowIndex + 12).GetCell(13).SetCellValue("备注"); #endregion #region 表格 ws = WsExcelCreateRow(ws, hssfworkbook, rowIndex + 13, rowIndex + 26, style, 0, 13, 2); var dataTit = rowIndex + 13; var tIndex = 12 + 14; var dStart = 0; var dEnd = 0; if (i == 1) { dStart = 0; dEnd = 14; } else { dStart = i == 2 ? 14 : ((i - 2) * 14) + 14; dEnd = ((i - 1) * 14) + 14; } //合并单元格 for (int hb = dataTit; hb <= rowIndex + tIndex; hb++) { region = new CellRangeAddress(hb, hb, 1, 3); ws.AddMergedRegion(region); region = new CellRangeAddress(hb, hb, 5, 6); ws.AddMergedRegion(region); region = new CellRangeAddress(hb, hb, 7, 10); ws.AddMergedRegion(region); region = new CellRangeAddress(hb, hb, 11, 12); ws.AddMergedRegion(region); } //获取当前页数据 var pageTb = GetPageToTable(tb, dStart, dEnd); for (int j = 0; j < pageTb.Rows.Count; j++) { int dataIndex = dataTit + j; //检测部位 ws.GetRow(dataIndex).GetCell(0).SetCellValue(""); //检件编号/管线号 ws.GetRow(dataIndex).GetCell(1).SetCellValue("数据待绑定"); //焊口号 ws.GetRow(dataIndex).GetCell(4).SetCellValue(""); //焊工代号 ws.GetRow(dataIndex).GetCell(5).SetCellValue(""); //检件材质 ws.GetRow(dataIndex).GetCell(7).SetCellValue(""); //检件规格(mm) ws.GetRow(dataIndex).GetCell(11).SetCellValue(""); //备注 ws.GetRow(dataIndex).GetCell(13).SetCellValue(""); } if (dataTit + pageTb.Rows.Count < 26) { ws.GetRow(dataTit + pageTb.Rows.Count).GetCell(0).SetCellValue("以下空白"); } rowIndex += tIndex; #endregion #region 尾部 ws = WsExcelCreateRow(ws, hssfworkbook, rowIndex + 1, rowIndex + 9, style, 0, 13, 3); //合并单元格 for (int hb = rowIndex + 1; hb <= rowIndex + 9; hb++) { for (int c = 0; c <= 13; c++) { if (hb >= rowIndex + 2 && hb <= rowIndex + 3) { ws.GetRow(hb).GetCell(c).CellStyle = CommonPrint.SetExcelStyle(hssfworkbook, BorderStyle.None, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.None, VerticalAlignment.Center, HorizontalAlignment.Left, 10.5, true, false); } if (hb >= rowIndex + 4 && hb <= rowIndex + 8) { ws.GetRow(hb).GetCell(c).CellStyle = CommonPrint.SetExcelStyle(hssfworkbook, BorderStyle.None, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.None, VerticalAlignment.Center, HorizontalAlignment.Right, 10.5, true, false); } if (hb == rowIndex + 9) { ws.GetRow(hb).GetCell(c).CellStyle = CommonPrint.SetExcelStyle(hssfworkbook, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.None, VerticalAlignment.Center, HorizontalAlignment.Center, 10.5, true, false); } } region = new CellRangeAddress(hb, hb, 0, 2); ws.AddMergedRegion(region); region = new CellRangeAddress(hb, hb, 3, 5); ws.AddMergedRegion(region); region = new CellRangeAddress(hb, hb, 6, 7); ws.AddMergedRegion(region); region = new CellRangeAddress(hb, hb, 8, 11); ws.AddMergedRegion(region); region = new CellRangeAddress(hb, hb, 12, 13); ws.AddMergedRegion(region); } //尾部行1 ws.GetRow(rowIndex + 1).GetCell(0).SetCellValue("施工单位"); ws.GetRow(rowIndex + 1).GetCell(3).SetCellValue("监理单位"); ws.GetRow(rowIndex + 1).GetCell(6).SetCellValue("管理公司"); ws.GetRow(rowIndex + 1).GetCell(8).SetCellValue("建设单位"); ws.GetRow(rowIndex + 1).GetCell(12).SetCellValue("检测单位"); //尾部行2 ws.GetRow(rowIndex + 2).GetCell(0).SetCellValue(""); ws.GetRow(rowIndex + 2).GetCell(3).SetCellValue(""); ws.GetRow(rowIndex + 2).GetCell(6).SetCellValue(""); ws.GetRow(rowIndex + 2).GetCell(8).SetCellValue(""); ws.GetRow(rowIndex + 2).GetCell(12).SetCellValue(""); //尾部行3 ws.GetRow(rowIndex + 3).GetCell(0).SetCellValue("质量检测员:"); ws.GetRow(rowIndex + 3).GetCell(3).SetCellValue("专业工程师:"); ws.GetRow(rowIndex + 3).GetCell(6).SetCellValue("专业/质量工程师:"); ws.GetRow(rowIndex + 3).GetCell(8).SetCellValue("业主代表:"); ws.GetRow(rowIndex + 3).GetCell(12).SetCellValue("接收人:"); //尾部行6 ws.GetRow(rowIndex + 6).GetCell(0).SetCellValue("专业工程师:"); //尾部行8 ws.GetRow(rowIndex + 8).GetCell(0).SetCellValue(" 年 月 日"); ws.GetRow(rowIndex + 8).GetCell(3).SetCellValue(" 年 月 日"); ws.GetRow(rowIndex + 8).GetCell(6).SetCellValue(" 年 月 日"); ws.GetRow(rowIndex + 8).GetCell(8).SetCellValue(" 年 月 日"); ws.GetRow(rowIndex + 8).GetCell(12).SetCellValue(" 年 月 日"); #endregion ws = WsExcelCreateRow(ws, hssfworkbook, rowIndex + 10, rowIndex + 10, style, 0, 13, 3, true); rowIndex += 11; } } ws.SetMargin(MarginType.LeftMargin, 0.3); ws.SetMargin(MarginType.RightMargin, 0.2); ws.SetMargin(MarginType.BottomMargin, 0.4); ws.PrintSetup.Landscape = false; ws.PrintSetup.PaperSize = 9; ws.ForceFormulaRecalculation = true; using (FileStream filess = System.IO.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("无损检测委托单(PMI).xlsx")); // 添加头信息,指定文件大小,让浏览器能够显示下载进度 Response.AddHeader("Content-Length", filet.Length.ToString()); // 指定返回的是一个不能被客户端读取的流,必须被下载 Response.ContentType = "application/ms-excel"; // 把文件流发送到客户端 Response.WriteFile(filet.FullName); // 停止页面的执行 Response.End(); } #region 私有方法 /// /// 无损检测委托单行和列 /// /// private XSSFSheet WsExcelCreateRow(XSSFSheet ws, XSSFWorkbook hssfworkbook, int sRows, int eRows, ICellStyle style, int cStart, int cEnd, int excelpart, bool isnobk = false) { for (int i = sRows; i <= eRows; i++) { ws.CreateRow(i); if (excelpart == 1) { ws.GetRow(i).HeightInPoints = i == sRows ? 27.75f : i == (sRows + 1) ? 25.5f : i == (sRows + 2) ? 25.5f : i == (sRows + 3) ? 25.5f : i == (sRows + 4) ? 23.25f : i == (sRows + 5) ? 23.25f : i == (sRows + 6) ? 23.25f : i == (sRows + 7) ? 23.25f : i == (sRows + 8) ? 23.25f : i == (sRows + 9) ? 23.25f : i == (sRows + 10) ? 23.25f : i == (sRows + 11) ? 23.25f : i == (sRows + 12) ? 23.25f : 17f; } else if (excelpart == 2) { ws.GetRow(i).HeightInPoints = 25.5f; } else { ws.GetRow(i).HeightInPoints = 15.5f; } for (int j = cStart; j <= cEnd; j++) { ws.GetRow(i).CreateCell(j); if (isnobk) { ws.GetRow(i).GetCell(j).CellStyle = CommonPrint.SetExcelStyle(hssfworkbook, BorderStyle.None, BorderStyle.None, BorderStyle.None, BorderStyle.None, VerticalAlignment.Center, HorizontalAlignment.Center, 10.5, true, false); } else { ws.GetRow(i).GetCell(j).CellStyle = style; } } } return ws; } /// /// 行和列 /// /// private XSSFSheet ClExcelCreateRow(XSSFSheet ws, XSSFWorkbook hssfworkbook, int sRows, int eRows, ICellStyle style, int cStart, int cEnd, bool istitle = false, bool isnobk = false) { for (int i = sRows; i <= eRows; i++) { ws.CreateRow(i); if (istitle) { ws.GetRow(i).HeightInPoints = i == sRows ? 27.75f : i == (sRows + 1) ? 25f : i == (sRows + 2) ? 21.75f : i == (sRows + 3) ? 21.75f : i == (sRows + 5) ? 28f : 14.75f; } else { ws.GetRow(i).HeightInPoints = 14.75f; } for (int j = cStart; j <= cEnd; j++) { ws.GetRow(i).CreateCell(j); if (isnobk) { ws.GetRow(i).GetCell(j).CellStyle = CommonPrint.SetExcelStyle(hssfworkbook, BorderStyle.None, BorderStyle.None, BorderStyle.None, BorderStyle.None, VerticalAlignment.Center, HorizontalAlignment.Center, 10, true, false); } else { ws.GetRow(i).GetCell(j).CellStyle = style; } } } return ws; } /// /// 查询指定条数分页 /// /// 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; } #endregion } }