845 lines
40 KiB
C#
845 lines
40 KiB
C#
using ClosedXML.Excel;
|
||
using LeatherApp.Utils;
|
||
using Models;
|
||
using Newtonsoft.Json.Linq;
|
||
using Service;
|
||
using SqlSugar;
|
||
using Sunny.UI;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel;
|
||
using System.Data;
|
||
using System.Drawing;
|
||
using System.Drawing.Imaging;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Linq.Expressions;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Windows.Forms;
|
||
|
||
namespace LeatherApp.Page
|
||
{
|
||
public partial class FReport : UIPage
|
||
{
|
||
RecordsService service=new RecordsService();
|
||
public FReport( )
|
||
{
|
||
InitializeComponent();
|
||
uiDatePicker1.Value = DateTime.Now.AddMonths(-1);
|
||
uiDatePicker2.Value=DateTime.Now;
|
||
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) =>//序号列头
|
||
{
|
||
Utils.Util.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
|
||
#region 分页及页脚合计设置
|
||
this.uiPagination1.PageChanged += new Sunny.UI.UIPagination.OnPageChangeEventHandler(this.uiPagination1_PageChanged);
|
||
//设置分页控件每页数量
|
||
uiPagination1.PageSize = 20;
|
||
//设置统计绑定的表格
|
||
//uiDataGridViewFooter1.DataGridView = uiDataGridView1;
|
||
//激活第1第,触发uiPagination1_PageChanged
|
||
uiPagination1.ActivePage = 1;
|
||
#endregion
|
||
}
|
||
private Expression<Func<Records, bool>> createQueryExpression()
|
||
{
|
||
return Expressionable.Create<Records>()
|
||
.And(it => it.CreateTime >= uiDatePicker1.Value.SetTime(0,0,0))
|
||
.And(it => it.CreateTime < uiDatePicker2.Value.SetTime(0, 0, 0).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();//注意 这一句 不能少
|
||
}
|
||
/// <summary>
|
||
/// 分页控件页面切换事件
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="pagingSource"></param>
|
||
/// <param name="pageIndex"></param>
|
||
/// <param name="count"></param>
|
||
private void uiPagination1_PageChanged(object sender, object pagingSource, int pageIndex, int count)
|
||
{
|
||
//未连接数据库,通过模拟数据来实现
|
||
//一般通过ORM的分页去取数据来填充
|
||
//pageIndex:第几页,和界面对应,从1开始,取数据可能要用pageIndex
|
||
//count:单页数据量,也就是PageSize值
|
||
int totalCount = 0;
|
||
var list = service.GetListNav(pageIndex , count, ref totalCount, createQueryExpression());
|
||
uiDataGridView1.DataSource = list;
|
||
uiPagination1.TotalCount = totalCount;
|
||
//表脚合计
|
||
//uiDataGridViewFooter1.Clear();
|
||
//uiDataGridViewFooter1["Column1"] = "合计:";
|
||
//uiDataGridViewFooter1["Column2"] = "Column2_" + pageIndex;
|
||
//uiDataGridViewFooter1["Column3"] = "Column3_" + pageIndex;
|
||
//uiDataGridViewFooter1["Column4"] = "Column4_" + pageIndex;
|
||
|
||
//
|
||
//this.uiDataGridView1.CurrentCell = null;
|
||
//this.uiDataGridView1.ClearSelection();
|
||
}
|
||
private void uiDataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
|
||
{
|
||
var list = uiDataGridView1.DataSource as List<Records>;
|
||
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 void btnQuery_Click(object sender, EventArgs e)
|
||
{
|
||
uiPagination1.ActivePage = 1;
|
||
}
|
||
|
||
private void btnExport_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 = FileUtil.selectFolder();
|
||
if (string.IsNullOrWhiteSpace(path))
|
||
return;
|
||
|
||
//var list = uiDataGridView1.DataSource as List<Records>;
|
||
//var table = ExcelUtil.ConvertToDataTable<Records>(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.DefectInfoList, 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) });
|
||
|
||
//
|
||
foreach (var item in list) {
|
||
item.Name = Config.getDefectName(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();
|
||
var image1 = captureControl(this.lineChartDefect);
|
||
var image2=captureControl(this.lineChartFaceWidth);
|
||
var filePath = $"{path}缺陷列表_{record.BatchId}_{record.ReelId}.xlsx";
|
||
exportTabel(data, image1, image2, filePath);
|
||
//if (!res)
|
||
// throw new Exception("导出失败!");
|
||
UIMessageTip.ShowOk("导出成功!", 1000);
|
||
System.Diagnostics.Process.Start(filePath);
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
UIMessageTip.ShowError(ex.Message, 2000);
|
||
API.OutputDebugString(ex.StackTrace);
|
||
}
|
||
}
|
||
private void btnExport_Click1(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 = FileUtil.selectFolder();
|
||
if (string.IsNullOrWhiteSpace(path))
|
||
return;
|
||
|
||
//var list = uiDataGridView1.DataSource as List<Records>;
|
||
//var table = ExcelUtil.ConvertToDataTable<Records>(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();
|
||
foreach (var item in list)
|
||
{
|
||
item.Name = Config.getDefectName(item.Code);
|
||
item.Height = item.Height / 100;
|
||
}
|
||
|
||
var table = ExcelUtil.ConvertToDataTable<DefectInfo>(list, new List<string> { "PhotoIndex", "Name", "ZXD", "Contrast", "X", "Y", "Width", "Height", "Area" });
|
||
for (int i = 0; i < table.Columns.Count; i++)
|
||
{
|
||
switch (table.Columns[i].ColumnName)
|
||
{
|
||
case "PhotoIndex":
|
||
table.Columns[i].ColumnName = "源图";
|
||
break;
|
||
case "Name":
|
||
table.Columns[i].ColumnName = "名称";
|
||
break;
|
||
case "ZXD":
|
||
table.Columns[i].ColumnName = "置信度";
|
||
break;
|
||
case "Contrast":
|
||
table.Columns[i].ColumnName = "对比度";
|
||
break;
|
||
case "X":
|
||
table.Columns[i].ColumnName = "X(cm)";
|
||
break;
|
||
case "Y":
|
||
table.Columns[i].ColumnName = "Y(cm)";
|
||
break;
|
||
case "Width":
|
||
table.Columns[i].ColumnName = "宽(cm)";
|
||
break;
|
||
case "Height":
|
||
table.Columns[i].ColumnName = "长度(米)";
|
||
break;
|
||
case "Area":
|
||
table.Columns[i].ColumnName = "面积(cm^2)";
|
||
break;
|
||
}
|
||
}
|
||
|
||
ExcelUtil.DataTable2CSV($"{path}\\缺陷列表_{record.BarCodeName}.csv", table);
|
||
UIMessageTip.ShowOk("导出成功!", 1000);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
UIMessageTip.ShowError(ex.Message, 2000);
|
||
}
|
||
}
|
||
|
||
public void exportTabel(JsonProductDefects ProductDefects, byte[] defectImage, byte[] faceWidthImage,string savePath)
|
||
{
|
||
//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++;
|
||
|
||
if (ProductDefects.DefectTotal != null && ProductDefects.DefectTotal.Count > 0)
|
||
{
|
||
cellIndex = 1;
|
||
int DefectTotalCount = ProductDefects.DefectTotal.Count;
|
||
//最少5行,固定4列
|
||
if (DefectTotalCount <= 20)
|
||
{
|
||
var row4_cell1 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex);
|
||
|
||
row4_cell1.Value = "疵点统计";
|
||
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:A8").Column(1).Merge();
|
||
mergeRange_row4.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
|
||
|
||
for (int i = 1; i <= 4; i++)
|
||
{
|
||
for (int j = 1; j <= 5; j++)
|
||
{
|
||
var temprowcell = wsDefectsDetail.Row(rowIndex + j - 1).Cell(cellIndex + i * 2 - 1);
|
||
var temprowcell_1 = wsDefectsDetail.Row(rowIndex + j - 1).Cell(cellIndex + i * 2);
|
||
if ((i - 1) * 5 + j <= DefectTotalCount)
|
||
{
|
||
var tempItemDefectTotal = ProductDefects.DefectTotal[(i - 1) * 5 + j - 1];
|
||
temprowcell.Value = tempItemDefectTotal.Name;
|
||
temprowcell.Style.Font.Bold = true;
|
||
temprowcell.Style.Font.FontSize = 10;
|
||
temprowcell.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
|
||
|
||
temprowcell_1.Value = tempItemDefectTotal.Count;
|
||
temprowcell_1.Style.Font.FontSize = 10;
|
||
temprowcell_1.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
|
||
|
||
}
|
||
else
|
||
{
|
||
temprowcell.Style.Font.Bold = true;
|
||
temprowcell.Style.Font.FontSize = 10;
|
||
temprowcell.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
|
||
|
||
temprowcell_1.Style.Font.FontSize = 10;
|
||
temprowcell_1.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
|
||
}
|
||
}
|
||
}
|
||
|
||
//补齐最后一列空列
|
||
//for (int i = 0; i < 5; i++)
|
||
//{
|
||
// var temprowcell = wsDefectsDetail.Row(rowIndex + i).Cell(8);
|
||
// temprowcell.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
|
||
//}
|
||
|
||
//更新行号
|
||
rowIndex = rowIndex + 5;
|
||
}
|
||
else //>20
|
||
{
|
||
var row4_cell1 = wsDefectsDetail.Row(rowIndex).Cell(cellIndex);
|
||
|
||
row4_cell1.Value = "疵点统计";
|
||
row4_cell1.Style.Font.Bold = true;
|
||
row4_cell1.Style.Font.FontSize = 10;
|
||
row4_cell1.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
|
||
|
||
int DefectTotalRowCount = (int)(ProductDefects.DefectTotal.Count / 4.0f + 0.5);
|
||
|
||
var mergeRange_row4 = wsDefectsDetail.Range($"A{rowIndex}:A{rowIndex+ DefectTotalRowCount-1}").Column(1).Merge();
|
||
mergeRange_row4.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
|
||
|
||
for (int i = 1; i <= 4; i++)
|
||
{
|
||
for (int j = 1; j <= DefectTotalRowCount; j++)
|
||
{
|
||
var temprowcell = wsDefectsDetail.Row(rowIndex + j - 1).Cell(cellIndex + i * 2 - 1);
|
||
var temprowcell_1 = wsDefectsDetail.Row(rowIndex + j - 1).Cell(cellIndex + i * 2);
|
||
if ((i - 1) * DefectTotalRowCount + j <= DefectTotalCount)
|
||
{
|
||
var tempItemDefectTotal = ProductDefects.DefectTotal[(i - 1) * DefectTotalRowCount + j - 1];
|
||
temprowcell.Value = tempItemDefectTotal.Name;
|
||
temprowcell.Style.Font.Bold = true;
|
||
temprowcell.Style.Font.FontSize = 10;
|
||
temprowcell.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
|
||
|
||
temprowcell_1.Value = tempItemDefectTotal.Count;
|
||
temprowcell_1.Style.Font.FontSize = 10;
|
||
temprowcell_1.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
|
||
|
||
}
|
||
else
|
||
{
|
||
temprowcell.Style.Font.Bold = true;
|
||
temprowcell.Style.Font.FontSize = 10;
|
||
temprowcell.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
|
||
|
||
temprowcell_1.Style.Font.FontSize = 10;
|
||
temprowcell_1.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
|
||
}
|
||
}
|
||
}
|
||
|
||
//补齐最后一列空列
|
||
//for (int i = 0; i < DefectTotalRowCount; i++)
|
||
//{
|
||
// var temprowcell = wsDefectsDetail.Row(rowIndex + i).Cell(8);
|
||
// temprowcell.Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
|
||
//}
|
||
|
||
//更新行号
|
||
rowIndex = rowIndex + DefectTotalRowCount;
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 第五行
|
||
//rowIndex++;
|
||
if (ProductDefects.DefectDetail != null && ProductDefects.DefectDetail.Count > 0)
|
||
{
|
||
List<string> lstRow5str = new List<string>() { "源图", "名称", "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 "宽(cm)":
|
||
temp_row6_cell.Value = ProductDefects.DefectDetail[i].Width;
|
||
break;
|
||
case "高(cm)":
|
||
temp_row6_cell.Value = ProductDefects.DefectDetail[i].Height;
|
||
break;
|
||
case "置信度":
|
||
temp_row6_cell.Value = ProductDefects.DefectDetail[i].ZXD;
|
||
break;
|
||
case "面积(cm^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;
|
||
//}
|
||
|
||
}
|
||
public static Image getImageBase64(string imgBase64)
|
||
{
|
||
byte[] imgByte = Convert.FromBase64String(imgBase64);
|
||
Stream stream = new MemoryStream(imgByte);
|
||
|
||
return Image.FromStream(stream);
|
||
}
|
||
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<JDefectTotal> DefectTotal = new List<JDefectTotal>();
|
||
public List<JDefectDetail> DefectDetail = new List<JDefectDetail>();
|
||
}
|
||
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; }
|
||
}
|
||
|
||
|
||
//
|
||
/// <summary>
|
||
/// 重新生成缺陷分布(cm2M在内部转换)
|
||
/// </summary>
|
||
/// <param name="lstDefectInfo">Records.DefectInfoList</param>
|
||
/// <param name="XSizeRange">幅宽</param>
|
||
/// <param name="YSizeRange">卷长度</param>
|
||
private void reDrawDefectPoints(List<DefectInfo> lstDefectInfo, double[] XSizeRange, double[] YSizeRange)
|
||
{
|
||
UILineOption option;
|
||
//AddTextEvent($"绘图", $"缺陷分布, W={string.Join(", ", XSizeRange)},H={string.Join(", ", YSizeRange)}, LastData={JsonConvert.SerializeObject(lstDefectInfo[lstDefectInfo.Count - 1])}");
|
||
var lstData = lstDefectInfo.OrderBy(m => m.Code).ThenBy(m => m.Code).ToList();
|
||
|
||
if (XSizeRange == null || YSizeRange == null)
|
||
option = this.lineChartDefect.Option;
|
||
else
|
||
{
|
||
if (YSizeRange[0] == YSizeRange[1])
|
||
{
|
||
YSizeRange[0] -= YSizeRange[0] / 10f;
|
||
YSizeRange[1] += YSizeRange[1] / 10f;
|
||
}
|
||
YSizeRange[0] /= 100;
|
||
YSizeRange[1] /= 100;
|
||
|
||
option = new UILineOption();
|
||
option.XAxis.Name = "面宽(cm)";
|
||
option.YAxis.Name = "长度(米)";
|
||
//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 = "";
|
||
}
|
||
|
||
|
||
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<string>("color"));
|
||
series = option.AddSeries(new UILineSeries(objItem.Value<string>("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轴值(可设置值超出范围用于闪烁)
|
||
//}));
|
||
}
|
||
|
||
/// <summary>
|
||
/// 重新门幅宽度
|
||
/// </summary>
|
||
/// <param name="points"></param>
|
||
/// <param name="XSizeRange">卷长度</param>
|
||
/// <param name="YSizeRange">幅宽</param>
|
||
private void reDrawFaceWidth(List<float[]> 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;
|
||
|
||
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;
|
||
|
||
float x;
|
||
foreach (var item in points)
|
||
{
|
||
x = item[0] / 100; //cm -> m
|
||
series.Add(x, item[1]);
|
||
}
|
||
//====
|
||
//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(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));
|
||
}
|
||
|
||
private void btnChar_Click(object sender, EventArgs e)
|
||
{
|
||
Frame.SelectPage(1004);
|
||
}
|
||
}
|
||
}
|