using GeBoShi.SysCtrl; using ClosedXML.Excel; using HZH_Controls.Controls; using Models; using Newtonsoft.Json.Linq; using Service; using SqlSugar; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Windows.Forms.DataVisualization.Charting; using DocumentFormat.OpenXml.Spreadsheet; using Color = System.Drawing.Color; namespace GeBoShi.UI.DataQuery { public partial class DataQueryFrm : Form { RecordsService service = new RecordsService(); ProductService productService = new ProductService(); Color[] ChartColor = new Color[30] { Color.Red, Color.Blue, Color.Green, Color.YellowGreen, Color.Orange, Color.Peru, Color.DarkRed,Color.GreenYellow, Color.Pink , Color.Brown, Color.CadetBlue, Color.OrangeRed, Color.Cyan, Color.Lime, Color.Magenta, Color.Tan, Color.Sienna, Color.DarkGray, Color.SaddleBrown, Color.DarkBlue, Color.Firebrick,Color.Gainsboro,Color.Honeydew,Color.Khaki,Color.Lavender, Color.LightGoldenrodYellow,Color.Navy, Color.Khaki, Color.IndianRed,Color.Lavender}; public DataQueryFrm() { InitializeComponent(); UIStyle.SetUIStyle(this); this.uiTitel1.FatherForm = this; this.lineChartDefect.Size = new Size(600, 800); this.lineChartFaceWidth.Size = new Size(1000, 300); #region dataGridView设置 uiDataGridView1.AutoGenerateColumns = false;//自动创建列 uiDataGridView1.AllowUserToAddRows = uiDataGridView1.AllowUserToDeleteRows = false;//用户添加删除行 uiDataGridView1.AllowUserToResizeRows = true;//用户调整行大小 uiDataGridView1.AllowUserToResizeColumns = false;//用户调整列大小 uiDataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;//只可选中整行,不是单元格 //显示行号与列宽度自动调整 uiDataGridView1.RowHeadersVisible = true; uiDataGridView1.RowHeadersWidth = 30; uiDataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders;//数据量过百绑定太变 uiDataGridView1.RowPostPaint += (sender, e) =>//序号列头 { SysMgr.showRowNum_onDataGrid_RowPostPaint(this.uiDataGridView1, sender, e); }; uiDataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing; //for (int i = 0; i < dataGridView1.Columns.Count; i++)//禁止点击列头排序 // uiDataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable; //行列交叉处标题 //if (uiDataGridView1.RowHeadersVisible) uiDataGridView1.TopLeftHeaderCell.Value = "SPH/CYL"; //事件 this.uiDataGridView1.DataBindingComplete += this.uiDataGridView1_DataBindingComplete;//bing data时发生,可修改单元格内容 //this.uiDataGridView1.SelectIndexChange += uiDataGridView1_SelectIndexChange;//选择行时发行 //this.uiDataGridView1.CellClick += UiDataGridView1_CellClick; #endregion } private void uiDataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) { var list = uiDataGridView1.DataSource as List; for (int i = 0; i < uiDataGridView1.Rows.Count; i++) { if (list[i].Grade < 1) uiDataGridView1.Rows[i].Cells["colGrade"].Value = ""; else if (list[i].Grade <= 5) uiDataGridView1.Rows[i].Cells["colGrade"].Value = (char)(list[i].Grade + 64); else uiDataGridView1.Rows[i].Cells["colGrade"].Value = "不合格"; //if (list[i].RoleInfo != null) // uiDataGridView1.Rows[i].Cells["colRoleName"].Value = list[i].RoleInfo.Name; } } private Expression> createQueryExpression() { return Expressionable.Create() .And(it => it.CreateTime >= uiDatePicker1.Value.Date) .And(it => it.CreateTime < uiDatePicker2.Value.Date.AddDays(1)) .AndIF(!string.IsNullOrWhiteSpace(txtBarcode.Text), it => it.BarCode.Contains(txtBarcode.Text.Trim())) .AndIF(!string.IsNullOrWhiteSpace(txtBatchId.Text), it => it.BatchId.Contains(txtBatchId.Text.Trim())) .AndIF(!string.IsNullOrWhiteSpace(txtReelId.Text), it => it.ReelId.Contains(txtReelId.Text.Trim())) .ToExpression();//注意 这一句 不能少 } private void skinButton2_Click(object sender, EventArgs e) { //未连接数据库,通过模拟数据来实现 //一般通过ORM的分页去取数据来填充 //pageIndex:第几页,和界面对应,从1开始,取数据可能要用pageIndex //count:单页数据量,也就是PageSize值 int count = 20; int totalCount = 0; var list = service.GetListNav( 1 , count, ref totalCount, createQueryExpression()); uiDataGridView1.DataSource = list; pageCtrl1.RecordCount = totalCount; } private void skinButton1_Click(object sender, EventArgs e) { try { if (this.uiDataGridView1.CurrentRow == null) return; Models.Records record = service.GetModelNav(int.Parse(this.uiDataGridView1.CurrentRow.Cells[0].Value.ToString())); if (record.DefectInfoList.Count < 0) throw new Exception("当前记录无缺陷!"); string path = ConfMgr.SelectFolder(); if (string.IsNullOrWhiteSpace(path)) return; //var list = uiDataGridView1.DataSource as List; //var table = ExcelUtil.ConvertToDataTable(list); //{ 名称=x.Name,Xcm=x.X,Ym=x.Y/100,宽cm=x.Width,高cm=x.Height,面积=x.Area, 置信度 =x.ZXD} var list = record.DefectInfoList;//.Select(x => new { x.Name,x.X,x.Y,x.Width,x.Height,x.Area, x.ZXD}).ToList(); //绘图1 double len = Math.Round(record.Len * 100, 2);//cm this.reDrawDefectPoints(record, new double[] { 0, Math.Round(record.FaceWidthMax + 0.005f, 2) }, new double[] { 0, len }); //绘图2 //var points = Array.ConvertAll(record.FaceWidthListStr.Split(new[] { ',', }, StringSplitOptions.RemoveEmptyEntries),Double.Parse).ToList(); //reDrawFaceWidth(record.FacePointList, // new double[] { 0, Math.Round(len + 0.005f, 2) }, // new double[] { record.FaceWidthMin, Math.Round(record.FaceWidthMax + 0.005f, 2) }); reDrawFaceWidth(record.FacePointList, new double[] { 0, Math.Round(len + 0.005f, 2) }, new double[] { 130, 160 }); // foreach (var item in list) { item.Name = SysMgr.Instance.GetDefectName(record.ProductInfo.ModelName, item.Code); //item.Height = item.Height / 100; //单位错误,保证单位一致 } // string Grade = ""; if (record.Grade < 1) Grade = ""; else if (record.Grade <= 5) Grade = (char)(record.Grade + 64) + ""; else Grade = "不合格"; JsonProductDefects data = new JsonProductDefects() { ProName = record.BarCodeName, BatchId = record.BatchId, ReelId = record.ReelId, Len = record.Len.ToString(), Speed = Math.Round(record.Len / record.TimeLen, 2).ToString(), Grade = Grade, DateTime = record.CreateTime.ToString("yyyy年MM月dd日 HH:mm") }; data.DefectTotal = record.DefectInfoList.GroupBy(x => x.Name).Select(g => new JDefectTotal { Name = g.Key, Count = g.Count() }).ToList(); data.DefectDetail = record.DefectInfoList.Select(x => new JDefectDetail { Index = x.PhotoIndex, Name = x.Name, X = x.X, Y = Math.Round(x.Y / 100.0d, 2), Width = x.Width * 10, Height = x.Height * 10, ZXD = x.ZXD, Area = x.Area * 100, Contrast = x.Contrast }) .OrderBy(x => x.Index).ThenBy(x => x.Y).ToList(); data.Pdt = productService.GetModelNav(record.ProductId); data.xyPix = $"X:{ConfMgr.Instance.SysConfigParams.Cm2px_x},Y:{ConfMgr.Instance.SysConfigParams.Cm2px_y}"; var image1 = captureControl(this.lineChartDefect); var image2 = captureControl(this.lineChartFaceWidth); var filePath = $"{path}缺陷列表_{record.BarCode}_{record.BarCodeName}.xlsx"; exportTabel(data, image1, image2, filePath, record.ProductInfo.ModelName); //if (!res) // throw new Exception("导出失败!"); MessageBox.Show("导出成功!", "导出完成"); System.Diagnostics.Process.Start(filePath); } catch (Exception ex) { MessageBox.Show($"{ex.Message}", "导出失败"); //UIMessageTip.ShowError(ex.Message, 2000); //API.OutputDebugString(ex.StackTrace); } } public void exportTabel(JsonProductDefects ProductDefects, byte[] defectImage, byte[] faceWidthImage, string savePath, string modelName) { //try //{ if (ProductDefects == null) throw new Exception("传入的参数为空"); using (var workbook = new XLWorkbook()) { var wsDefectsDetail = workbook.Worksheets.Add("正面疵点列表"); wsDefectsDetail.RowHeight = 20; wsDefectsDetail.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center; wsDefectsDetail.Style.Alignment.Vertical = XLAlignmentVerticalValues.Center; wsDefectsDetail.Style.Font.FontName = "宋体"; wsDefectsDetail.Column("A").Width = 12; wsDefectsDetail.Column("B").Width = 25; wsDefectsDetail.Column("C").Width = 10; wsDefectsDetail.Column("D").Width = 10; wsDefectsDetail.Column("E").Width = 10; wsDefectsDetail.Column("F").Width = 10; wsDefectsDetail.Column("G").Width = 12; wsDefectsDetail.Column("H").Width = 14; wsDefectsDetail.Column("I").Width = 14; int rowIndex = 1; int cellIndex = 1; #region 第一行 var row1_cell1 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex); row1_cell1.Value = "产品疵点缺陷分布图表"; row1_cell1.Style.Font.Bold = true; //row1_cell1.Style.Font.FontName = "宋体"; row1_cell1.Style.Font.FontSize = 12; var mergeRange_row1 = wsDefectsDetail.Range("A1:I1").Row(1).Merge(); mergeRange_row1.Style.Border.OutsideBorder = XLBorderStyleValues.Thin; #endregion #region 第二行 rowIndex++; var row2_cell1 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex); row2_cell1.Value = "产品品名"; row2_cell1.Style.Font.Bold = true; row2_cell1.Style.Font.FontSize = 10; row2_cell1.Style.Border.OutsideBorder = XLBorderStyleValues.Thin; var row2_cell2 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 1); row2_cell2.DataType = XLDataType.Text; row2_cell2.Value = ProductDefects.ProName; row2_cell2.Style.Font.FontSize = 10; row2_cell2.Style.Border.OutsideBorder = XLBorderStyleValues.Thin; var row2_cell3 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 2); row2_cell3.Value = "产品批号"; row2_cell3.Style = row2_cell1.Style; var row2_cell4 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 3); row2_cell4.Value = "'" + ProductDefects.BatchId; row2_cell4.Style = row2_cell2.Style; var row2_cell5 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 4); row2_cell5.Value = "产品卷号"; row2_cell5.Style = row2_cell1.Style; var row2_cell6 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 5); //row2_cell6.SetDataType(XLDataType.Text);//类型设置不起作用 用"'"+内容代替 //row2_cell6.DataType = XLDataType.Text; row2_cell6.Value = "'" + ProductDefects.ReelId; row2_cell6.Style = row2_cell2.Style; var row2_cell7 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 6); row2_cell7.Value = "长度(米)"; row2_cell7.Style = row2_cell1.Style; var row2_cell8 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 7); row2_cell8.Value = ProductDefects.Len; row2_cell8.Style = row2_cell2.Style; //NULL var row2_cell9 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 8); row2_cell9.Style = row2_cell2.Style; #endregion #region 第三行 rowIndex++; var row3_cell1 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex); row3_cell1.Value = "检验时间"; row3_cell1.Style = row2_cell1.Style; var row3_cell2 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 1); row3_cell2.Value = "'" + ProductDefects.DateTime; row3_cell2.Style = row2_cell2.Style; var row3_cell3 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 2); row3_cell3.Value = "检验长度"; row3_cell3.Style = row2_cell1.Style; var row3_cell4 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 3); row3_cell4.Value = ProductDefects.Len; row3_cell4.Style = row2_cell2.Style; var row3_cell5 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 4); row3_cell5.Value = "平均速度"; row3_cell5.Style = row2_cell1.Style; var row3_cell6 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 5); row3_cell6.Value = ProductDefects.Speed; row3_cell6.Style = row2_cell2.Style; var row3_cell7 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 6); row3_cell7.Value = "等级"; row3_cell7.Style = row2_cell1.Style; var row3_cell8 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 7); row3_cell8.Value = ProductDefects.Grade; row3_cell8.Style = row2_cell2.Style; var row3_cell9 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 8); row3_cell9.Style = row2_cell2.Style; #endregion #region 第四第五行 rowIndex++; var row4_cell1 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex); row4_cell1.Value = "设备参数"; row4_cell1.Style = row2_cell1.Style; row4_cell1.Style.Font.Bold = true; row4_cell1.Style.Font.FontSize = 10; row4_cell1.Style.Border.OutsideBorder = XLBorderStyleValues.Thin; var mergeRange_row4 = wsDefectsDetail.Range("A4:A5").Column(1).Merge(); mergeRange_row4.Style.Border.OutsideBorder = XLBorderStyleValues.Thin; var row4_cell2 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 1); row4_cell2.Value = "光源亮度"; row4_cell2.Style = row2_cell1.Style; var row4_cell3 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 2); row4_cell3.Value = "曝光时间"; row4_cell3.Style = row2_cell1.Style; var row4_cell4 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 3); row4_cell4.Value = "增益"; row4_cell4.Style = row2_cell1.Style; var row4_cell5 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 4); row4_cell5.Value = "行频比"; row4_cell5.Style = row2_cell1.Style; var row4_cell6 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 5); row4_cell6.Value = "物面分辨率"; row4_cell6.Style = row2_cell1.Style; var row4_cell7 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 6); row4_cell7.Value = "触发计数"; row4_cell7.Style = row2_cell1.Style; var row4_cell8 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 7); row4_cell8.Value = "采集计数"; row4_cell8.Style = row2_cell1.Style; var row4_cell9 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 8); row4_cell9.Value = ""; row4_cell9.Style = row2_cell1.Style; rowIndex++; var row5_cell2 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 1); row5_cell2.Value = ProductDefects.Pdt.LightValue; row5_cell2.Style = row2_cell1.Style; var row5_cell3 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 2); row5_cell3.Value = ProductDefects.Pdt.ExposureTime; row5_cell3.Style = row2_cell1.Style; var row5_cell4 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 3); row5_cell4.Value = ProductDefects.Pdt.Gain; row5_cell4.Style = row2_cell1.Style; var row5_cell5 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 4); row5_cell5.Value = ""; row5_cell5.Style = row2_cell1.Style; var row5_cell6 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 5); row5_cell6.Value = ProductDefects.xyPix; row5_cell6.Style = row2_cell1.Style; var row5_cell7 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 6); row5_cell7.Value = ""; row5_cell7.Style = row2_cell1.Style; var row5_cell8 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 7); row5_cell8.Value = ""; row5_cell8.Style = row2_cell1.Style; var row5_cell9 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 8); row5_cell9.Value = ""; row5_cell9.Style = row2_cell1.Style; #endregion #region 第六行后 rowIndex++; if (ProductDefects.DefectTotal != null && ProductDefects.DefectTotal.Count > 0) { cellIndex = 1; int DefectTotalCount = ProductDefects.DefectTotal.Count; //最少5行,固定4列 var row6_cell1 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex); row6_cell1.Value = "检测参数"; row6_cell1.Style.Font.Bold = true; row6_cell1.Style.Font.FontSize = 10; row6_cell1.Style.Border.OutsideBorder = XLBorderStyleValues.Thin; string cellstr = $"A6:A{7 + ProductDefects.DefectTotal.Count}"; var mergeRange_row6 = wsDefectsDetail.Range(cellstr).Column(1).Merge(); mergeRange_row6.Style.Border.OutsideBorder = XLBorderStyleValues.Thin; var row6_cell2 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 1); row6_cell2.Value = "筛选标准"; row6_cell2.Style.Font.Bold = true; row6_cell2.Style.Font.FontSize = 10; row6_cell2.Style.Border.OutsideBorder = XLBorderStyleValues.Thin; var mergeRange_row6_2 = wsDefectsDetail.Range($"B6:I6").Row(1).Merge(); mergeRange_row6_2.Style.Border.OutsideBorder = XLBorderStyleValues.Thin; //第七行 rowIndex++; var row7_cell2 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 1); row7_cell2.Value = "缺陷类型"; row7_cell2.Style = row2_cell1.Style; var row7_cell3 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 2); row7_cell3.Value = "置信度"; row7_cell3.Style = row2_cell1.Style; var row7_cell4 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 3); row7_cell4.Value = "面积"; row7_cell4.Style = row2_cell1.Style; var row7_cell5 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 4); row7_cell5.Value = "对比度下限"; row7_cell5.Style = row2_cell1.Style; var row7_cell6 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 5); row7_cell6.Value = "对比度上限"; row7_cell6.Style = row2_cell1.Style; var row7_cell7 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 6); row7_cell7.Value = "所用模型版本"; row7_cell7.Style = row2_cell1.Style; var row7_cell8 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 7); row7_cell8.Value = "或向选择"; row7_cell8.Style = row2_cell1.Style; var row7_cell9 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + 8); row7_cell9.Value = "本次检出数量"; row7_cell9.Style = row2_cell1.Style; //第八行之后 rowIndex++; for (int j = 1; j <= ProductDefects.DefectTotal.Count; j++) // 行 { var temprowcel2 = wsDefectsDetail.Row(rowIndex + j - 1).Cell(cellIndex + 1); var temprowcel3 = wsDefectsDetail.Row(rowIndex + j - 1).Cell(cellIndex + 2); var temprowcel4 = wsDefectsDetail.Row(rowIndex + j - 1).Cell(cellIndex + 3); var temprowcel5 = wsDefectsDetail.Row(rowIndex + j - 1).Cell(cellIndex + 4); var temprowcel6 = wsDefectsDetail.Row(rowIndex + j - 1).Cell(cellIndex + 5); var temprowcel7 = wsDefectsDetail.Row(rowIndex + j - 1).Cell(cellIndex + 6); var temprowcel8 = wsDefectsDetail.Row(rowIndex + j - 1).Cell(cellIndex + 7); var temprowcel9 = wsDefectsDetail.Row(rowIndex + j - 1).Cell(cellIndex + 8); var tempItemDefectTotal = ProductDefects.DefectTotal[j - 1]; temprowcel2.Value = tempItemDefectTotal.Name; temprowcel2.Style.Font.Bold = true; temprowcel2.Style.Font.FontSize = 10; temprowcel2.Style.Border.OutsideBorder = XLBorderStyleValues.Thin; var temp = ProductDefects.Pdt.QualifiedLimitList.Find(x => SysMgr.Instance.GetDefectName(modelName, x.Code) == tempItemDefectTotal.Name); temprowcel3.Value = temp.ZXD; temprowcel3.Style.Font.Bold = true; temprowcel3.Style.Font.FontSize = 10; temprowcel3.Style.Border.OutsideBorder = XLBorderStyleValues.Thin; temprowcel4.Value = temp.Area; temprowcel4.Style.Font.Bold = true; temprowcel4.Style.Font.FontSize = 10; temprowcel4.Style.Border.OutsideBorder = XLBorderStyleValues.Thin; temprowcel5.Value = ContrastToPercent(temp.ContrastLower); temprowcel5.Style.Font.Bold = true; temprowcel5.Style.Font.FontSize = 10; temprowcel5.Style.Border.OutsideBorder = XLBorderStyleValues.Thin; temprowcel6.Value = ContrastToPercent(temp.ContrastTop); temprowcel6.Style.Font.Bold = true; temprowcel6.Style.Font.FontSize = 10; temprowcel6.Style.Border.OutsideBorder = XLBorderStyleValues.Thin; temprowcel7.Value = ProductDefects.Pdt.ModelName; temprowcel7.Style.Font.Bold = true; temprowcel7.Style.Font.FontSize = 10; temprowcel7.Style.Border.OutsideBorder = XLBorderStyleValues.Thin; temprowcel8.Value = temp.IsOR; temprowcel8.Style.Font.Bold = true; temprowcel8.Style.Font.FontSize = 10; temprowcel8.Style.Border.OutsideBorder = XLBorderStyleValues.Thin; temprowcel9.Value = tempItemDefectTotal.Count; temprowcel9.Style.Font.FontSize = 10; temprowcel9.Style.Border.OutsideBorder = XLBorderStyleValues.Thin; } //更新行号 rowIndex = rowIndex + ProductDefects.DefectTotal.Count; } #endregion #region 最后 //rowIndex++; if (ProductDefects.DefectDetail != null && ProductDefects.DefectDetail.Count > 0) { List lstRow5str = new List() { "源图", "名称", "X(cm)", "Y(米)", "宽(mm)", "高(mm)", "置信度", "面积(mm^2)", "对比度" }; for (int i = 0; i < lstRow5str.Count; i++) { var temp_row5_cell = wsDefectsDetail.Row(rowIndex).Cell(cellIndex + i); temp_row5_cell.Value = lstRow5str[i]; temp_row5_cell.Style.Font.Bold = true; temp_row5_cell.Style.Font.FontSize = 11; temp_row5_cell.Style.Font.FontName = "等线"; temp_row5_cell.Style.Border.OutsideBorder = XLBorderStyleValues.Thin; } rowIndex++; for (int i = 0; i < ProductDefects.DefectDetail.Count; i++) for (int j = 0; j < lstRow5str.Count; j++) { var temp_row6_cell = wsDefectsDetail.Row(rowIndex + i).Cell(cellIndex + j); switch (lstRow5str[j]) { case "源图": temp_row6_cell.Value = ProductDefects.DefectDetail[i].Index; break; case "名称": temp_row6_cell.Value = ProductDefects.DefectDetail[i].Name; break; case "X(cm)": temp_row6_cell.Value = ProductDefects.DefectDetail[i].X; break; case "Y(米)": temp_row6_cell.Value = ProductDefects.DefectDetail[i].Y; break; case "宽(mm)": temp_row6_cell.Value = ProductDefects.DefectDetail[i].Width; break; case "高(mm)": temp_row6_cell.Value = ProductDefects.DefectDetail[i].Height; break; case "置信度": temp_row6_cell.Value = ProductDefects.DefectDetail[i].ZXD; break; case "面积(mm^2)": temp_row6_cell.Value = ProductDefects.DefectDetail[i].Area; break; case "对比度": temp_row6_cell.Value = ProductDefects.DefectDetail[i].Contrast; break; } temp_row6_cell.Style.Font.FontSize = 11; temp_row6_cell.Style.Font.FontName = "等线"; temp_row6_cell.Style.Border.OutsideBorder = XLBorderStyleValues.Thin; } // row6_cell1.InsertTable(ProductDefects.DefectDetail); } #endregion var wsDefectsImg = workbook.Worksheets.Add("正面疵点分布图"); wsDefectsImg.AddPicture(new MemoryStream(defectImage), "纵向计算") .MoveTo(wsDefectsImg.Cell(1, 1)); var wsFaceWidthImg = workbook.Worksheets.Add("门幅曲线"); wsFaceWidthImg.AddPicture(new MemoryStream(faceWidthImage), "幅宽曲线图") .MoveTo(wsFaceWidthImg.Cell(1, 1)); workbook.SaveAs(savePath); } // return true; //} //catch (Exception ex) //{ // return false; //} } private double ContrastLow = 0.8; private double ContrastTop = 1.2; private double ContrastToPercent(double val) { if (val < ContrastLow) return 0; else if (val > ContrastTop) return 100; double temp = 100 / (ContrastTop - ContrastLow); return Math.Round(temp * (val - ContrastLow), 2); } // /// /// 重新生成缺陷分布(cm2M在内部转换) /// /// Records.DefectInfoList /// 幅宽 /// 卷长度 private void reDrawDefectPoints(Records records, double[] XSizeRange, double[] YSizeRange) { //AddTextEvent($"绘图", $"缺陷分布, W={string.Join(", ", XSizeRange)},H={string.Join(", ", YSizeRange)}, LastData={JsonConvert.SerializeObject(lstDefectInfo[lstDefectInfo.Count - 1])}"); var lstData = records.DefectInfoList.OrderBy(m => m.Code).ThenBy(m => m.Code).ToList(); if (XSizeRange == null || YSizeRange == null) return; else { if (YSizeRange[0] == YSizeRange[1]) { YSizeRange[0] -= YSizeRange[0] / 10f; YSizeRange[1] += YSizeRange[1] / 10f; } YSizeRange[0] /= 100; YSizeRange[1] /= 100; lineChartDefect.Series.Clear(); lineChartDefect.ChartAreas[0].AxisX.Title = "宽度(cm)"; lineChartDefect.ChartAreas[0].AxisY.Title = "长度(m)"; //option = new UILineOption(); //option.XAxis.Name = "面宽(cm)"; //option.YAxis.Name = "长度(米)"; //option.Grid.Top = 20;//边距 //option.Grid.Right = 20;//边距 foreach (var rowItem in lstData) { string name = SysMgr.Instance.GetDefectName(records.ProductInfo.ModelName, rowItem.Code); if (lineChartDefect.Series.FindByName(name) == null)//rowItem[3] 为name { lineChartDefect.Series.Add(name); var obj = SysMgr.Instance.GetDefectLabel(records.ProductInfo.ModelName, name); int labelIndex = obj.Value("id"); lineChartDefect.Series[name].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point; lineChartDefect.Series[name].MarkerSize = 5; lineChartDefect.Series[name].MarkerColor = ChartColor[labelIndex]; lineChartDefect.Series[name].MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Circle; } lineChartDefect.Series[name].Points.AddXY(rowItem.CentreX, rowItem.CentreY /100); } lineChartDefect.ChartAreas[0].AxisX.Minimum = XSizeRange[0]; lineChartDefect.ChartAreas[0].AxisX.Maximum = XSizeRange[1]; lineChartDefect.ChartAreas[0].AxisY.Minimum = YSizeRange[0]; lineChartDefect.ChartAreas[0].AxisY.Maximum = YSizeRange[1]; //X轴数据类型 //option.XAxisType = UIAxisType.Value; //设置X/Y轴显示范围 //option.XAxis.SetRange(XSizeRange[0], XSizeRange[1]); //option.YAxis.SetRange(YSizeRange[0], YSizeRange[1]); //坐标轴显示小数位数 //option.XAxis.AxisLabel.DecimalPlaces = option.YAxis.AxisLabel.DecimalPlaces = 1; //X/Y轴画参考线 //option.YAxisScaleLines.Add(new UIScaleLine("上限", 3.5, Color.Red)); //option.YAxisScaleLines.Add(new UIScaleLine("下限", 2.2, Color.Gold)); //option.XAxisScaleLines.Add(new UIScaleLine(dt.AddHours(3).DateTimeString(), dt.AddHours(3), Color.Red)); //option.XAxisScaleLines.Add(new UIScaleLine(dt.AddHours(6).DateTimeString(), dt.AddHours(6), Color.Red)); //option.ToolTip.Visible = true; //option.ToolTip.Formatter = "怎么自定义X,Y显示名称??{X}"; //option.Title = new UITitle(); //option.Title.Text = ""; //option.Title.SubText = ""; } //string preCode = ""; //UILineSeries series = null; //foreach (var item in lstData) //{ // if (preCode != item.Code)//加一组新类型及样式 // { // preCode = item.Code; // var one = Config.getDefectItem(item.Code); // if (one == null) // { // continue; // } // JObject objItem = one as JObject; // Color color = ColorTranslator.FromHtml(objItem.Value("color")); // series = option.AddSeries(new UILineSeries(objItem.Value("name"), color));//加一组 // series.Symbol = UILinePointSymbol.Star; // series.SymbolSize = 4; // series.SymbolLineWidth = 2; // series.ShowLine = false; // series.SymbolColor = color; // //数据点显示小数位数(针对当前UILineSeries) // series.XAxisDecimalPlaces = 1; // series.YAxisDecimalPlaces = 2; // //series.Smooth = false; // } // series.Add(item.CentreX, item.CentreY / 100); //cm -> m //} //==== //option.GreaterWarningArea = new UILineWarningArea(3.5); //option.LessWarningArea = new UILineWarningArea(2.2, Color.Gold); //this.BeginInvoke(new System.Action(() => //{ //this.lineChartDefect.SetOption(option); //series.UpdateYData();//按序号更新Y轴值(可设置值超出范围用于闪烁) //})); } /// /// 重新门幅宽度 /// /// /// 卷长度 /// 幅宽 private void reDrawFaceWidth(List points, double[] XSizeRange, double[] YSizeRange) { //AddTextEvent($"绘图", $"门幅宽度, W={string.Join(", ", XSizeRange)},H={string.Join(", ", YSizeRange)}, LastData={JsonConvert.SerializeObject(points[points.Count-1])}"); if (YSizeRange[0] == YSizeRange[1]) { YSizeRange[0] -= YSizeRange[0] / 10f; YSizeRange[1] += YSizeRange[1] / 10f; } XSizeRange[0] /= 100; XSizeRange[1] /= 100; //防止超限 XSizeRange[1] += 0.01; YSizeRange[1] += 0.1; lineChartFaceWidth.Series.Clear(); lineChartFaceWidth.Series.Add("面宽"); lineChartFaceWidth.ChartAreas[0].AxisX.Title = "长度(m)"; lineChartFaceWidth.ChartAreas[0].AxisY.Title = "宽度(cm)"; lineChartFaceWidth.Series["面宽"].ChartType = SeriesChartType.Line; lineChartFaceWidth.Series["面宽"].MarkerSize = 5; lineChartFaceWidth.Series["面宽"].MarkerColor = Color.Blue; lineChartFaceWidth.Series["面宽"].MarkerStyle = MarkerStyle.Circle; //UILineOption option = new UILineOption(); //option.XAxis.Name = "长度(米)"; //option.YAxis.Name = "面宽(cm)"; //option.Grid.Top = 20; //option.Grid.Right = 20; //X轴数据类型 //option.XAxisType = UIAxisType.Value; //设置X/Y轴显示范围 //option.XAxis.SetRange(XSizeRange[0], XSizeRange[1]); //option.YAxis.SetRange(YSizeRange[0], YSizeRange[1]); //坐标轴显示小数位数 //option.XAxis.AxisLabel.DecimalPlaces = option.YAxis.AxisLabel.DecimalPlaces = 1; //X/Y轴画参考线 //option.YAxisScaleLines.Add(new UIScaleLine("上限", 3.5, Color.Red)); //option.YAxisScaleLines.Add(new UIScaleLine("下限", 2.2, Color.Gold)); //option.XAxisScaleLines.Add(new UIScaleLine(dt.AddHours(3).DateTimeString(), dt.AddHours(3), Color.Red)); //option.XAxisScaleLines.Add(new UIScaleLine(dt.AddHours(6).DateTimeString(), dt.AddHours(6), Color.Red)); //option.ToolTip.Visible = true; //option.ToolTip.Formatter = "怎么自定义X,Y显示名称??{X}"; //option.Title = new UITitle(); //option.Title.Text = ""; //option.Title.SubText = ""; //Color color = Color.Blue; //UILineSeries series = null; //series = option.AddSeries(new UILineSeries("面宽", color)); //series.Symbol = UILinePointSymbol.Circle; //series.ShowLine = true; //series.SymbolSize = 4; //series.SymbolLineWidth = 2; //series.SymbolColor = color; ////数据点显示小数位数(针对当前UILineSeries) //series.XAxisDecimalPlaces = 2; //series.YAxisDecimalPlaces = 1; lineChartFaceWidth.ChartAreas[0].AxisY.Maximum = YSizeRange[1]; lineChartFaceWidth.ChartAreas[0].AxisY.Minimum = YSizeRange[0]; lineChartFaceWidth.ChartAreas[0].AxisX.Maximum = XSizeRange[1]; lineChartFaceWidth.ChartAreas[0].AxisX.Minimum = XSizeRange[0]; float x; foreach (var item in points) { x = item[0] / 100; //cm -> m //series.Add(x, item[1]); this.lineChartFaceWidth.Series["面宽"].Points.AddXY((float)Math.Round(x, 2), (float)Math.Round(item[1], 2)); } //==== //option.GreaterWarningArea = new UILineWarningArea(3.5); //option.LessWarningArea = new UILineWarningArea(2.2, Color.Gold); //this.BeginInvoke(new System.Action(() => //{ //this.lineChartFaceWidth.SetOption(option); //})); } // 截图操作函数 private byte[] captureControl(System.Windows.Forms.Control control) { Bitmap bmp = new Bitmap(control.Width, control.Height); Graphics graphics = Graphics.FromImage(bmp); Rectangle rectangle = new Rectangle(0, 0, control.Width, control.Height); control.DrawToBitmap(bmp, rectangle); //bmp to jpg MemoryStream ms = new MemoryStream(); bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);//bmp是已经获得的bitmap数据 byte[] bytes = ms.GetBuffer(); ms.Close(); graphics.Dispose(); return bytes; //bitmap.Save(@"C:\Images\Capture.jpg", ImageFormat.Jpeg); //return Image.FromStream(new MemoryStream(bytes)); } public class JsonProductDefects { [Description("产品品名")] public string ProName { get; set; } [Description("产品批号")] public string BatchId { get; set; } [Description("产品卷号")] public string ReelId { get; set; } [Description("检验长度")] public string Len { get; set; } [Description("平均速度")] public string Speed { get; set; } [Description("等级")] public string Grade { get; set; } [Description("检验时间")] public string DateTime { get; set; } [Description("疵点统计")] public List DefectTotal = new List(); public List DefectDetail = new List(); [Description("检测设置")] public Models.Product Pdt { get; set; } [Description("物面分辨率")] public string xyPix { get; set; } } public class JDefectTotal { [Description("疵点名")] public string Name { get; set; } [Description("疵点数")] public int Count { get; set; } } public class JDefectDetail { [Description("源图")] public int Index { get; set; } [Description("名称")] public string Name { get; set; } [Description("X(cm)")] public double X { get; set; } [Description("Y(米)")] public double Y { get; set; } [Description("宽(cm)")] public double Width { get; set; } [Description("高(cm)")] public double Height { get; set; } [Description("置信度")] public double ZXD { get; set; } [Description("面积(cm^2)")] public double Area { get; set; } [Description("对比度")] public double Contrast { get; set; } } } }