public JobList(FileInfo[] files, JobParameterStruct jobParam, Person person) { this.jobList = new List <Job>(); this.jobParam = jobParam; this.isStopping = false; switch (jobParam.ActionType) { case 0: foreach (FileInfo item in files) { AddJob(new GeneratingFormJob(item.FullName, jobParam, person)); } break; case 1: foreach (FileInfo item in files) { AddJob(new GeneratingCertificateJob(item.FullName, jobParam, person)); } break; default: throw new Exception("生成任务时无法识别选择的任务类型!"); } this.jobCount = jobList.Count; }
public Job(string filePath, JobParameterStruct jobParam, Person person) { this.jobParam = jobParam; this.filePath = filePath; this.fileName = Path.GetFileName(filePath); this.person = person; }
public JobList(FileInfo[] files, JobParameterStruct jobParam, Person person) { this.jobList = new List<Job>(); this.jobParam = jobParam; this.isStopping = false; switch (jobParam.ActionType) { case 0: foreach (FileInfo item in files) { AddJob(new GeneratingFormJob(item.FullName, jobParam, person)); } break; case 1: foreach (FileInfo item in files) { AddJob(new GeneratingCertificateJob(item.FullName, jobParam, person)); } break; default: throw new Exception("生成任务时无法识别选择的任务类型!"); } this.jobCount = jobList.Count; }
/// <summary> /// /// </summary> /// <param name="filePath"></param> /// <param name="pattern"></param> /// <param name="text1"></param> /// <param name="text7"></param> /// <param name="text3"></param> /// <param name="problemFilesList"></param> public void VerificateOne(string filePath, JobParameterStruct pS, ref List<string> problemFilesList) { int pattern = pS.DataPattern; Dictionary<int, string> exSheets = new Dictionary<int, string>(); Dictionary<int, string> cert = new Dictionary<int, string>(); List<string> sheetsName = new List<string>(); string fileName = ""; bool pass = false; ExcelUtility _sr = new ExcelUtility(filePath, out checkClear); if (!checkClear) { AddException(@"Excel文档无法打开", true); _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); AddLog(@"***************************************************************", true); problemFilesList.Add(filePath); exceptionNum = 0; dataerrorNum = 0; return; } _sr.ExcelApp.DisplayAlerts = false; _sr.ExcelApp.AlertBeforeOverwriting = false; try { //TODO: 单独校验处校验是否可选 fileName = _sr.Verification(_sr.ExcelWorkbook, true, pattern, out pass); } catch (Exception ex) { AddException(@"校验时遇到异常:" + ex.Message, true); } finally { if (pass) { FileInfo fi = new FileInfo(filePath); fileName = DataUtility.DataUtility.PathRightFileName(fi.DirectoryName, fileName, fi.Extension, "_new"); if (filePath != fileName) { _sr.ExcelWorkbook.SaveAs(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, MSExcel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); Thread.Sleep(1000); File.Delete(filePath); } else { _sr.ExcelWorkbook.Save(); } } _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); if (exceptionNum > 0) { AddLog(@"***************************************************************", true); problemFilesList.Add(filePath); exceptionNum = 0; dataerrorNum = 0; } } }
/// <summary> /// 存档合并模式 /// </summary> /// <param name="filePath">需要处理的文件名</param> /// <param name="pS">处理过程需要的参数集</param> /// <param name="problemFilesList">出问题的文件列表</param> public void ArchievingMergeOne(string filePath, JobParameterStruct pS, ref List<string> problemFilesList) { int pattern = pS.DataPattern; string output = pS.OutputFolder; Dictionary<int, string> exSheets = new Dictionary<int, string>(); bool Perfect; bool isArchieved = false; bool needFix = true; bool shouldFix = true; int stateIndex = -1; int noIdNumber = 0; int startDestiRowIndex = -1; string tempName; string certId; string newName = ""; string fileText = ""; string renameStr; string strCompany = "", strType = "", strMacSerial = "", strSensorSerial = ""; FileInfo temp_fi = null; FileInfo fi = new FileInfo(filePath); Object format = MSExcel.XlFileFormat.xlWorkbookDefault; existFile = (new DirectoryInfo(output)).GetFiles(@"*.xls*", SearchOption.AllDirectories); ExcelUtility _sr = new ExcelUtility(filePath, out checkClear); ExcelUtility _eu = null; MSExcel.Range rr = null; if (!checkClear) { AddException(@"Excel文档无法打开", true); if (_sr != null && _sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } AddLog(@"***************************************************************", true); problemFilesList.Add(filePath); exceptionNum = 0; dataerrorNum = 0; return; } _sr.ExcelApp.DisplayAlerts = false; _sr.ExcelApp.AlertBeforeOverwriting = false; //第一次循环:获取信息,并规范每页的标签 foreach (MSExcel.Worksheet item in _sr.ExcelWorkbook.Sheets) { //规范sheet标签名为证书编号 certId = _sr.GetText(_sr.ExcelWorkbook, item.Index, "L2", out checkClear).Trim(); if (certId.StartsWith(@"20") && (certId.Length == 9 || certId.Length == 10)) { //有规范的证书号 exSheets.Add(item.Index, certId); stateIndex = item.Index; } else { //无规范的证书号 rr = _sr.GetRange(_sr.ExcelWorkbook, item.Index, "A4", out checkClear); if (!item.Name.Contains(@"标准模板") && rr.Text.ToString().Trim().StartsWith(@"送校单位")) { //有记录不包含规范的证书编号 AddException(@"该文档有实验数据不包含证书编号", true); noIdNumber++; } } renameStr = _sr.GetText(_sr.ExcelWorkbook, item.Index, 5, 7, out checkClear).Trim().ToLower(); if (renameStr.StartsWith(@"编号:")) { _sr.WriteValue(_sr.ExcelWorkbook, item.Index, 5, 7, renameStr.Replace(@"编号:", @"主机编号:"), out checkClear); } renameStr = _sr.GetText(_sr.ExcelWorkbook, item.Index, 5, 11, out checkClear).Trim().ToLower(); if (renameStr.StartsWith(@"电离室号:")) { _sr.WriteValue(_sr.ExcelWorkbook, item.Index, 5, 11, renameStr.Replace(@"电离室号:", @"探测器编号:"), out checkClear); } else if (renameStr.StartsWith(@"探测器号:")) { _sr.WriteValue(_sr.ExcelWorkbook, item.Index, 5, 11, renameStr.Replace(@"探测器号:", @"探测器编号:"), out checkClear); } } _sr.ExcelWorkbook.Save(); _sr.ExcelWorkbook.Saved = true; if (exSheets.Count == 0) { if (noIdNumber == 0) { AddException(@"该文档可能是空文档", true); } if (_sr != null && _sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } AddLog(@"***************************************************************", true); problemFilesList.Add(filePath); exceptionNum = 0; dataerrorNum = 0; return; } else if (exSheets.Count + noIdNumber > 1) { AddException(@"该文档包含多个数据sheet,默认处理第一个", true); } certId = exSheets[stateIndex]; tempName = _sr.GenerateFileName(_sr.ExcelWorkbook, stateIndex, out strCompany, out strType, out strMacSerial, out strSensorSerial, out Perfect); checkClear = false; if (strCompany == "") { AddException(@"送校单位信息未提取到", true); checkClear = true; } if (strType == "") { AddException(@"仪器型号信息未提取到", true); checkClear = true; } if (strMacSerial == "" || strMacSerial == "主机_/") { AddException(@"主机编号信息未提取到", true); checkClear = true; } if (checkClear) { AddLog(@"***************************************************************", true); if (_sr != null && _sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } problemFilesList.Add(filePath); exceptionNum = 0; dataerrorNum = 0; return; } GetFixState(_sr, stateIndex, pattern, out needFix, out shouldFix); if (pattern < 2) { startDestiRowIndex = 18; } else if (pattern == 2) { startDestiRowIndex = 17; } else { AddException(@"数据类型无效", true); if (_sr != null && _sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } AddLog(@"***************************************************************", true); problemFilesList.Add(filePath); exceptionNum = 0; dataerrorNum = 0; return; } try { //寻找目标统计文件 temp_fi = SearchForFile(filePath, strType, strMacSerial, strSensorSerial, existFile, out isArchieved); if (temp_fi == null) { if (isArchieved) { //不存在,复制当前记录过去 //1.合成新文件名,按新名另存,定义为temp_fi newName = DataUtility.DataUtility.PathCombineFolderFileExtension(output, DataUtility.DataUtility.FileNameCleanName(tempName), fi.Extension); fileText = newName; _sr.ExcelWorkbook.SaveAs(newName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, MSExcel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); temp_fi = new FileInfo(newName); //2.统计新文件 Statistic(_sr, pattern, Perfect, strCompany, strType, tempName, certId); //3.对新文件进行校验 _sr.Verification(_sr.ExcelWorkbook, true, pattern, out checkClear); } else { //不存在,但搜索到有可疑项,暂不处理 fileText = filePath; } } else if (File.Exists(temp_fi.FullName)) { fileText = temp_fi.FullName; _eu = new ExcelUtility(temp_fi.FullName, out checkClear); if (!checkClear) { AddException(@"Excel文档无法打开", true); _sr.ExcelWorkbook.Saved = true; _eu.ExcelWorkbook.Saved = true; _sr.TryClose(); _eu.TryClose(); AddLog(@"***************************************************************", true); problemFilesList.Add(filePath); exceptionNum = 0; dataerrorNum = 0; return; } _eu.ExcelApp.DisplayAlerts = false; _eu.ExcelApp.AlertBeforeOverwriting = false; CopyData(_sr, stateIndex, _eu, pattern, certId, needFix, shouldFix, startDestiRowIndex, out checkClear); if (checkClear) { _eu.ExcelWorkbook.Save(); _eu.ExcelWorkbook.Saved = true; Statistic(_eu, pattern, Perfect, strCompany, strType, tempName, certId); _eu.ExcelWorkbook.Save(); _eu.ExcelWorkbook.Saved = true; //TODO: 存档合并处校验是否可选(存在,合并入原记录) _eu.Verification(_eu.ExcelWorkbook, false, pattern, out checkClear); } } else { AddException(@"文件不存在:" + temp_fi.FullName, true); fileText = filePath; } } catch (Exception ex) { AddException(@"存档合并时遇到异常:" + ex.Message, true); } finally { if (_sr != null && _sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } if (_eu != null && _eu.ExcelWorkbook != null) { _eu.ExcelWorkbook.Saved = true; _eu.TryClose(); } if (exceptionNum > 0) { problemFilesList.Add(fileText); AddLog(@"***************************************************************", true); exceptionNum = 0; dataerrorNum = 0; } else { if (dataerrorNum > 0) { AddLog(@"该文件只有非致命错误,已经成功处理", true); AddLog(@"***************************************************************", true); exceptionNum = 0; dataerrorNum = 0; } File.Delete(filePath); } } }
public void StandardizeOne(string filePath, JobParameterStruct pS, ref List<string> problemFilesList) { int pattern = pS.DataPattern; string output = pS.OutputFolder; string templateName = pS.DataTemplateFilePath; Dictionary<int, string> exSheets = new Dictionary<int, string>(); Dictionary<int, string> cert = new Dictionary<int, string>(); List<string> sheetsName = new List<string>(); int stateIndex = -1; int noIdNumber = 0; int startDestiRowIndex = -1; bool Perfect; bool needFix = true; bool shouldFix = true; bool hasTemplate = false; bool canDelete = false; string year = ""; string certId; string tempName; string text; string temp_strCompany = "", temp_strType = "", temp_strMacSerial = "", temp_strSensorSerial = ""; string filePathNew = Path.Combine(Path.GetDirectoryName(filePath), Path.GetFileNameWithoutExtension(filePath) + "_处理中" + Path.GetExtension(filePath)); FileInfo temp_fi = null; File.Move(filePath, filePathNew); ExcelUtility _sr = new ExcelUtility(filePathNew, out checkClear); if (!checkClear) { AddException(@"Excel文档无法打开", true); if (_sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } AddLog(@"***************************************************************", true); problemFilesList.Add(filePath); exceptionNum = 0; dataerrorNum = 0; return; } _sr.ExcelApp.DisplayAlerts = false; _sr.ExcelApp.AlertBeforeOverwriting = false; MSExcel.Range rr = null; sheetsName = new List<string>(); exSheets = new Dictionary<int, string>(); //第一次循环:获取信息,并规范每页的标签 foreach (MSExcel.Worksheet item in _sr.ExcelWorkbook.Sheets) { if (!hasTemplate && item.Name == @"标准模板") { hasTemplate = true; } //规范sheet标签名为证书编号 certId = _sr.GetText(_sr.ExcelWorkbook, item.Index, "L2", out checkClear).Trim(); if (certId.StartsWith(@"20") && (certId.Length == 9 || certId.Length == 10)) { //有规范的证书号 exSheets.Add(item.Index, certId); stateIndex = item.Index; } else { //无规范的证书号 rr = _sr.GetRange(_sr.ExcelWorkbook, item.Index, "A4", out checkClear); if (!item.Name.Contains(@"标准模板") && rr.Text.ToString().Trim().StartsWith(@"送校单位")) { //有记录不包含规范的证书编号 AddException(@"该文档有实验数据不包含证书编号", true); noIdNumber++; } } } if (exSheets.Count == 0) { if (noIdNumber == 0) { AddException(@"该文档可能是空文档", true); } if (_sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } AddLog(@"***************************************************************", true); problemFilesList.Add(filePath); exceptionNum = 0; dataerrorNum = 0; return; } else if (exSheets.Count + noIdNumber > 1) { AddException(@"该文档包含多个数据sheet,默认处理第一个", true); } TypeStandardize(_sr, stateIndex); certId = exSheets[stateIndex]; year = certId.Substring(0, 4); tempName = year + "_" + _sr.GenerateFileName(_sr.ExcelWorkbook, stateIndex, out temp_strCompany, out temp_strType, out temp_strMacSerial, out temp_strSensorSerial, out Perfect); if (temp_strCompany == "") AddException(@"送校单位信息未提取到", true); if (temp_strType == "") AddException(@"仪器型号信息未提取到", true); if ((temp_strSensorSerial.Length + temp_strMacSerial.Length) == 0) AddException(@"主机编号和探头编号信息未提取到", true); if (temp_strMacSerial == "" || temp_strMacSerial == "/") AddException(@"主机编号信息未提取到", true); GetFixState(_sr, stateIndex, pattern, out needFix, out shouldFix); switch (pattern) { case 0: //TODO : 剂量 更改路径 startDestiRowIndex = 18; //templateName = DataUtility.DataUtility.PathClassifiedCombineName(Application.StartupPath + @"\试验记录模板", pattern, @"剂量证书实验记录模板.xlsx"); break; case 1: //TODO : CT 更改路径 startDestiRowIndex = 18; //templateName = DataUtility.DataUtility.PathClassifiedCombineName(Application.StartupPath + @"\试验记录模板", pattern, @"ct证书实验记录模板.xlsx"); break; case 2: //TODO : KV 更改路径 startDestiRowIndex = 17; //templateName = DataUtility.DataUtility.PathClassifiedCombineName(Application.StartupPath + @"\试验记录模板", pattern, @"KV校准证书实验记录模板.xlsx"); break; default: AddException(@"数据类型无效", true); if (_sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } AddLog(@"***************************************************************", true); problemFilesList.Add(filePath); exceptionNum = 0; dataerrorNum = 0; return; } if (File.Exists(templateName)) { tempName = DataUtility.DataUtility.PathRightFileName(output, tempName, ".xlsx", "_new"); File.Copy(templateName, tempName, true); if (!File.Exists(tempName)) { AddException(@"拷贝模板失败", true); if (_sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } AddLog(@"***************************************************************", true); problemFilesList.Add(filePath); exceptionNum = 0; dataerrorNum = 0; return; } else { temp_fi = new FileInfo(tempName); } } else { AddException(@"模板不存在:" + templateName, true); if (_sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } AddLog(@"***************************************************************", true); problemFilesList.Add(filePath); exceptionNum = 0; dataerrorNum = 0; return; } exSheets.Clear(); ExcelUtility _eu = new ExcelUtility(temp_fi.FullName, out checkClear); if (!checkClear) { AddException(@"Excel文档无法打开", true); if (_sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } if (_eu.ExcelWorkbook != null) { _eu.ExcelWorkbook.Saved = true; _eu.TryClose(); } File.Delete(temp_fi.FullName); AddLog(@"***************************************************************", true); problemFilesList.Add(filePath); exceptionNum = 0; dataerrorNum = 0; return; } _eu.ExcelApp.DisplayAlerts = false; _eu.ExcelApp.AlertBeforeOverwriting = false; Object format = MSExcel.XlFileFormat.xlWorkbookDefault; try { int newSheetIndex = -1; CopyData(_sr, stateIndex, _eu, pattern, certId, needFix, shouldFix, startDestiRowIndex, out newSheetIndex, out checkClear); if (newSheetIndex > 0 && checkClear) { bool testflag; text = _eu.GetText(_eu.ExcelWorkbook, newSheetIndex, 2, 11, out testflag); if (!text.ToLower().EndsWith("dyjl")) { AddException("证书编号不以DYjl开始", true); } if (certId.StartsWith("200")) { _eu.WriteValue(_eu.ExcelWorkbook, newSheetIndex, 2, 11, "证书编号:DYjx", "", out testflag); } } else { AddException(@"无法找到新合并入数据的位置", true); } _eu.ExcelWorkbook.Save(); } catch (Exception ex) { AddException(@"标准化时遇到异常:" + ex.Message, true); } finally { if (_sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } if (_eu.ExcelWorkbook != null) { _eu.ExcelWorkbook.Saved = true; _eu.TryClose(); } if (exceptionNum > 0) { File.Delete(temp_fi.FullName); AddLog(@"***************************************************************", true); problemFilesList.Add(filePath); exceptionNum = 0; dataerrorNum = 0; } else { canDelete = true; } } if (canDelete) { try { File.Delete(filePathNew); } catch (Exception ex) { AddDataError("已处理完成,但原文件无法正常删除!遇到异常:" + ex.Message, true); } } }
public void GeneratingCertificateOne(string filePath, JobParameterStruct pS, ref List<string> problemFilesList) { int pattern = pS.DataPattern; string output = pS.AutoOutputFolder; string ext = pS.AutoExtension; bool createNew = pS.CreateNew; string tempFo = pS.TempFolder; Dictionary<int, string> exSheets = new Dictionary<int, string>(); bool Perfect; bool hasArchieved = false; bool needFix = true; bool shouldFix = true; bool needTestGeCe = true, canGeCe = false; int stateIndex = -1; int noIdNumber = 0; int startDestiRowIndex = -1; string tempName; string certId; string newName = ""; string fileText = ""; string strCompany = "", strType = "", strMacSerial = "", strSensorSerial = ""; string backupKey = ""; FileInfo temp_fi = null; FileInfo fi = new FileInfo(filePath); Object format = MSExcel.XlFileFormat.xlWorkbookDefault; ExcelUtility _sr = new ExcelUtility(filePath, out checkClear); ExcelUtility _eu = null; MSExcel.Range rr = null; if (!checkClear) { AddException(@"Excel文档无法打开", true); if (_sr != null && _sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } AddLog(@"***************************************************************", true); problemFilesList.Add(filePath); exceptionNum = 0; dataerrorNum = 0; return; } _sr.ExcelApp.DisplayAlerts = false; _sr.ExcelApp.AlertBeforeOverwriting = false; //第一次循环:获取信息,并规范每页的标签 foreach (MSExcel.Worksheet item in _sr.ExcelWorkbook.Sheets) { //规范sheet标签名为证书编号 certId = _sr.GetText(_sr.ExcelWorkbook, item.Index, "L2", out checkClear).Trim(); if (certId.StartsWith(@"20") && (certId.Length == 9 || certId.Length == 10)) { //有规范的证书号 exSheets.Add(item.Index, certId); stateIndex = item.Index; } else { //无规范的证书号 rr = _sr.GetRange(_sr.ExcelWorkbook, item.Index, "A4", out checkClear); if (!item.Name.Contains(@"标准模板") && rr.Text.ToString().Trim().StartsWith(@"送校单位")) { //有记录不包含规范的证书编号 AddException(@"该文档有实验数据不包含证书编号", true); noIdNumber++; } } } if (exSheets.Count == 0) { if (noIdNumber == 0) { AddException(@"该文档可能是空文档", true); } if (_sr != null && _sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } AddLog(@"***************************************************************", true); problemFilesList.Add(filePath); exceptionNum = 0; dataerrorNum = 0; return; } else if (exSheets.Count + noIdNumber > 1) { AddException(@"该文档包含多个数据sheet,默认处理第一个", true); } certId = exSheets[stateIndex]; tempName = _sr.GenerateFileName(_sr.ExcelWorkbook, stateIndex, out strCompany, out strType, out strMacSerial, out strSensorSerial, out Perfect); checkClear = false; if (strCompany == "") { AddException(@"送校单位信息未提取到", true); checkClear = true; } if (strType == "") { AddException(@"仪器型号信息未提取到", true); checkClear = true; } if (strMacSerial == "") { AddException(@"主机编号信息未提取到", true); checkClear = true; } if (checkClear) { AddLog(@"***************************************************************", true); if (_sr != null && _sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } problemFilesList.Add(filePath); exceptionNum = 0; dataerrorNum = 0; return; } GetFixState(_sr, stateIndex, pattern, out needFix, out shouldFix); //判断目标行数 if (pattern < 2) { startDestiRowIndex = 18; } else if (pattern == 2) { //TODO: kV修正项 } else { AddException(@"数据类型无效", true); if (_sr != null && _sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } AddLog(@"***************************************************************", true); problemFilesList.Add(filePath); exceptionNum = 0; dataerrorNum = 0; return; } try { //寻找目标统计文件 existFile = GetFilesFromType(output, _sr.GetText(_sr.ExcelWorkbook, stateIndex, "F5", out checkClear), ext, out checkClear); if (!checkClear) { if (_sr != null && _sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } AddLog(@"***************************************************************", true); problemFilesList.Add(filePath); exceptionNum = 0; dataerrorNum = 0; return; } //output = Path.Combine(output, _sr.GetText(_sr.ExcelWorkbook, stateIndex, "F5", out checkClear)); //if (!Directory.Exists(output)) //{ // AddException("无法识别的仪器类型:" + _sr.GetText(_sr.ExcelWorkbook, stateIndex, "F5", out checkClear), true); // if (_sr != null && _sr.ExcelWorkbook != null) // { // _sr.ExcelWorkbook.Saved = true; // _sr.TryClose(); // } // AddLog(@"***************************************************************", true); // problemFilesList.Add(filePath); // exceptionNum = 0; // dataerrorNum = 0; // return; //} //existFile = (new DirectoryInfo(output)).GetFiles(@"*.xls*", SearchOption.AllDirectories); temp_fi = SearchForFile(filePath, strType, strMacSerial, strSensorSerial, existFile, out hasArchieved); if (temp_fi == null) { if (createNew) { //对新记录建档 needTestGeCe = false; if (hasArchieved) { //不存在,复制当前记录过去 newName = DataUtility.DataUtility.PathRightFileName(output, tempName, fi.Extension, "_new"); _sr.ExcelWorkbook.SaveAs(newName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, MSExcel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); temp_fi = new FileInfo(newName); Statistic(_sr, pattern, Perfect, strCompany, strType, tempName, certId); //TODO: 生成证书处校验是否可选(不存在,复制当前记录过去) _sr.Verification(_sr.ExcelWorkbook, true, pattern, out checkClear); fileText = newName; } else { //不存在,但搜索到有可疑项,暂不处理 fileText = filePath; } } else { //对新记录不建档,退出 AddException("没有在历史数据记录中发现匹配项,暂不处理。", true); if (_sr != null && _sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } AddLog(@"***************************************************************", true); problemFilesList.Add(filePath); exceptionNum = 0; dataerrorNum = 0; return; } } else if (File.Exists(temp_fi.FullName)) { DataUtility.DataUtility.BackupFile(tempFo, temp_fi.FullName, out backupKey); _eu = new ExcelUtility(temp_fi.FullName, out checkClear); if (!checkClear) { AddException(@"Excel文档无法打开", true); if (_sr != null && _sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } if (_eu != null && _eu.ExcelWorkbook != null) { _eu.ExcelWorkbook.Saved = true; _eu.TryClose(); } AddLog(@"***************************************************************", true); problemFilesList.Add(filePath); exceptionNum = 0; dataerrorNum = 0; return; } _eu.ExcelApp.DisplayAlerts = false; _eu.ExcelApp.AlertBeforeOverwriting = false; CopyData(_sr, stateIndex, _eu, pattern, certId, needFix, shouldFix, startDestiRowIndex, out checkClear); if (!checkClear) { if (_sr != null && _sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } if (_eu != null && _eu.ExcelWorkbook != null) { _eu.ExcelWorkbook.Saved = true; _eu.TryClose(); } problemFilesList.Add(fileText); AddLog(@"***************************************************************", true); exceptionNum = 0; dataerrorNum = 0; return; } _eu.ExcelWorkbook.Save(); _eu.ExcelWorkbook.Saved = true; canGeCe = Statistic(_eu, pattern, Perfect, strCompany, strType, tempName, certId); _eu.ExcelWorkbook.Save(); _eu.ExcelWorkbook.Saved = true; //TODO: 生成证书处校验是否可选(存在,合并入原记录) _eu.Verification(_eu.ExcelWorkbook, false, pattern, out checkClear); fileText = temp_fi.FullName; } else { AddException(@"文件不存在:" + temp_fi.FullName, true); fileText = filePath; } } catch (Exception ex) { AddException(@"生成证书时合并一步遇到异常:" + ex.Message, true); } try { //有重大失误,关闭两个excel,报错退出。没有失误继续运行 if (exceptionNum > 0) { if (_sr != null && _sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } if (_eu != null && _eu.ExcelWorkbook != null) { _eu.ExcelWorkbook.Saved = true; _eu.TryClose(); } problemFilesList.Add(fileText); AddLog(@"***************************************************************", true); exceptionNum = 0; dataerrorNum = 0; } else { //有以前记录,需要看是否超差 并且 超差不通过 并且 超差不通过的提示选择不生成证书 时选择退出 if (needTestGeCe && !canGeCe && (MessageBox.Show("检测到有数据超差,是否继续生成证书?", "问题", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)) { AddException("有实验数据超差,暂时保留原记录,不做任何处理。", true); if (_sr != null && _sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } if (_eu != null && _eu.ExcelWorkbook != null) { _eu.ExcelWorkbook.Saved = true; _eu.TryClose(); } DataUtility.DataUtility.RestoreFile(tempFo, backupKey); problemFilesList.Add(fileText); AddLog(@"***************************************************************", true); exceptionNum = 0; dataerrorNum = 0; } else { bool success = false; //关闭归档后的Excel if (_eu != null && _eu.ExcelWorkbook != null) { _eu.ExcelWorkbook.Saved = true; _eu.TryClose(); } //新文档因为将sr另存为合并后的文档,需要关闭后重新打开原文档 if (!needTestGeCe) { //关闭原来另存为成合并好的excel文件 if (_sr != null && _sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } //重新打开待合并的excel文件 _sr = new ExcelUtility(filePath, out checkClear); if (!checkClear) { AddException(@"Excel文档无法打开", true); if (_sr != null && _sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } AddLog(@"***************************************************************", true); problemFilesList.Add(filePath); exceptionNum = 0; dataerrorNum = 0; return; } //打开成功 _sr.ExcelApp.DisplayAlerts = false; _sr.ExcelApp.AlertBeforeOverwriting = false; } //找到原记录里的数据页,记下序号 stateIndex = -1; string temp = ""; foreach (MSExcel.Worksheet item in _sr.ExcelWorkbook.Sheets) { temp = _sr.GetText(_sr.ExcelWorkbook, item.Index, "L2", out checkClear); if (certId == item.Name || certId == temp) { stateIndex = item.Index; } } //找到序号的话,加入校核人的签名,删除其他sheet if (stateIndex > 0 && stateIndex < _sr.ExcelWorkbook.Worksheets.Count) { _sr.WriteImage(_sr.ExcelWorkbook, stateIndex, 29, 9, person, 45, 28, out success); for (int i = _sr.ExcelWorkbook.Worksheets.Count; i > 0; i--) { if (i != stateIndex) { ((MSExcel.Worksheet)_sr.ExcelApp.ActiveWorkbook.Sheets[i]).Delete(); } } _sr.ExcelWorkbook.Save(); _sr.ExcelWorkbook.Saved = true; } //出证书 GenerateCert(_sr, stateIndex, pS.DataPattern, pS.CertTemplateFilePath, pS.CertFolder, pS.PDFDataFolder, pS.TempFolder, shouldFix, out success); if (!success) { AddException("生成证书失败", true); if (_sr != null && _sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } problemFilesList.Add(fileText); AddLog(@"***************************************************************", true); exceptionNum = 0; dataerrorNum = 0; } else { if (_sr != null && _sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } File.Delete(filePath); } } } } catch (Exception ex) { AddException(@"生成证书时遇到异常:" + ex.Message, true); } if (dataerrorNum > 0) { dataerrorNum = 0; AddLog(@"***************************************************************", true); } }
//public void DoWork(JobParameterStruct pS) //{ // string inputFi = pS.InputFile; // string inputFo = pS.AutoInputFolder; // string outputFo = pS.AutoOutputFolder; // string inputEx = pS.AutoExtension; // int cbsi = pS.DataPattern; // if (pS.ActionType < 0 || pS.ActionType > 4) // { // if (MessageBox.Show(@"没有选择任何操作类型,请点击确定后返回重试", "提示", MessageBoxButtons.OK) == DialogResult.OK) { } // } // else if (pS.ActionType < 1 && (pS.FixType < 0 || pS.FixType > 1)) // { // if (MessageBox.Show(@"没有选择温度气压修正选项,请点击确定后返回重试", "提示", MessageBoxButtons.OK) == DialogResult.OK) { } // } // else if (MessageBox.Show(@"处理成功将会删除原纪录,是否要继续?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes) // { // List<string> probfile = new List<string>(); // UpdateProgress(0); // if (inputFi != "") // { // if (!inputFi.Contains(@"~$")) // { // currentFile = inputFi; // ToolStripStatusLabel_SetText(new FileInfo(inputFi).Name); // try // { // dataerrorNum = 0; // exceptionNum = 0; // dod(inputFi, pS, ref probfile); // } // catch (Exception ex) // { // probfile.Add(inputFi); // if (exceptionNum > 0) // { // exceptionNum = 0; // } // else // { // AddLog(@"***************************************************************", true); // } // AddLog(@"* 文件名称:" + inputFi, true); // AddLog(@"* 异常消息:" + ex.Message, true); // AddLog(@"* 异常方法:" + ex.TargetSite, true); // AddLog(@"***************************************************************", true); // } // if (dataerrorNum > 0) // { // dataerrorNum = 0; // AddLog(@"***************************************************************", true); // } // ToolStripStatusLabel_SetText(""); // } // UpdateProgress(100); // if (probfile.Count > 0) // { // AddLog(@"结果02", "共处理文件数:1", true); // AddLog(@"结果03", "异常文件总数:" + probfile.Count, true); // foreach (string item in probfile) // { // AddLog(@"结果04", item, true); // } // } // else // { // AddLog(@"结果05", "没有发现任何异常,共处理文件数:1", true); // } // MessageBox.Show("共处理文件" + "1" + "个。" + Environment.NewLine + "其中成功" + (1 - probfile.Count).ToString() + "个,失败" + probfile.Count + "个。", "处理完成", MessageBoxButtons.OK); // } // else if (inputFo != "") // { // DirectoryInfo di = new DirectoryInfo(inputFo); // FileInfo[] fis = di.GetFiles(inputEx, SearchOption.AllDirectories); // probfile.Clear(); // int doneNumber = 0; // if (fis.Length > 0) // { // double Step = 100.0 / (double)fis.Length; // double values = 0; // foreach (FileInfo item in fis) // { // if (!item.Name.StartsWith(@"~$")) // { // currentFile = item.FullName; // ToolStripStatusLabel_SetText(new FileInfo(item.FullName).Name); // try // { // dataerrorNum = 0; // exceptionNum = 0; // dod(item.FullName, pS, ref probfile); // } // catch (Exception ex) // { // probfile.Add(item.FullName); // if (exceptionNum > 0) // { // exceptionNum = 0; // } // else // { // AddLog(@"***************************************************************", true); // } // AddLog(@"* 文件名称:" + item.FullName, true); // AddLog(@"* 异常消息:" + ex.Message, true); // AddLog(@"* 异常方法:" + ex.TargetSite, true); // AddLog(@"***************************************************************", true); // } // if (dataerrorNum > 0) // { // dataerrorNum = 0; // AddLog(@"***************************************************************", true); // } // ToolStripStatusLabel_SetText(""); // } // values += Step; // UpdateProgress(values); // doneNumber++; // if (isStopping) // { // break; // } // } // if (probfile.Count > 0) // { // AddLog(@"结果06", "异常文件总数:" + probfile.Count, true); // foreach (string item in probfile) // { // AddLog(@"结果07", item, true); // } // } // else // { // AddLog(@"结果08", "没有发现任何异常,共处理文件数:" + doneNumber, true); // } // MessageBox.Show("共处理文件" + fis.Length + "个。" + Environment.NewLine + "其中成功" + (fis.Length - probfile.Count).ToString() + "个,失败" + probfile.Count + "个。", "处理完成", MessageBoxButtons.OK); // } // else // { // MessageBox.Show(@"输入文件夹没有找到待处理的文件"); // } // } // else // { // MessageBox.Show(@"未指定输入文件"); // } // } // isWorking = false; // isStopping = false; // Button_SetText(@"开始"); // AddLog(Environment.NewLine + Environment.NewLine, true); //} ///// <summary> ///// pattern 1 KV ///// pattern 2 Dose ///// pattern 3 CT ///// pattern 4 ///// </summary> ///// <param name="filePath"></param> ///// <param name="pattern"></param> //public void StatisticsOne(string filePath, int pattern, string text1, string text7, string text3, ref List<string> problemFilesList) //{ // Dictionary<int, string> exSheets = new Dictionary<int, string>(); // Dictionary<int, string> cert = new Dictionary<int, string>(); // List<string> sheetsName = new List<string>(); // bool Perfect; // int stateIndex = -1; // FileInfo temp_fi = null; // int lineIndex = 8; // int countIndex = 0; // bool firstTime = true; // bool needInitialState = true; // string tempName; // string insNumber = ""; // string standardInsNumber = ""; // string sourceDirectory = "";//, temp_text3 = ""; // string strCompany = "", strType = "", strMacSerial = "", strSensorSerial = ""; // string temp_strCompany = "", temp_strType = "", temp_strMacSerial = "", temp_strSensorSerial = ""; // ExcelUtility _eu = new ExcelUtility(filePath, out checkClear); // if (!checkClear) { return; } // _eu.ExcelApp.DisplayAlerts = false; // _eu.ExcelApp.AlertBeforeOverwriting = false; // Object format = MSExcel.XlFileFormat.xlWorkbookDefault; // FileInfo fi = new FileInfo(_eu.path); // //输入文件的目录 // if (text1 == "") // { // sourceDirectory = fi.DirectoryName.Replace(text7, text3) + @"\"; // } // else // { // sourceDirectory = text3 + @"\"; // } // try // { // //1.初始化:规范统计sheet结构,写入固定内容 // stateIndex = _eu.InitialStatisticSheet(out exSheets, out needInitialState, out checkClear); // if (!checkClear) { return; } // _eu.WriteTitle(pattern, stateIndex); // switch (pattern) // { // case 0: // //2.搬运数据 // countIndex = 0; // foreach (int item in exSheets.Keys) // { // countIndex++; // cert.Add(lineIndex, exSheets[item]); // _eu.CopyOneYearData(item, stateIndex, lineIndex, exSheets[item], out insNumber); // tempName = _eu.PerfectFileName(item, out temp_strCompany, out temp_strType, out temp_strMacSerial, out temp_strSensorSerial, out Perfect); // if (temp_strCompany.Length > strCompany.Length) // { // strCompany = temp_strCompany; // } // if (temp_strMacSerial.Length > strMacSerial.Length) // { // strMacSerial = temp_strMacSerial; // } // if (temp_strType.Length > strType.Length) // { // strType = temp_strType; // } // if (temp_strSensorSerial.Length > strSensorSerial.Length) // { // strSensorSerial = temp_strSensorSerial; // } // if (countIndex == 1) // { // standardInsNumber = insNumber; // } // else // { // if (GetPureNumber(standardInsNumber, true) != GetPureNumber(insNumber, true)) // { // AddException(cert[8] + "和" + cert[lineIndex] + "的仪器编号不同:", true); // AddLog(@"错误", " " + cert[8] + @":" + standardInsNumber, true); // AddLog(@"错误", " " + cert[lineIndex] + @":" + insNumber, true); // } // } // if (firstTime && Perfect) // { // _eu.DesiredName = CleanName(tempName); // //写入送校单位 // _eu.WriteValue(stateIndex, 2, 2, strCompany, out checkClear); // //写入仪器名称 // _eu.WriteValue(stateIndex, 3, 2, strType, out checkClear); // firstTime = false; // } // lineIndex++; // } // if (firstTime) // { // //写入送校单位 // _eu.WriteValue(stateIndex, 2, 2, strCompany, out checkClear); // //写入仪器名称 // _eu.WriteValue(stateIndex, 3, 2, strType, out checkClear); // //文件名 // _eu.DesiredName = CleanName(strCompany + "_" + strType + "_" + strMacSerial + ((strSensorSerial.Replace(@"/", "").Trim() != "") ? "+" + strSensorSerial : "")); // } // //3.分析数据 // if (exSheets.Count > 1) // { // _eu.WriteValue(stateIndex, lineIndex, 1, @"重复性", out checkClear); // //计算平均值和年稳定性 // _eu.StatisticsOneColumn(stateIndex, lineIndex, 4, cert, "RQR4(60kV)", exSheets.Count > 1); // _eu.StatisticsOneColumn(stateIndex, lineIndex, 6, cert, "RQR5(70kV)", exSheets.Count > 1); // _eu.StatisticsOneColumn(stateIndex, lineIndex, 8, cert, "RQR6(80kV)", exSheets.Count > 1); // _eu.StatisticsOneColumn(stateIndex, lineIndex, 10, cert, "RQR8(100kV)", exSheets.Count > 1); // _eu.StatisticsOneColumn(stateIndex, lineIndex, 12, cert, "RQR9(120kV)", exSheets.Count > 1); // } // if (exceptionNum > 0) // { // problemFilesList.Add(_eu.DesiredName); // } // break; // case 1: // break; // } // temp_fi = new FileInfo(RightFileName(sourceDirectory, _eu.DesiredName, fi.Extension, "-new")); // if (!temp_fi.Directory.Exists) // { // Directory.CreateDirectory(temp_fi.DirectoryName); // } // _eu.ExcelDoc.SaveAs(temp_fi.FullName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, MSExcel.XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); // } // catch (Exception ex) // { // exceptionNum++; // if (exceptionNum == 1) // { // AddLog(@"信息", "原文件名:" + filePath, true); // } // AddLog(@"异常", ex.Message, true); // AddLog(@"异常", " " + ex.TargetSite.ToString(), true); // } // finally // { // //移动前的输出文件路径 // sourceDirectory = sourceDirectory + _eu.DesiredName + fi.Extension; // _eu.ExcelDoc.Saved = true; // _eu.TryClose(); // if (exceptionNum > 0) // { // //移动后的输出文件路径 // tempName = RightFileName(Application.StartupPath + @"\有问题整理后\", _eu.DesiredName, fi.Extension, "-new"); // AddLog(@"信息", "新文件名:" + tempName, true); // AddLog(@"***************************************************************", true); // if (File.Exists(sourceDirectory)) File.Move(sourceDirectory, tempName); // if (text1 == "") // { // temp_fi = new FileInfo(fi.FullName.Replace(text7, Application.StartupPath + @"\有问题")); // if (!temp_fi.Directory.Exists) // { // Directory.CreateDirectory(temp_fi.DirectoryName); // } // File.Copy(fi.FullName, temp_fi.FullName, true); // } // else // { // File.Copy(fi.FullName, Application.StartupPath + @"\有问题\" + fi.Name + fi.Extension, true); // } // } // } //} #endregion #region TestingFunction #region GeneratingForm public void GeneratingForm(string filePath, JobParameterStruct pS, ref List<string> problemFilesList) { //int pattern = pS.DataPattern; string output = pS.AutoOutputFolder; bool needFix = pS.AutoFixType; string templateName = pS.DataTemplateFilePath; string macType = pS.MacType; bool success = true; int templateIndex = -1; MSExcel.Worksheet ws1 = null; WordUtility _wu = new WordUtility(filePath, out success); if (!success) { AddException("Word文档打开失败", true); return; } string tempName = _wu.GetText(_wu.WordDocument, 7);//B4:送校单位 string tempSerial = _wu.GetText(_wu.WordDocument, 15).Trim();//F5:仪器型号 //string tempNum = _wu.GetText(_wu.WordDocument, 19);//*H5:仪器编号 string tempQiju = _wu.GetText(_wu.WordDocument, 11);//B5:仪器名称 string tempZhsh = _wu.GetText(_wu.WordDocument, 3);//L2:证书编号 string tempStress = _wu.GetText(_wu.WordDocument, 27);//F4:联系地址 _wu.TryClose(); if (tempSerial != "" && tempSerial != macType) { AddDataError("证书中包含的仪器型号与指定的仪器型号不符", true); } string str = tempZhsh.Substring(8); string strSavename = Path.Combine(output, tempName + "_" + macType + "_" + str + ".xlsx"); if (File.Exists(strSavename)) { if (MessageBox.Show(@"文件已存在,是否覆盖?" + Environment.NewLine + strSavename, "提示", MessageBoxButtons.YesNo) == DialogResult.Yes) { File.Delete(strSavename); } else { success = false; return; } } File.Copy(templateName, strSavename); ExcelUtility _sr = new ExcelUtility(strSavename, out checkClear); if (!checkClear) { AddException(@"Excel文档无法打开", true); if (_sr != null && _sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } AddLog(@"***************************************************************", true); problemFilesList.Add(filePath); exceptionNum = 0; dataerrorNum = 0; return; } _sr.ExcelApp.DisplayAlerts = false; _sr.ExcelApp.AlertBeforeOverwriting = false; try { foreach (MSExcel.Worksheet item in _sr.ExcelWorkbook.Sheets) { if (item.Name == @"标准模板") { templateIndex = item.Index; } else if (item.Name.Contains(@"标准模板")) { AddException(@"发现多余的标准模板", true); } } if (templateIndex > -1) { ws1 = (MSExcel.Worksheet)_sr.ExcelWorkbook.Sheets[templateIndex]; ws1.Copy(ws1, Type.Missing); ws1 = (MSExcel.Worksheet)_sr.ExcelWorkbook.Sheets[templateIndex]; if (!ws1.Name.Contains(@"标准模板")) { AddException(@"标准模板复制出错", true); success = false; return; } else { ws1.Name = str; } } else { AddException(@"找不到模板excel中的标准模板页", true); } _sr.WriteValue(_sr.ExcelWorkbook, ws1.Index, 4, 2, tempName, out success); _sr.WriteValue(_sr.ExcelWorkbook, ws1.Index, 5, 6, macType, out success); _sr.WriteValue(_sr.ExcelWorkbook, ws1.Index, 5, 2, tempQiju, out success); _sr.WriteValue(_sr.ExcelWorkbook, ws1.Index, 2, 12, str, out success); _sr.WriteValue(_sr.ExcelWorkbook, ws1.Index, 4, 6, tempStress, out success); if (needFix) { _sr.WriteValue(_sr.ExcelWorkbook, ws1.Index, 8, 13, "修正", out success); } else { //电离室->半导体 MSExcel.Range rr = _sr.GetRange(_sr.ExcelWorkbook, ws1.Index, "L8", out success); rr.FormulaLocal = ""; rr.Formula = ""; rr.FormulaArray = ""; _sr.WriteValue(_sr.ExcelWorkbook, ws1.Index, 8, 12, "1.000000", "@", out success); _sr.WriteValue(_sr.ExcelWorkbook, ws1.Index, 8, 13, "不修正", out success); } //写入记录者 _sr.WriteImage(_sr.ExcelWorkbook, ws1.Index, 29, 7, person, 45, 28, out success); _sr.ExcelWorkbook.Save(); } catch (Exception ex) { AddException("生成证书时遇到异常:" + ex.Message, true); } finally { //关闭Excel if (_sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } //有重大失误的情况下报错,没有失误就删除源word文件 if (exceptionNum > 0) { AddLog(@"***************************************************************", true); problemFilesList.Add(filePath); exceptionNum = 0; dataerrorNum = 0; } else { File.Delete(filePath); } } }
public GeneratingCertificateJob(string filePath, JobParameterStruct jobParam, Person person) : base(filePath, jobParam, person) { }
public void ClearJob() { this.jobList.Clear(); this.jobParam = null; this.isStopping = false; }
public GeneratingFormJob(string filePath, JobParameterStruct jobParam, Person person) : base(filePath, jobParam, person) { }