398 lines
14 KiB
C#
398 lines
14 KiB
C#
|
using DevExpress.XtraBars;
|
|||
|
using DevExpress.XtraBars.ToolbarForm;
|
|||
|
using DevExpress.XtraEditors;
|
|||
|
using DeviceRepair.Models;
|
|||
|
using DeviceRepair.Models.Enum;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.ComponentModel;
|
|||
|
using System.Data;
|
|||
|
using System.Drawing;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using System.Windows.Forms;
|
|||
|
using DeviceRepair.Utils;
|
|||
|
using DeviceRepairAndOptimization.Common;
|
|||
|
using System.IO;
|
|||
|
using NPOI.HSSF.UserModel;
|
|||
|
using NPOI.XSSF.UserModel;
|
|||
|
using NPOI.SS.UserModel;
|
|||
|
|
|||
|
namespace DeviceRepairAndOptimization.Pages.AM.FormVersion
|
|||
|
{
|
|||
|
public partial class pageFormVersionEdit : ToolbarForm
|
|||
|
{
|
|||
|
public MaintenanceFormVersionInfo CurrentModel;
|
|||
|
public pageFormVersionEdit() : this(new MaintenanceFormVersionInfo { FormBelong = EnumDeviceBelong.AM.ToString() })
|
|||
|
{
|
|||
|
}
|
|||
|
|
|||
|
public pageFormVersionEdit(MaintenanceFormVersionInfo currentModel)
|
|||
|
{
|
|||
|
InitializeComponent();
|
|||
|
CurrentModel = currentModel;
|
|||
|
}
|
|||
|
|
|||
|
private void pageFormVersionEdit_Load(object sender, EventArgs e)
|
|||
|
{
|
|||
|
if (CurrentModel.AutoID != 0)
|
|||
|
{
|
|||
|
txtName.EditValue = CurrentModel.FormName;
|
|||
|
txtName.Enabled = false;
|
|||
|
txtCode.EditValue = CurrentModel.VersionCode;
|
|||
|
txtCode.Enabled = false;
|
|||
|
txtRev.EditValue = CurrentModel.VersionRev;
|
|||
|
txtRev.Enabled = false;
|
|||
|
|
|||
|
txtFilePath.EditValue = Path.GetFileName(CurrentModel.FormPath);
|
|||
|
txtFilePath.Enabled = false;
|
|||
|
|
|||
|
txtVersion.EditValue = CurrentModel.VersionCode;
|
|||
|
txtVersion.Enabled = false;
|
|||
|
|
|||
|
txtNote.EditValue = CurrentModel.Remarks;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 提交
|
|||
|
/// </summary>
|
|||
|
/// <param name="sender"></param>
|
|||
|
/// <param name="e"></param>
|
|||
|
private void btnSave_ItemClick(object sender, ItemClickEventArgs e)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
if (CanCloas())
|
|||
|
{
|
|||
|
if (CurrentModel.AutoID == 0)
|
|||
|
{
|
|||
|
CurrentModel.FormName = txtName.EditValue + "";
|
|||
|
CurrentModel.FormPath = txtFilePath.EditValue + "";
|
|||
|
CurrentModel.FormStatus = true;
|
|||
|
CurrentModel.Remarks = txtNote.EditValue + "";
|
|||
|
CurrentModel.VersionCode = txtCode.EditValue + "";
|
|||
|
CurrentModel.VersionRev = txtRev.EditValue + "";
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
CurrentModel.Remarks = txtNote.EditValue + "";
|
|||
|
}
|
|||
|
this.DialogResult = DialogResult.OK;
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
XtraMessageBoxHelper.Error(ex.Message);
|
|||
|
}
|
|||
|
|
|||
|
this.DialogResult = DialogResult.OK;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 选择文件
|
|||
|
/// </summary>
|
|||
|
/// <param name="sender"></param>
|
|||
|
/// <param name="e"></param>
|
|||
|
private void txtFilePath_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
|
|||
|
{
|
|||
|
if (e.Button.Kind == DevExpress.XtraEditors.Controls.ButtonPredefines.Ellipsis)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
XtraOpenFileDialog xtraOpenFileDialog1 = new XtraOpenFileDialog();
|
|||
|
xtraOpenFileDialog1.FileName = string.Empty;
|
|||
|
xtraOpenFileDialog1.Filter = "(*.xls)|*.xls;*.xlsx";
|
|||
|
xtraOpenFileDialog1.Multiselect = false;
|
|||
|
xtraOpenFileDialog1.Title = "请选择待导入的点检表Excel文件";
|
|||
|
|
|||
|
if (xtraOpenFileDialog1.ShowDialog(this) == DialogResult.OK)
|
|||
|
{
|
|||
|
string FilePath = xtraOpenFileDialog1.FileName;
|
|||
|
splashScreenManager1.ShowWaitForm();
|
|||
|
|
|||
|
using (FileStream fileStream = new FileStream(FilePath, FileMode.Open, FileAccess.Read))
|
|||
|
{
|
|||
|
string fileExtension = Path.GetExtension(FilePath);
|
|||
|
MaintenanceFormVersionInfo entity;
|
|||
|
if (fileExtension.Equals(".xls", StringComparison.OrdinalIgnoreCase))
|
|||
|
{
|
|||
|
// 如果是 .xls 文件
|
|||
|
HSSFWorkbook workbook = new HSSFWorkbook(fileStream);
|
|||
|
entity = ReadHeaderFromWorkbook(workbook);
|
|||
|
}
|
|||
|
else if (fileExtension.Equals(".xlsx", StringComparison.OrdinalIgnoreCase))
|
|||
|
{
|
|||
|
// 如果是 .xlsx 文件
|
|||
|
XSSFWorkbook workbook = new XSSFWorkbook(fileStream);
|
|||
|
entity = ReadHeaderFromWorkbook(workbook);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
// 不支持的文件类型
|
|||
|
throw new NotSupportedException("不支持的文件类型");
|
|||
|
}
|
|||
|
|
|||
|
txtCode.EditValue = entity.VersionCode;
|
|||
|
txtFilePath.EditValue = FilePath;
|
|||
|
txtName.EditValue = entity.FormName;
|
|||
|
txtRev.EditValue = entity.VersionRev;
|
|||
|
}
|
|||
|
|
|||
|
splashScreenManager1.TryCloseWait();
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
splashScreenManager1.TryCloseWait();
|
|||
|
XtraMessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 校验表单
|
|||
|
/// </summary>
|
|||
|
/// <returns></returns>
|
|||
|
private bool CanCloas()
|
|||
|
{
|
|||
|
// 校验点检表文件
|
|||
|
if (CurrentModel.AutoID == 0)
|
|||
|
{
|
|||
|
if ((txtFilePath.EditValue + "").IsNull())
|
|||
|
return false;
|
|||
|
|
|||
|
string FilePath = txtFilePath.EditValue + "";
|
|||
|
if (!System.IO.File.Exists(FilePath))
|
|||
|
return false;
|
|||
|
|
|||
|
CurrentModel.FormPath = FilePath;
|
|||
|
|
|||
|
// 校验说明
|
|||
|
string Note = txtNote.EditValue + "";
|
|||
|
if (!Note.IsNull() && Note.Length > 2000)
|
|||
|
return false;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
string Note = txtNote.EditValue + "";
|
|||
|
if (!Note.IsNull() && Note.Length > 2000)
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 从工作簿中读取页眉
|
|||
|
/// </summary>
|
|||
|
/// <param name="workbook"></param>
|
|||
|
/// <returns></returns>
|
|||
|
private MaintenanceFormVersionInfo ReadHeaderFromWorkbook(IWorkbook workbook)
|
|||
|
{
|
|||
|
MaintenanceFormVersionInfo maintenance = null;
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
// 获取第一个工作表
|
|||
|
ISheet CurrentSheet = null;
|
|||
|
int SheetCount = workbook.NumberOfSheets;
|
|||
|
|
|||
|
if (SheetCount == 0)
|
|||
|
{
|
|||
|
throw new ArgumentException("导入的点检表文件出错,文件中无内容!");
|
|||
|
}
|
|||
|
|
|||
|
CurrentSheet = workbook.GetSheet("Table2");
|
|||
|
if (CurrentSheet == null)
|
|||
|
{
|
|||
|
throw new ArgumentException("导入的点检表文件出错,当前文件中不存在【Table2】的Sheet页!");
|
|||
|
}
|
|||
|
|
|||
|
// 获取页眉
|
|||
|
if (CurrentSheet != null && CurrentSheet.Header != null)
|
|||
|
{
|
|||
|
Tuple<string, string, string> tuple = GetPageHeadVersionInfo(CurrentSheet);
|
|||
|
maintenance = new MaintenanceFormVersionInfo();
|
|||
|
maintenance.FormName = tuple.Item1;
|
|||
|
maintenance.VersionCode = tuple.Item2;
|
|||
|
maintenance.VersionRev = tuple.Item3;
|
|||
|
}
|
|||
|
|
|||
|
// 验证模板内容
|
|||
|
ArgumentException argEx;
|
|||
|
if (maintenance.VersionCode == "FM-P0075.01")
|
|||
|
{
|
|||
|
Biz.AM.Templates.FM_P0075_01 fm = new Biz.AM.Templates.FM_P0075_01();
|
|||
|
argEx = fm.CheckCurrentSheet(CurrentSheet);
|
|||
|
}
|
|||
|
else if (maintenance.VersionCode == "FM-P0075.15")
|
|||
|
{
|
|||
|
Biz.AM.Templates.FM_P0075_15 fm = new Biz.AM.Templates.FM_P0075_15();
|
|||
|
argEx = fm.CheckCurrentSheet(CurrentSheet);
|
|||
|
}
|
|||
|
else if (maintenance.VersionCode == "FM-P0075.17")
|
|||
|
{
|
|||
|
Biz.AM.Templates.FM_P0075_17 fm = new Biz.AM.Templates.FM_P0075_17();
|
|||
|
argEx = fm.CheckCurrentSheet(CurrentSheet);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
throw new ArgumentException($"未适配的点检表编号【{maintenance.VersionCode}】,无法导入!");
|
|||
|
}
|
|||
|
|
|||
|
if (argEx != null)
|
|||
|
throw argEx;
|
|||
|
|
|||
|
return maintenance;
|
|||
|
// int SheetIndex = 0;
|
|||
|
// bool HasContent = false;
|
|||
|
//ReadSheet:
|
|||
|
// CurrentSheet = workbook.GetSheetAt(SheetIndex);
|
|||
|
|
|||
|
// try
|
|||
|
// {
|
|||
|
// if (SheetCount == 0)
|
|||
|
// {
|
|||
|
// throw new ArgumentException("导入的点检表文件出错,文件中无内容!");
|
|||
|
// }
|
|||
|
|
|||
|
// CurrentSheet = workbook.GetSheet("Table2");
|
|||
|
// if (CurrentSheet == null)
|
|||
|
// {
|
|||
|
// throw new ArgumentException("导入的点检表文件出错,当前文件中不存在【Table2】的Sheet页!");
|
|||
|
// }
|
|||
|
|
|||
|
|
|||
|
// // 获取页眉
|
|||
|
// if (CurrentSheet != null && CurrentSheet.Header != null)
|
|||
|
// {
|
|||
|
// Tuple<string, string, string> tuple = GetPageHeadVersionInfo(CurrentSheet);
|
|||
|
// maintenance = new MaintenanceFormVersionInfo();
|
|||
|
// maintenance.FormName = tuple.Item1;
|
|||
|
// maintenance.VersionCode = tuple.Item2;
|
|||
|
// maintenance.VersionRev = tuple.Item3;
|
|||
|
// }
|
|||
|
|
|||
|
|
|||
|
|
|||
|
// return maintenance;
|
|||
|
// }
|
|||
|
// catch (Exception)
|
|||
|
// {
|
|||
|
// if (SheetIndex < SheetCount - 1)
|
|||
|
// {
|
|||
|
// SheetIndex++;
|
|||
|
// goto ReadSheet;
|
|||
|
// }
|
|||
|
|
|||
|
// throw;
|
|||
|
// }
|
|||
|
}
|
|||
|
catch (Exception)
|
|||
|
{
|
|||
|
throw;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 获取页眉右侧单元格
|
|||
|
/// </summary>
|
|||
|
/// <param name="CurrentSheet"></param>
|
|||
|
/// <returns></returns>
|
|||
|
private Tuple<string, string, string> GetPageHeadVersionInfo(ISheet CurrentSheet)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
Func<string, string, string> func = (s1, s2) =>
|
|||
|
{
|
|||
|
if (s2.IsNull())
|
|||
|
s2 = "&\"\\S*?\"";
|
|||
|
|
|||
|
string s3 = System.Text.RegularExpressions.Regex.Replace(s1, s2, string.Empty);
|
|||
|
return s3;
|
|||
|
};
|
|||
|
|
|||
|
//文档名称
|
|||
|
string FormName = CurrentSheet.Header.Center;
|
|||
|
if (!FormName.IsNull())
|
|||
|
FormName = func(FormName, null).Trim();
|
|||
|
|
|||
|
if (FormName.IsNull())
|
|||
|
{
|
|||
|
throw new Exception("操作失败,点检表名称不能为空!");
|
|||
|
}
|
|||
|
|
|||
|
if (FormName.Length > 100)
|
|||
|
{
|
|||
|
throw new Exception("操作失败,点检表名称长度超限,最大长度为100字!");
|
|||
|
}
|
|||
|
|
|||
|
string VersionInfo = CurrentSheet.Header.Right;
|
|||
|
if (!VersionInfo.IsNull())
|
|||
|
{
|
|||
|
string[] infs = func(VersionInfo, null).Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
|
|||
|
if (infs.Length == 3)
|
|||
|
{
|
|||
|
string VersionCode = infs[0].Trim().ToUpper();
|
|||
|
if (VersionCode.IsNull())
|
|||
|
{
|
|||
|
throw new Exception("操作失败,点检表编码版本不能为空!");
|
|||
|
}
|
|||
|
|
|||
|
if (VersionCode != txtVersion.EditValue + "")
|
|||
|
{
|
|||
|
throw new Exception("操作失败,点检表编码版本与选定的编码版本不一致!");
|
|||
|
}
|
|||
|
|
|||
|
string VersionRev = func(infs[1].Replace(":", ":"), "Rev版本:(\\s{0,})").Trim();
|
|||
|
if (VersionRev.IsNull())
|
|||
|
{
|
|||
|
throw new Exception("操作失败,点检表Rev版本不能为空!");
|
|||
|
}
|
|||
|
|
|||
|
if (VersionRev.Length > 50)
|
|||
|
{
|
|||
|
throw new Exception("操作失败,点检表Rev版本长度不能超出50个字符!");
|
|||
|
}
|
|||
|
|
|||
|
return Tuple.Create(FormName, VersionCode, VersionRev);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
throw new Exception("操作失败,点检表文档页眉编号或点检表版本读取失败,格式不正确!");
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
throw new Exception("操作失败,点检表中未检测到页眉数据!!");
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception)
|
|||
|
{
|
|||
|
|
|||
|
throw;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 点检表校验
|
|||
|
/// </summary>
|
|||
|
/// <param name="FilePath"></param>
|
|||
|
private void VerifyFile(string FilePath)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
catch (Exception)
|
|||
|
{
|
|||
|
|
|||
|
throw;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
}
|