LogicDocNodeHelper.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System.Data;
  2. using System.Text.RegularExpressions;
  3. using Common;
  4. using Infrastructure;
  5. using LitJson;
  6. using Model;
  7. using Model.Custom;
  8. using Model.Source;
  9. using Newtonsoft.Json;
  10. using OfficeOpenXml;
  11. using OfficeOpenXml.Style;
  12. namespace Util.Logic
  13. {
  14. public class LogicDocNodeHelper
  15. {
  16. /// <summary>
  17. /// 文档导出节点
  18. /// </summary>
  19. /// <param name="node">节点数据</param>
  20. /// <param name="paramList">参数列表</param>
  21. /// <param name="logDic">日志列表</param>
  22. public static void export(NodeList node, List<ParamList> paramList, List<LogItem> logDic)
  23. {
  24. Dictionary<string, object> condi = new Dictionary<string, object>(); // 条件参数字典
  25. JsonData nodeData = node.nodeData;
  26. JsonData inputFields = nodeData["data"]["formData"]["input"]["value"]; // 输入参数
  27. string paramAttribute = "";
  28. if (nodeData.ToJson().Contains("\"parentNode\""))
  29. {
  30. paramAttribute = "iterationInputField";
  31. }
  32. if(inputFields.Count > 0)
  33. {
  34. JsonData item = inputFields[0];
  35. string paramName = item["label"].ToString();
  36. string paramValue = item["value"].ToString();
  37. string paramType = item["type"][0].ToString();
  38. if(paramValue.Contains("."))
  39. {
  40. paramValue = paramValue.Substring(paramValue.LastIndexOf(".") + 1);
  41. var param = paramList.FirstOrDefault(x => x.paramName == paramValue && x.paramAttribute == paramAttribute) ?? paramList.FirstOrDefault(x => x.paramName == paramValue) ?? new ParamList();
  42. paramValue = param.paramValue;
  43. }// 聚合字段类型
  44. if(paramType == "array")
  45. {
  46. string savePath = Function.getPath("/ex");
  47. }
  48. }
  49. logDic.Add(new LogItem()
  50. {
  51. title = node.nodeData["data"]["nodeMeta"]["label"].ToString(),
  52. param = condi
  53. });
  54. }
  55. /// <summary>
  56. /// JSON字符串转XLSX文件
  57. /// </summary>
  58. /// <param name="jsonStr">JSON数组字符串</param>
  59. /// <param name="savePath">文件保存完整路径</param>
  60. public static void JsonToExcel(string jsonStr, string savePath)
  61. {
  62. // 1. 校验入参
  63. if (string.IsNullOrWhiteSpace(jsonStr))
  64. throw new ArgumentException("JSON数据不能为空");
  65. // 2. JSON字符串转DataTable(自动解析表头和行数据)
  66. DataTable dt = JsonConvert.DeserializeObject<DataTable>(jsonStr);
  67. // 3. 确保保存目录存在(避免目录不存在报错)
  68. string directory = Path.GetDirectoryName(savePath);
  69. if (!Directory.Exists(directory))
  70. Directory.CreateDirectory(directory);
  71. // 4. 创建Excel包
  72. using (ExcelPackage package = new ExcelPackage())
  73. {
  74. // 5. 添加工作表
  75. ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("数据明细");
  76. // 6. 把DataTable写入Excel(从A1开始,true=包含表头行)
  77. worksheet.Cells["A1"].LoadFromDataTable(dt, true);
  78. // 7. 可选优化:自动调整列宽,避免内容被截断
  79. worksheet.Cells.AutoFitColumns();
  80. // 8. 可选美化:表头加粗+背景色+居中
  81. using (ExcelRange header = worksheet.Cells[1, 1, 1, dt.Columns.Count])
  82. {
  83. header.Style.Font.Bold = true;
  84. header.Style.Fill.PatternType = ExcelFillStyle.Solid;
  85. header.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightBlue);
  86. header.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
  87. }
  88. // 9. 可选:指定日期列格式(避免Excel里变成文本)
  89. // worksheet.Columns["D"].Style.Numberformat.Format = "yyyy-mm-dd hh:mm:ss";
  90. // 10. 保存文件到本地
  91. package.SaveAs(new FileInfo(savePath));
  92. }
  93. }
  94. }
  95. }