using DevExpress.Spreadsheet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace DeviceRepairAndOptimization.Models.MaintenanceVer
{
public class TemplateIndexV1
{
private Worksheet worksheet;
///
/// 最大行下标
///
private int LastUsedRowIndex
{
get
{
return worksheet.Rows.LastUsedIndex;
}
}
///
/// 最大列下标
///
private int LastUsedColumnIndex
{
get
{
return worksheet.GetUsedRange().ColumnCount - 1;
}
}
public TemplateIndexV1()
{
DeviceSpecification = new List();
EquipmentID = new List();
Year = new List();
Content = new List();
ExceptionDescription = new List();
Operation = new List();
OperationDate = new List();
}
///
/// 初始化对象
///
/// Worksheet 控件对象
/// 当前计划类型
/// 当前编辑列
public TemplateIndexV1(Worksheet ws)
{
worksheet = ws;
try
{
DeviceSpecification = new List();
EquipmentID = new List();
Year = new List();
Content = new List();
ExceptionDescription = new List();
Operation = new List();
OperationDate = new List();
SheetDataItem FrequencyItem = new SheetDataItem();
for (int rowIndex = 0; rowIndex < LastUsedRowIndex; rowIndex++)
{
for (int colIndex = 0; colIndex < LastUsedColumnIndex; colIndex++)
{
Cell cell = worksheet.Cells[rowIndex, colIndex];
string CellValue = cell.Value.TextValue?.Replace("\n", "")?.Trim();
if (cell.Value.IsText && CellValue == "设备型号:")
{
DeviceSpecification.Add(new SheetDataItem { ColumnIndex = colIndex + 1, RowIndex = rowIndex });
}
else if (cell.Value.IsText && CellValue == "设备编号:")
{
EquipmentID.Add(new SheetDataItem { ColumnIndex = colIndex + 1, RowIndex = rowIndex });
Year.Add(new SheetDataItem { ColumnIndex = colIndex + 2, RowIndex = rowIndex });
}
else if (cell.Value.IsText && CellValue == "频次")
{
FrequencyItem.RowIndex = cell.RowIndex;
FrequencyItem.ColumnIndex = cell.ColumnIndex;
}
else if (worksheet.Cells[rowIndex, 0].Value.IsText && Regex.IsMatch(worksheet.Cells[rowIndex, 0].Value.TextValue.Replace("\n", "").Trim(), @"^[a-zA-Z][1-9]\d*$") && FrequencyItem.ColumnIndex < colIndex)
{
string FrequencyType = worksheet.Cells[rowIndex, FrequencyItem.ColumnIndex].Value.TextValue;
EnumMaintenanceType type = EnumMaintenanceTypeHelper.MatchToEnum(FrequencyType);
string FrequencyValue = worksheet.Cells[FrequencyItem.RowIndex, colIndex].Value.IsText
? worksheet.Cells[FrequencyItem.RowIndex, colIndex].Value.TextValue
: worksheet.Cells[FrequencyItem.RowIndex, colIndex].Value.NumericValue + "";
//如果首列的内容为 单英文开头+ N*数字结束,则判断为需填写的列
if (cell.IsMerged)
{
//获取主单元格位置
int top = (worksheet.Cells[rowIndex, colIndex].GetMergedRanges()[0]).TopRowIndex;
int left = (worksheet.Cells[rowIndex, colIndex].GetMergedRanges()[0]).LeftColumnIndex;
if (!worksheet.Cells[top, left].Value.IsEmpty)
continue;
if (!Content.Any(f => f.RowIndex == top && f.ColumnIndex == left))
Content.Add(new SheetDataItem { ColumnIndex = colIndex, RowIndex = rowIndex, MaintenanceType = type, MaintenanceTypeValue = FrequencyValue });
}
else
{
Content.Add(new SheetDataItem { ColumnIndex = colIndex, RowIndex = rowIndex, MaintenanceType = type, MaintenanceTypeValue = FrequencyValue });
}
}
}
CellValue FirstColValue = worksheet.Cells[rowIndex, 0].Value;
if (FirstColValue.IsText && FirstColValue.TextValue.Replace("\n", "").Trim() == "其它异常处理")
{
//异常处理信息
List cells = worksheet.Cells[rowIndex, 1].GetMergedRanges().ToList();
if (cells != null && cells.Count == 1)
{
string FrequencyType = worksheet.Cells[rowIndex - 1, FrequencyItem.ColumnIndex].Value.TextValue;
EnumMaintenanceType type = EnumMaintenanceTypeHelper.MatchToEnum(FrequencyType);
ExceptionDescription.Add(new SheetDataItem { ColumnIndex = 1, RowIndex = rowIndex, MaintenanceType = type });
//保养签名
FirstColValue = worksheet.Cells[rowIndex, cells[0].RightColumnIndex + 1].Value;
if (FirstColValue.IsText && (FirstColValue.TextValue.Replace("\n", "").Trim() == "保养签名") || FirstColValue.TextValue.Replace("\n", "").Trim() == "保养签字")
{
for (int j = cells[0].RightColumnIndex + 1; j < LastUsedColumnIndex; j++)
{
string FrequencyValue = worksheet.Cells[FrequencyItem.RowIndex, j + 1].Value.IsText
? worksheet.Cells[FrequencyItem.RowIndex, j + 1].Value.TextValue
: worksheet.Cells[FrequencyItem.RowIndex, j + 1].Value.NumericValue + "";
Operation.Add(new SheetDataItem { RowIndex = rowIndex, ColumnIndex = j + 1, MaintenanceType = type, MaintenanceTypeValue = FrequencyValue });
}
}
//保养日期
FirstColValue = worksheet.Cells[rowIndex + 1, cells[0].RightColumnIndex + 1].Value;
if (FirstColValue.IsText && FirstColValue.TextValue.Replace("\n", "").Trim() == "保养日期")
{
for (int j = cells[0].RightColumnIndex + 1; j < LastUsedColumnIndex; j++)
{
string FrequencyValue = worksheet.Cells[FrequencyItem.RowIndex, j + 1].Value.IsText
? worksheet.Cells[FrequencyItem.RowIndex, j + 1].Value.TextValue
: worksheet.Cells[FrequencyItem.RowIndex, j + 1].Value.NumericValue + "";
OperationDate.Add(new SheetDataItem { RowIndex = rowIndex + 1, ColumnIndex = j + 1, MaintenanceType = type, MaintenanceTypeValue = FrequencyValue });
}
}
}
}
}
}
catch (Exception ex)
{
throw;
}
}
///
/// 设备型号规格
///
public List DeviceSpecification { get; set; }
///
/// 设备编号
///
public List EquipmentID { get; set; }
///
/// 年
///
public List Year { get; set; }
///
/// 内容
///
public List Content { get; set; }
///
/// 其他异常处理
///
public List ExceptionDescription { get; set; }
///
/// 操作员
///
public List Operation { get; set; }
///
/// 操作日期
///
public List OperationDate { get; set; }
}
}