complete config for reading excel data
This commit is contained in:
126
LibReadTetraExcel/ExcelHelper.cs
Normal file
126
LibReadTetraExcel/ExcelHelper.cs
Normal file
@@ -0,0 +1,126 @@
|
||||
using LibDataBase;
|
||||
using OfficeOpenXml;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
namespace LibReadTetraExcel
|
||||
{
|
||||
public class ExcelHelper
|
||||
{
|
||||
public static OrderConfig ReadOCROrder(string execlFileName, string order)
|
||||
{
|
||||
OrderConfig config = new OrderConfig();
|
||||
try
|
||||
{
|
||||
ExcelPackage.License.SetNonCommercialOrganization("My Noncommercial organization");
|
||||
using (ExcelPackage package = new ExcelPackage(execlFileName))
|
||||
{
|
||||
ExcelWorksheet sheet1 = package.Workbook.Worksheets["P2生成数据"];
|
||||
config.ParseQSV = false;
|
||||
config.ParseProduct = false;
|
||||
config.ParseArrange = false;
|
||||
config.QSV = "";
|
||||
config.NumberOfLanes = 0;
|
||||
for (int i = 1; i < sheet1.Dimension.Rows; i++)
|
||||
{
|
||||
if (sheet1.GetValue(i, 3) == null)
|
||||
continue;
|
||||
if (sheet1.Cells[i, 3].Value.ToString() != order)
|
||||
continue;
|
||||
config.QSV = sheet1.Cells[i, 4].Value.ToString();
|
||||
config.NumberOfLanes = Convert.ToInt32(sheet1.Cells[i, 5].Value.ToString());
|
||||
int num = 0;
|
||||
for (int k = 0; k < 10; k++)
|
||||
{
|
||||
string lanes = sheet1.Cells[i + k, 6].Value.ToString();
|
||||
string design = sheet1.Cells[i + k, 7].Value.ToString().Split('-')[1];
|
||||
string[] tmp = lanes.Substring(1, lanes.Length - 2).Split(',');
|
||||
num += tmp.Length;
|
||||
List<string> textOCR = ExecelGetOcrText(design, sheet1.Cells[i + k, 8].Value.ToString());
|
||||
foreach (string lan in tmp)
|
||||
{
|
||||
int lanInt = Convert.ToInt32(lan);
|
||||
config.OCRRequest[lanInt] = textOCR;
|
||||
config.OCRDesign[lanInt] = design;
|
||||
}
|
||||
|
||||
if (num >= config.NumberOfLanes)
|
||||
{
|
||||
config.ParseQSV = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (!config.ParseQSV)
|
||||
return config;
|
||||
|
||||
config.ProductStandard = "";
|
||||
ExcelWorksheet sheet2 = package.Workbook.Worksheets["QSV对应产品规格和梯度"];
|
||||
for (int j = 1; j <= sheet2.Dimension.Rows; j++)
|
||||
{
|
||||
if (sheet2.GetValue(j, 1) == null)
|
||||
continue;
|
||||
if (sheet2.Cells[j, 1].Value.ToString() != config.QSV)
|
||||
continue;
|
||||
config.ProductStandard = sheet2.Cells[j, 2].Value.ToString();
|
||||
config.Width = Convert.ToDouble(sheet2.Cells[j, 3].Value.ToString());
|
||||
config.Height = Convert.ToDouble(sheet2.Cells[j, 4].Value.ToString());
|
||||
config.Gradient = Convert.ToDouble(sheet2.Cells[j, 5].Value.ToString());
|
||||
config.ParseProduct = true;
|
||||
break;
|
||||
}
|
||||
if (!config.ParseProduct)
|
||||
return config;
|
||||
|
||||
ExcelWorksheet sheet3 = package.Workbook.Worksheets["产品规格对应排布方式"];
|
||||
for (int n = 1; n < sheet3.Dimension.Rows; n++)
|
||||
{
|
||||
if (sheet3.GetValue(n, 1) == null)
|
||||
continue;
|
||||
if (sheet3.Cells[n, 1].Value.ToString() != config.ProductStandard)
|
||||
continue;
|
||||
string str = sheet3.Cells[n + config.OCRRequest[1].Count - 1, 4].Value.ToString();
|
||||
string[] x_y = str.Split(',');
|
||||
string[] X = x_y[0].Split(':');
|
||||
string[] Y = x_y[1].Split(':');
|
||||
config.DistX = Convert.ToDouble(X[1].Replace("mm", ""));
|
||||
config.DistY = Convert.ToDouble(Y[1].Replace("mm", ""));
|
||||
|
||||
string alignColor = sheet3.Cells[n + config.OCRRequest[1].Count - 1, 3].Value.ToString();
|
||||
if (alignColor.Contains("单排"))
|
||||
config.ColorMax = config.OCRRequest.Values.Select(x => x.Count).Max();
|
||||
else
|
||||
config.ColorMax = alignColor.Replace("排布", "").Split(',').ToList().Select(int.Parse).Max();
|
||||
config.ColorArray = alignColor;
|
||||
config.ParseArrange = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
}
|
||||
return config;
|
||||
}
|
||||
private static List<string> ExecelGetOcrText(string design, string layer)
|
||||
{
|
||||
List<string> result = new List<string>();
|
||||
string[] ll = layer.Replace("[", "").Replace("]", "").Split(',');
|
||||
for (int i = 0; i < ll.Length; i++)
|
||||
{
|
||||
ll[i] = ll[i].Replace(" ", "");
|
||||
string[] str = ll[i].Split('-');
|
||||
if (str.Length < 2)
|
||||
continue;
|
||||
result.Add(design + str[1] + str[0]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user