using System.Data; using System.Text.RegularExpressions; using Common; using Infrastructure; using LitJson; using Model; using Model.Custom; using Model.Source; using Newtonsoft.Json; using OfficeOpenXml; using OfficeOpenXml.Style; namespace Util.Logic { public class LogicDocNodeHelper { /// /// 文档导出节点 /// /// 节点数据 /// 参数列表 /// 日志列表 public static void export(NodeList node, List paramList, List logDic) { Dictionary condi = new Dictionary(); // 条件参数字典 JsonData nodeData = node.nodeData; JsonData inputFields = nodeData["data"]["formData"]["input"]["value"]; // 输入参数 string paramAttribute = ""; if (nodeData.ToJson().Contains("\"parentNode\"")) { paramAttribute = "iterationInputField"; } if(inputFields.Count > 0) { JsonData item = inputFields[0]; string paramName = item["label"].ToString(); string paramValue = item["value"].ToString(); string paramType = item["type"][0].ToString(); if(paramValue.Contains(".")) { paramValue = paramValue.Substring(paramValue.LastIndexOf(".") + 1); var param = paramList.FirstOrDefault(x => x.paramName == paramValue && x.paramAttribute == paramAttribute) ?? paramList.FirstOrDefault(x => x.paramName == paramValue) ?? new ParamList(); paramValue = param.paramValue; }// 聚合字段类型 if(paramType == "array") { string savePath = Function.getPath("/ex"); } } logDic.Add(new LogItem() { title = node.nodeData["data"]["nodeMeta"]["label"].ToString(), param = condi }); } /// /// JSON字符串转XLSX文件 /// /// JSON数组字符串 /// 文件保存完整路径 public static void JsonToExcel(string jsonStr, string savePath) { // 1. 校验入参 if (string.IsNullOrWhiteSpace(jsonStr)) throw new ArgumentException("JSON数据不能为空"); // 2. JSON字符串转DataTable(自动解析表头和行数据) DataTable dt = JsonConvert.DeserializeObject(jsonStr); // 3. 确保保存目录存在(避免目录不存在报错) string directory = Path.GetDirectoryName(savePath); if (!Directory.Exists(directory)) Directory.CreateDirectory(directory); // 4. 创建Excel包 using (ExcelPackage package = new ExcelPackage()) { // 5. 添加工作表 ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("数据明细"); // 6. 把DataTable写入Excel(从A1开始,true=包含表头行) worksheet.Cells["A1"].LoadFromDataTable(dt, true); // 7. 可选优化:自动调整列宽,避免内容被截断 worksheet.Cells.AutoFitColumns(); // 8. 可选美化:表头加粗+背景色+居中 using (ExcelRange header = worksheet.Cells[1, 1, 1, dt.Columns.Count]) { header.Style.Font.Bold = true; header.Style.Fill.PatternType = ExcelFillStyle.Solid; header.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightBlue); header.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; } // 9. 可选:指定日期列格式(避免Excel里变成文本) // worksheet.Columns["D"].Style.Numberformat.Format = "yyyy-mm-dd hh:mm:ss"; // 10. 保存文件到本地 package.SaveAs(new FileInfo(savePath)); } } } }