using BLL; using NPOI.SS.Formula.Functions; using NPOI.SS.UserModel; using NPOI.SS.Util; using NPOI.XSSF.UserModel; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.IO; using System.Linq; namespace FineUIPro.Web.WeldingProcess.WeldingReport { public partial class ProPointWeldList : PageBase { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindGrid(); } } private void BindGrid() { string strSql = @"select * from View_Pre_PointWeldList where ProjectId=@ProjectId"; List listStr = new List(); listStr.Add(new SqlParameter("@ProjectId", CurrUser.LoginProjectId)); if (!string.IsNullOrEmpty(txtTrustBatchCode.Text)) { strSql += " AND TrustBatchCode LIKE @TrustBatchCode"; listStr.Add(new SqlParameter("@TrustBatchCode", "%" + txtTrustBatchCode.Text.Trim() + "%")); } if (!string.IsNullOrEmpty(txtIsoNo.Text)) { strSql += " AND PipelineCode LIKE @PipelineCode"; listStr.Add(new SqlParameter("@PipelineCode", "%" + txtIsoNo.Text.Trim() + "%")); } strSql += " ORDER BY TrustBatchCode, PipelineCode,WeldJointCode, PrePipelineCode,PreWeldJointCode"; SqlParameter[] parameter = listStr.ToArray(); DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter); Grid1.RecordCount = tb.Rows.Count; var table = this.GetPagedDataTable(Grid1, tb); Grid1.DataSource = table; Grid1.DataBind(); } //private void BindGrid2(string pointBatchItemId) //{ // var point=BLL.Batch_PointBatchItemService.GetPointBatchItemByPointBatchItemId(pointBatchItemId); // string strSql = @"SELECT pipe.PipelineCode,jot.WeldJointCode // FROM dbo.Batch_PointBatchItem batchItem // LEFT JOIN dbo.Pipeline_WeldJoint jot ON jot.WeldJointId = batchItem.WeldJointId // LEFT JOIN dbo.Pipeline_Pipeline pipe ON pipe.PipelineId = jot.PipelineId //WHERE batchItem.PointState IS NULL // AND (jot.IsCancel=0 OR jot.IsCancel is null) // AND batchItem.CutDate IS NULL"; // List listStr = new List(); // if (point!=null && !string.IsNullOrEmpty(point.PointBatchId)) // { // strSql += " AND batchItem.PointBatchId = @PointBatchId"; // listStr.Add(new SqlParameter("@PointBatchId", point.PointBatchId)); // } // strSql += " ORDER BY pipe.PipelineCode,jot.WeldJointCode"; // SqlParameter[] parameter = listStr.ToArray(); // DataTable dt = SQLHelper.GetDataTableRunText(strSql, parameter); // Grid2.DataSource = dt; // Grid2.DataBind(); //} //protected void Grid1_RowClick(object sender, GridRowClickEventArgs e) //{ // object[] keys = Grid1.DataKeys[e.RowIndex]; // if (keys[0]!=null) // { // BindGrid2(keys[0].ToString()); // } //} protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e) { Grid1.PageIndex = e.NewPageIndex; BindGrid(); } protected void Grid1_Sort(object sender, GridSortEventArgs e) { BindGrid(); } protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e) { Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue); BindGrid(); } protected void btnSearch_Click(object sender, EventArgs e) { BindGrid(); } #region 导出 /// /// 导出 /// /// /// protected void btnExport_Click(object sender, EventArgs e) { var preList = from x in Funs.DB.View_Pre_PointWeldList where x.ProjectId == this.CurrUser.LoginProjectId select x; if (!string.IsNullOrEmpty(txtTrustBatchCode.Text)) { preList = preList.Where(x => x.TrustBatchCode.Contains(txtTrustBatchCode.Text.Trim())); } if (!string.IsNullOrEmpty(txtIsoNo.Text)) { preList = preList.Where(x => x.PipelineCode.Contains(txtIsoNo.Text.Trim())); } preList = preList.OrderBy(x => x.TrustBatchCode).OrderBy(x => x.PipelineCode).OrderBy(x => x.WeldJointCode); string rootPath = Server.MapPath("~/") + Const.ExcelUrl; //模板文件 string TempletFileName = rootPath + "NDE预点口统计表.xlsx"; //导出文件 string filePath = rootPath + DateTime.Now.ToString("yyyyMMddhhmmss") + "\\"; if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } string ReportFileName = filePath + "out.xls"; FileStream file = new FileStream(TempletFileName, FileMode.Open, FileAccess.Read); XSSFWorkbook hssfworkbook = new XSSFWorkbook(file); XSSFSheet ws = (XSSFSheet)hssfworkbook.GetSheet("Sheet1"); int rowIndex = 3; if (preList.Count() > 0) { var style = SetExcelStyle(hssfworkbook, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, BorderStyle.Thin, VerticalAlignment.Center, HorizontalAlignment.Center, 10, true, false); var PointList = (from x in preList select new { x.TrustBatchCode, x.PipelineCode, x.WeldJointCode }).Distinct(); foreach(var item in PointList) { int num = 0; var itemList = preList.Where(x => x.TrustBatchCode == item.TrustBatchCode && x.PipelineCode == item.PipelineCode && x.WeldJointCode == item.WeldJointCode); if (itemList.Count() > 0) { num = itemList.Count() - 1; ws = ExcelCreateRow(ws, hssfworkbook, rowIndex, rowIndex + num, style, 0, 4); //合并单元格 if (num > 0) { CellRangeAddress region = new CellRangeAddress(rowIndex, rowIndex + num, 0, 0); ws.AddMergedRegion(region); region = new CellRangeAddress(rowIndex, rowIndex + num, 1, 1); ws.AddMergedRegion(region); region = new CellRangeAddress(rowIndex, rowIndex + num, 2, 2); ws.AddMergedRegion(region); } int j = 0; foreach (var pre in itemList) { int datanum = rowIndex + j; //合格项目 ws.GetRow(datanum).GetCell(3).SetCellValue(pre.PrePipelineCode); //考试时间 ws.GetRow(datanum).GetCell(4).SetCellValue(pre.PreWeldJointCode); j++; } } else { ws = ExcelCreateRow(ws, hssfworkbook, rowIndex, rowIndex, style, 0, 4); } //委托单 ws.GetRow(rowIndex).GetCell(0).SetCellValue(item.TrustBatchCode); //管线 ws.GetRow(rowIndex).GetCell(1).SetCellValue(item.PipelineCode); //焊口 ws.GetRow(rowIndex).GetCell(2).SetCellValue(item.WeldJointCode); rowIndex += 1 + num; } } 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=NDE预点口统计表" + Server.UrlEncode(DateTime.Now.ToString("yyyyMMddhhmmss") + ".xlsx")); // 添加头信息,指定文件大小,让浏览器能够显示下载进度 Response.AddHeader("Content-Length", filet.Length.ToString()); // 指定返回的是一个不能被客户端读取的流,必须被下载 Response.ContentType = "application/ms-excel"; // 把文件流发送到客户端 Response.WriteFile(filet.FullName); // 停止页面的执行 Response.End(); } #endregion #region 私有方法 /// /// 创建样式 /// /// private ICellStyle SetExcelStyle(XSSFWorkbook wb, BorderStyle Bottom, BorderStyle Left, BorderStyle Right, BorderStyle Top, VerticalAlignment VerAig, HorizontalAlignment HorAig, double FontSize, bool WrapText = true, bool Bold = false, string FontName = "宋体") { ICellStyle style = wb.CreateCellStyle(); style.BorderBottom = Bottom; style.BorderLeft = Left; style.BorderRight = Right; style.BorderTop = Top; style.VerticalAlignment = VerAig; style.Alignment = HorAig; IFont font = wb.CreateFont(); font.FontHeightInPoints = FontSize; font.IsBold = Bold; font.FontName = FontName; style.SetFont(font); style.WrapText = WrapText; return style; } /// /// 行和列 /// /// private XSSFSheet ExcelCreateRow(XSSFSheet ws, XSSFWorkbook hssfworkbook, int sRows, int eRows, ICellStyle style, int cStart, int cEnd, bool istitle = false) { for (int i = sRows; i <= eRows; i++) { ws.CreateRow(i); if (istitle) { ws.GetRow(i).HeightInPoints = i == sRows ? 49.75f : i == (sRows + 1) ? 13.75f : 38f; } else { ws.GetRow(i).HeightInPoints = 24f; } for (int j = cStart; j <= cEnd; j++) { ws.GetRow(i).CreateCell(j); ws.GetRow(i).GetCell(j).CellStyle = style; } } return ws; } #endregion } }