Browse Source

导出数据,子集解析

lcl 1 năm trước cách đây
mục cha
commit
0dc1b2b3c6
3 tập tin đã thay đổi với 98 bổ sung14 xóa
  1. 97 9
      AppStart/Helper/ExportService.cs
  2. 1 0
      MySystem.csproj
  3. 0 5
      global.json

+ 97 - 9
AppStart/Helper/ExportService.cs

@@ -8,6 +8,7 @@ using NPOI.HSSF.UserModel;
 using NPOI.XSSF.UserModel;
 using NPOI.SS.UserModel;
 using NPOI.HSSF.Util;
+using System.Text.RegularExpressions;
 
 namespace MySystem
 {
@@ -111,12 +112,13 @@ namespace MySystem
             string ConnectStr = "";
             if(content.Contains("\"ConnectStr\""))
             {
-                ConnectStr = jsonObj[prop_name: "ConnectStr"].ToString();
+                ConnectStr = jsonObj["ConnectStr"].ToString();
             }
             else
             {
                 ConnectStr = MysqlConn.connstr;
             }
+            bool SubSqlFlag = content.Contains("\"SubSqlString\"");
 
             //创建工作薄
             var workbook = new XSSFWorkbook();
@@ -137,6 +139,50 @@ namespace MySystem
             string FilePath = function.ReadInstance("/WebRootPath.txt");
             function.WriteLog(FilePath, "导出日志");
 
+            //获取子集字典数据
+            Dictionary<string, Dictionary<string, string>> dicValsJson = new Dictionary<string, Dictionary<string, string>>();
+            if(SubSqlFlag)
+            {
+                JsonData subItems = jsonObj["SubSqlString"];
+                for (int index = 0; index < subItems.Count; index++)
+                {
+                    JsonData sub = subItems[index];
+                    string DatabaseConnect = sub["DatabaseConnect"].ToString();
+                    string SqlContent = sub["SqlContent"].ToString();
+                    string DataKey = sub["DataKey"].ToString();
+                    string DataText = sub["DataText"].ToString();
+                    string Alias = sub["Alias"].ToString();
+                    string SubConnectStr = DatabaseConnect;
+                    if(!string.IsNullOrEmpty(SubConnectStr))
+                    {
+                        SubConnectStr = GetConnectStr(DatabaseConnect);
+                    }
+                    else
+                    {
+                        SubConnectStr = MysqlConn.readconnstr;
+                    }
+                    string sqlstr = SqlContent;
+
+                    DataTable dtsub = CustomerSqlConn.dtable(sqlstr, SubConnectStr);
+                    if(dtsub.Rows.Count > 0)
+                    {
+                        if(!string.IsNullOrEmpty(DataKey))
+                        {
+                            string[] TextFields = DataText.Split(',');
+                            Dictionary<string, string> vals = new Dictionary<string, string>();
+                            foreach(DataRow dr in dtsub.Rows)
+                            {
+                                foreach(string key in TextFields)
+                                {
+                                    vals.Add(key, dr[key].ToString());
+                                }
+                            }
+                            dicValsJson.Add(Alias + "." + DataKey, vals);
+                        }
+                    }
+                }
+            }
+
             //填充数据
             int i = 0;
             int page = 0;
@@ -163,21 +209,54 @@ namespace MySystem
                                 int j = 0;
                                 foreach (DataColumn dc in dt.Columns)
                                 {
-                                    var cell = title.CreateCell(j);
-                                    cell.CellStyle = style;
-                                    cell.SetCellValue(dc.ColumnName);
-                                    j += 1;
+                                    string val = dr[dc.ColumnName].ToString();
+                                    Match m = Regex.Match(val, @"\#\{.*?\}\#");
+                                    if(m.Success)
+                                    {
+                                        string fieldName = m.Value.Replace("#{", "").Replace("}#", "");
+                                        Dictionary<string, string> fieldVals = dicValsJson[fieldName];
+                                        foreach(string field in fieldVals.Keys)
+                                        {
+                                            var cell = title.CreateCell(j);
+                                            cell.CellStyle = style;
+                                            cell.SetCellValue(dc.ColumnName + field);
+                                            j += 1;
+                                        }
+                                    }
+                                    else
+                                    {
+                                        var cell = title.CreateCell(j);
+                                        cell.CellStyle = style;
+                                        cell.SetCellValue(dc.ColumnName);
+                                        j += 1;
+                                    }
                                 }
                             }
                             var row = table.CreateRow(i);
                             int index = 0;
                             foreach (DataColumn dc in dt.Columns)
                             {
-                                var cell = row.CreateCell(index);
                                 string val = dr[dc.ColumnName].ToString();
-                                cell.CellStyle = style;
-                                cell.SetCellValue(val);
-                                index += 1;
+                                Match m = Regex.Match(val, @"\#\{.*?\}\#");
+                                if(m.Success)
+                                {
+                                    string fieldName = m.Value.Replace("#{", "").Replace("}#", "");
+                                    Dictionary<string, string> fieldVals = dicValsJson[fieldName];
+                                    foreach(string field in fieldVals.Keys)
+                                    {
+                                        var cell = row.CreateCell(index);
+                                        cell.CellStyle = style;
+                                        cell.SetCellValue(fieldVals[field]);
+                                        index += 1;
+                                    }
+                                }
+                                else
+                                {
+                                    var cell = row.CreateCell(index);
+                                    cell.CellStyle = style;
+                                    cell.SetCellValue(val);
+                                    index += 1;
+                                }
                             }
                         }
                         else
@@ -204,5 +283,14 @@ namespace MySystem
                 function.WriteLog("end", "导出日志");
             }
         }
+
+        #region 获取链接数据库字符串
+
+        public string GetConnectStr(string key)
+        {
+            return Library.ConfigurationManager.AppSettings[key].ToString();
+        }
+
+        #endregion
     }
 }

+ 1 - 0
MySystem.csproj

@@ -2,6 +2,7 @@
 <Project Sdk="Microsoft.NET.Sdk.Web">
   <PropertyGroup>
     <TargetFramework>netcoreapp3.0</TargetFramework>
+    <SatelliteResourceLanguages>e</SatelliteResourceLanguages>
   </PropertyGroup>
   <ItemGroup>
     <DotNetCliToolReference Include="dotnet-svcutil" Version="1.0.*" />

+ 0 - 5
global.json

@@ -1,5 +0,0 @@
-{
-  "sdk": {
-    "version": "3.0.100"
-  }
-}