/// <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) { LogHelper.AddException(@"Excel文档无法打开", true); if (_sr != null && _sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } LogHelper.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, new ExcelPosition("L2")); 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, new ExcelPosition("A4")); if (!item.Name.Contains(@"标准模板") && rr.Text.ToString().Trim().StartsWith(@"送校单位")) { //有记录不包含规范的证书编号 LogHelper.AddException(@"该文档有实验数据不包含证书编号", true); noIdNumber++; } } renameStr = _sr.GetText(_sr.ExcelWorkbook, item.Index, new ExcelPosition(5, 7)).ToLower(); if (renameStr.StartsWith(@"编号:")) { _sr.WriteValue(_sr.ExcelWorkbook, item.Index, new ExcelPosition(5, 7), renameStr.Replace(@"编号:", @"主机编号:")); } renameStr = _sr.GetText(_sr.ExcelWorkbook, item.Index, new ExcelPosition(5, 11)).ToLower(); if (renameStr.StartsWith(@"电离室号:")) { _sr.WriteValue(_sr.ExcelWorkbook, item.Index, new ExcelPosition(5, 11), renameStr.Replace(@"电离室号:", @"探测器编号:")); } else if (renameStr.StartsWith(@"探测器号:")) { _sr.WriteValue(_sr.ExcelWorkbook, item.Index, new ExcelPosition(5, 11), renameStr.Replace(@"探测器号:", @"探测器编号:")); } } _sr.ExcelWorkbook.Save(); _sr.ExcelWorkbook.Saved = true; if (exSheets.Count == 0) { if (noIdNumber == 0) { LogHelper.AddException(@"该文档可能是空文档", true); } if (_sr != null && _sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } LogHelper.AddLog(@"***************************************************************", true); problemFilesList.Add(filePath); exceptionNum = 0; dataerrorNum = 0; return; } else if (exSheets.Count + noIdNumber > 1) { LogHelper.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 == "") { LogHelper.AddException(@"送校单位信息未提取到", true); checkClear = true; } if (strType == "") { LogHelper.AddException(@"仪器型号信息未提取到", true); checkClear = true; } if (strMacSerial == "" || strMacSerial == "主机_/") { LogHelper.AddException(@"主机编号信息未提取到", true); checkClear = true; } if (checkClear) { LogHelper.AddLog(@"***************************************************************", true); if (_sr != null && _sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } problemFilesList.Add(filePath); exceptionNum = 0; dataerrorNum = 0; return; } JobMethods.GetFixState(_sr, stateIndex, pattern, out needFix, out shouldFix); if (pattern < 2) { startDestiRowIndex = 18; } else if (pattern == 2) { startDestiRowIndex = 17; } else { LogHelper.AddException(@"数据类型无效", true); if (_sr != null && _sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } LogHelper.AddLog(@"***************************************************************", true); problemFilesList.Add(filePath); exceptionNum = 0; dataerrorNum = 0; return; } try { //寻找目标统计文件 temp_fi = JobMethods.SearchForFile(filePath, strType, strMacSerial, strSensorSerial, existFile, out isArchieved); if (temp_fi == null) { if (isArchieved) { //不存在,复制当前记录过去 //1.合成新文件名,按新名另存,定义为temp_fi newName = Util.PathExt.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.统计新文件 JobMethods.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) { LogHelper.AddException(@"Excel文档无法打开", true); _sr.ExcelWorkbook.Saved = true; _eu.ExcelWorkbook.Saved = true; _sr.TryClose(); _eu.TryClose(); LogHelper.AddLog(@"***************************************************************", true); problemFilesList.Add(filePath); exceptionNum = 0; dataerrorNum = 0; return; } _eu.ExcelApp.DisplayAlerts = false; _eu.ExcelApp.AlertBeforeOverwriting = false; JobMethods.CopyData(_sr, stateIndex, _eu, pattern, certId, needFix, shouldFix, startDestiRowIndex, out checkClear); if (checkClear) { _eu.ExcelWorkbook.Save(); _eu.ExcelWorkbook.Saved = true; JobMethods.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 { LogHelper.AddException(@"文件不存在:" + temp_fi.FullName, true); fileText = filePath; } } catch (Exception ex) { LogHelper.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); LogHelper.AddLog(@"***************************************************************", true); exceptionNum = 0; dataerrorNum = 0; } else { if (dataerrorNum > 0) { LogHelper.AddLog(@"该文件只有非致命错误,已经成功处理", true); LogHelper.AddLog(@"***************************************************************", true); exceptionNum = 0; dataerrorNum = 0; } File.Delete(filePath); } } }
/// <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) { LogHelper.AddException(@"Excel文档无法打开", true); _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); LogHelper.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) { LogHelper.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) { LogHelper.AddLog(@"***************************************************************", true); problemFilesList.Add(filePath); exceptionNum = 0; dataerrorNum = 0; } } }
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) { LogHelper.AddException(@"Excel文档无法打开", true); if (_sr != null && _sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } LogHelper.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, new ExcelPosition("L2")); 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, new ExcelPosition("A4")); if (!item.Name.Contains(@"标准模板") && rr.Text.ToString().Trim().StartsWith(@"送校单位")) { //有记录不包含规范的证书编号 LogHelper.AddException(@"该文档有实验数据不包含证书编号", true); noIdNumber++; } } } if (exSheets.Count == 0) { if (noIdNumber == 0) { LogHelper.AddException(@"该文档可能是空文档", true); } if (_sr != null && _sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } LogHelper.AddLog(@"***************************************************************", true); problemFilesList.Add(filePath); exceptionNum = 0; dataerrorNum = 0; return; } else if (exSheets.Count + noIdNumber > 1) { LogHelper.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 == "") { LogHelper.AddException(@"送校单位信息未提取到", true); checkClear = true; } if (strType == "") { LogHelper.AddException(@"仪器型号信息未提取到", true); checkClear = true; } if (strMacSerial == "") { LogHelper.AddException(@"主机编号信息未提取到", true); checkClear = true; } if (checkClear) { LogHelper.AddLog(@"***************************************************************", true); if (_sr != null && _sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } problemFilesList.Add(filePath); exceptionNum = 0; dataerrorNum = 0; return; } JobMethods.GetFixState(_sr, stateIndex, pattern, out needFix, out shouldFix); //判断目标行数 if (pattern < 2) { startDestiRowIndex = 18; } else if (pattern == 2) { startDestiRowIndex = 17; } else { LogHelper.AddException(@"数据类型无效", true); if (_sr != null && _sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } LogHelper.AddLog(@"***************************************************************", true); problemFilesList.Add(filePath); exceptionNum = 0; dataerrorNum = 0; return; } try { //寻找目标统计文件 existFile = JobMethods.GetFilesFromType(output, _sr.GetText(_sr.ExcelWorkbook, stateIndex, new ExcelPosition("F5")), ext, out checkClear); if (!checkClear) { if (_sr != null && _sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } LogHelper.AddLog(@"***************************************************************", true); problemFilesList.Add(filePath); exceptionNum = 0; dataerrorNum = 0; return; } temp_fi = JobMethods.SearchForFile(filePath, strType, strMacSerial, strSensorSerial, existFile, out hasArchieved); if (temp_fi == null) { if (createNew) { //对新记录建档 needTestGeCe = false; if (hasArchieved) { //不存在,复制当前记录过去 newName = DataUtility.DataUtility.PathRightFileName(Util.PathExt.PathCombine(output, strType), 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); JobMethods.Statistic(_sr, pattern, Perfect, strCompany, strType, tempName, certId); //TODO: 生成证书处校验是否可选(不存在,复制当前记录过去) _sr.Verification(_sr.ExcelWorkbook, true, pattern, out checkClear); fileText = newName; } else { //不存在,但搜索到有可疑项,暂不处理 fileText = filePath; } } else { //对新记录不建档,退出 LogHelper.AddException("没有在历史数据记录中发现匹配项,暂不处理。", true); if (_sr != null && _sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } LogHelper.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) { LogHelper.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(); } LogHelper.AddLog(@"***************************************************************", true); problemFilesList.Add(filePath); exceptionNum = 0; dataerrorNum = 0; return; } _eu.ExcelApp.DisplayAlerts = false; _eu.ExcelApp.AlertBeforeOverwriting = false; JobMethods.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); LogHelper.AddLog(@"***************************************************************", true); exceptionNum = 0; dataerrorNum = 0; return; } _eu.ExcelWorkbook.Save(); _eu.ExcelWorkbook.Saved = true; canGeCe = JobMethods.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 { LogHelper.AddException(@"文件不存在:" + temp_fi.FullName, true); fileText = filePath; } } catch (Exception ex) { LogHelper.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); LogHelper.AddLog(@"***************************************************************", true); exceptionNum = 0; dataerrorNum = 0; } else { //有以前记录,需要看是否超差 并且 超差不通过 并且 超差不通过的提示选择不生成证书 时选择退出 if (needTestGeCe && !canGeCe && (MessageBox.Show("检测到有数据超差,是否继续生成证书?", "问题", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)) { LogHelper.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); LogHelper.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) { LogHelper.AddException(@"Excel文档无法打开", true); if (_sr != null && _sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } LogHelper.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, new ExcelPosition("L2")); if (certId == item.Name || certId == temp) { stateIndex = item.Index; } } //找到序号的话,加入校核人的签名,删除其他sheet if (stateIndex > 0 && stateIndex < _sr.ExcelWorkbook.Worksheets.Count) { _sr.WriteImage(_sr.ExcelWorkbook, stateIndex, new ExcelPosition(29, 9), Util.PathExt.PathCombine(Application.StartupPath, person.Path), 45, 28); 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; } //出证书 JobMethods.GenerateCert(_sr, stateIndex, pS.DataPattern, pS.CertTemplateFilePath, pS.CertFolder, pS.PDFDataFolder, pS.TempFolder, shouldFix, out success); if (!success) { LogHelper.AddException("生成证书失败", true); if (_sr != null && _sr.ExcelWorkbook != null) { _sr.ExcelWorkbook.Saved = true; _sr.TryClose(); } problemFilesList.Add(fileText); LogHelper.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) { LogHelper.AddException(@"生成证书时遇到异常:" + ex.Message, true); } if (dataerrorNum > 0) { dataerrorNum = 0; LogHelper.AddLog(@"***************************************************************", true); } }