| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- 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
- {
- /// <summary>
- /// 文档导出节点
- /// </summary>
- /// <param name="node">节点数据</param>
- /// <param name="paramList">参数列表</param>
- /// <param name="logDic">日志列表</param>
- public static void export(NodeList node, List<ParamList> paramList, List<LogItem> logDic)
- {
- Dictionary<string, object> condi = new Dictionary<string, object>(); // 条件参数字典
- 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
- });
- }
- /// <summary>
- /// JSON字符串转XLSX文件
- /// </summary>
- /// <param name="jsonStr">JSON数组字符串</param>
- /// <param name="savePath">文件保存完整路径</param>
- public static void JsonToExcel(string jsonStr, string savePath)
- {
- // 1. 校验入参
- if (string.IsNullOrWhiteSpace(jsonStr))
- throw new ArgumentException("JSON数据不能为空");
- // 2. JSON字符串转DataTable(自动解析表头和行数据)
- DataTable dt = JsonConvert.DeserializeObject<DataTable>(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));
- }
- }
- }
- }
|