// 输出一行日志或册信息 static void OutputTransferColumns( int no, TransferItem transfer_item, IXLWorksheet sheet, int nStartColIndex, // 从 0 开始计数 List <string> col_list, int nRowIndex) // 从 0 开始计数。 { string strError = ""; XmlDocument dom = new XmlDocument(); try { dom.LoadXml(transfer_item.NewXml); } catch (Exception ex) { // 2019/12/3 strError = $"!error: 装载册记录 XML 到 DOM 时出错: {ex.Message}"; dom.LoadXml("<root />"); } List <IXLCell> cells = new List <IXLCell>(); int i = 0; foreach (string col in col_list) { string strValue = ""; if (col == "log_no") { strValue = no.ToString(); } else if (col == "log_operTime") { strValue = transfer_item.OperTime.ToString(); } else if (col == "item_recPath") { strValue = transfer_item.RecPath; } else if (col.StartsWith("log_")) { string name = col.Substring("log_".Length).Trim(); // 把第一个字母大写 name = char.ToUpper(name[0]) + name.Substring(1); strValue = GetPropertyOrField(transfer_item, name); if (strValue == null) { i++; continue; } } else if (col.StartsWith("item_")) { string name = col.Substring("item_".Length).Trim(); strValue = DomUtil.GetElementText(dom.DocumentElement, name); } else { i++; continue; } { IXLCell cell = sheet.Cell(nRowIndex + 1, nStartColIndex + (i++) + 1).SetValue(strValue); // 统计最大字符数 // DigitalPlatform.dp2.Statis.ClosedXmlUtil.SetMaxChars(column_max_chars, cell.Address.ColumnNumber - 1, strValue?.Length); cell.Style.Alignment.Vertical = XLAlignmentVerticalValues.Center; if (col == "recpath" || col.EndsWith("_recpath")) { cell.Style.Font.FontColor = XLColor.LightGray; } else if (string.IsNullOrEmpty(strError) == false) { cell.Style.Fill.SetBackgroundColor(XLColor.DarkRed); cell.Style.Font.SetFontColor(XLColor.White); } } } }
// 典藏移交清单。内置统计方案 // return: // -1 出错 // 0 成功 // 1 用户中断 int TransferList(out string strError) { strError = ""; var items = new List <TransferItem>(); EnableControls(false); stop.OnStop += new StopEventHandler(this.DoStop); stop.Initial("正在执行脚本 ..."); stop.BeginLoop(); try { // 搜集信息 int nRet = DoLoop((string strLogFileName, string strXml, bool bInCacheFile, long lHint, long lIndex, long lAttachmentTotalLength, object param, out string strError1) => { strError1 = ""; XmlDocument dom = new XmlDocument(); dom.LoadXml(strXml); // 搜集全部相关日志记录 string operation = DomUtil.GetElementText(dom.DocumentElement, "operation"); if (operation != "setEntity") { return(0); } string action = DomUtil.GetElementText(dom.DocumentElement, "action"); if (action != "transfer") { return(0); } var item = new TransferItem(); item.BatchNo = DomUtil.GetElementText(dom.DocumentElement, "batchNo"); item.Operator = DomUtil.GetElementText(dom.DocumentElement, "operator"); string operTime = DomUtil.GetElementText(dom.DocumentElement, "operTime"); item.OperTime = DateTimeUtil.FromRfc1123DateTimeString(operTime).ToLocalTime(); XmlDocument old_itemdom = new XmlDocument(); old_itemdom.LoadXml(DomUtil.GetElementText(dom.DocumentElement, "oldRecord")); item.SourceLocation = DomUtil.GetElementText(old_itemdom.DocumentElement, "location"); string new_xml = DomUtil.GetElementText(dom.DocumentElement, "record", out XmlNode node); XmlDocument new_itemdom = new XmlDocument(); new_itemdom.LoadXml(new_xml); item.TargetLocation = DomUtil.GetElementText(new_itemdom.DocumentElement, "location"); item.Barcode = DomUtil.GetElementText(new_itemdom.DocumentElement, "barcode"); item.RecPath = ((XmlElement)node).GetAttribute("recPath"); item.NewXml = new_xml; item.Style = DomUtil.GetElementText(dom.DocumentElement, "style"); items.Add(item); return(0); }, out strError); if (nRet == -1 || nRet == 1) { return(nRet); } } finally { stop.EndLoop(); stop.OnStop -= new StopEventHandler(this.DoStop); stop.Initial(""); stop.HideProgress(); EnableControls(true); } // 让用户选择需要统计的范围。根据批次号、目标位置来进行选择 var list = items.GroupBy( x => new { x.BatchNo, x.TargetLocation }, (key, item_list) => new TransferGroup { BatchNo = key.BatchNo, TargetLocation = key.TargetLocation, Items = new List <TransferItem>(item_list) }).ToList(); List <TransferGroup> groups = null; bool output_one_sheet = true; using (var dlg = new SelectOutputRangeDialog()) { dlg.Font = this.Font; dlg.Groups = list; dlg.UiState = Program.MainForm.AppInfo.GetString( "OperLogStatisForm", "SelectOutputRangeDialog_uiState", ""); Program.MainForm.AppInfo.LinkFormState(dlg, "SelectOutputRangeDialog_formstate"); dlg.ShowDialog(this); Program.MainForm.AppInfo.SetString( "OperLogStatisForm", "SelectOutputRangeDialog_uiState", dlg.UiState); if (dlg.DialogResult == DialogResult.Cancel) { return(1); } groups = dlg.SelectedGroups; output_one_sheet = dlg.OutputOneSheet; } this.ShowMessage("正在创建 Excel 报表 ..."); string fileName = ""; // 创建 Excel 报表 // 询问文件名 using (SaveFileDialog dlg = new SaveFileDialog { Title = "请指定要输出的 Excel 文件名", CreatePrompt = false, OverwritePrompt = true, // dlg.FileName = this.ExportExcelFilename; // dlg.InitialDirectory = Environment.CurrentDirectory; Filter = "Excel 文件 (*.xlsx)|*.xlsx|All files (*.*)|*.*", RestoreDirectory = true }) { if (dlg.ShowDialog() != DialogResult.OK) { return(0); } fileName = dlg.FileName; XLWorkbook doc = null; try { doc = new XLWorkbook(XLEventTracking.Disabled); File.Delete(dlg.FileName); } catch (Exception ex) { strError = ExceptionUtil.GetAutoText(ex); return(-1); } if (output_one_sheet) { CreateSheet(doc, "典藏移交清单", groups); } else { int count = groups.Count; int i = 0; foreach (var group in groups) { CreateSheet(doc, $"{++i} {count} {group.BatchNo}-{group.TargetLocation}", // Cut($"({++i} of {count})", 31), new List <TransferGroup> { group }); } } doc.SaveAs(dlg.FileName); } this.ClearMessage(); try { System.Diagnostics.Process.Start(fileName); } catch { } return(0); }