#006 调试解析文件,录入数据库,生成客户所需报告样式的功能;(对比奇瑞间隙面差自动化项目)

This commit is contained in:
HM-CN\zhengxuan.zhang
2025-08-06 18:12:49 +08:00
parent 89ab9cce88
commit 3b986f5146
13 changed files with 7981 additions and 737 deletions
+122 -1
View File
@@ -1,6 +1,8 @@
using NLog;
using Newtonsoft.Json;
using NLog;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Drawing;
@@ -3141,4 +3143,123 @@ namespace BaseFunction
#endregion
}
public static class JsonHelper
{
/// <summary>
/// 对象转成JSON 格式字符串
/// </summary>
/// <param name="obj">对象</param>
/// <returns>JSON格式的字符串</returns>
public static string ObjectToJson(object obj)
{
return JsonConvert.SerializeObject(obj);
}
/// <summary>
/// 解析JSON字符串生成对象实体
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="json">json字符串</param>
/// <returns>对象实体</returns>
public static T DeserializeJsonToObject<T>(string json) where T : class
{
Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer();
StringReader sr = new StringReader(json);
object o = serializer.Deserialize(new JsonTextReader(sr), typeof(T));
T t = o as T;
return t;
}
/// <summary>
/// 解析JSON数组生成对象实体集合
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="json">json数组字符串(eg.[{"ID":"112","Name":"石子儿"}])</param>
/// <returns>对象实体集合</returns>
public static List<T> DeserializeJsonToList<T>(string json) where T : class
{
Newtonsoft.Json.JsonSerializer serializer = new JsonSerializer();
StringReader sr = new StringReader(json);
object o = serializer.Deserialize(new JsonTextReader(sr), typeof(List<T>));
List<T> list = o as List<T>;
return list;
}
/// <summary>
/// 数据表转键值对集合 把DataTable转成 List集合, 存每一行 集合中放的是键值对字典,存每一列
/// </summary>
/// <param name="dt">数据表</param>
/// <returns>哈希表数组</returns>
public static List<Dictionary<string, object>> DataTableToList(DataTable dt)
{
List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
foreach (DataRow dr in dt.Rows)
{
Dictionary<string, object> dic = new Dictionary<string, object>();
foreach (DataColumn dc in dt.Columns)
{
dic.Add(dc.ColumnName, dr[dc.ColumnName]);
}
list.Add(dic);
}
return list;
}
/// <summary>
/// 数据集转键值对数组字典
/// </summary>
/// <returns>键值对数组字典</returns>
public static Dictionary<string, List<Dictionary<string, object>>> DataSetToDic(DataSet ds)
{
Dictionary<string, List<Dictionary<string, object>>> result = new Dictionary<string, List<Dictionary<string, object>>>();
foreach (DataTable dt in ds.Tables)
result.Add(dt.TableName, DataTableToList(dt));
return result;
}
/// <summary>
/// 数据表转JSON
/// </summary>
/// <param name="dt">数据表</param>
/// <returns>JSON字符串</returns>
public static string DataTableToJson(DataTable dt)
{
return ObjectToJson(DataTableToList(dt));
}
/// <summary>
/// JSON文本转对象,泛型方法 常用
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="jsonText">JSON文本</param>
/// <returns>指定类型的对象</returns>
public static T JsonToObject<T>(string jsonText)
{
return JsonConvert.DeserializeObject<T>(jsonText);
}
/// <summary>
/// 将JSON文本转换为数据表数据
/// </summary>
/// <param name="jsonText">JSON文本</param>
/// <returns>数据表字典</returns>
public static Dictionary<string, List<Dictionary<string, object>>> TablesDataFromJson(string jsonText)
{
return JsonToObject<Dictionary<string, List<Dictionary<string, object>>>>(jsonText);
}
/// <summary>
/// 将JSON文本转换成数据行
/// </summary>
/// <param name="jsonText">JSON文本</param>
/// <returns>数据行的字典</returns>
public static Dictionary<string, object> DataRowFromJson(string jsonText)
{
return JsonToObject<Dictionary<string, object>>(jsonText);
}
}
}
+2 -12
View File
@@ -3,7 +3,6 @@ using NSAnalysis.DAL;
using NSAnalysis.Model;
using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
namespace NSAnalysis.BaseUnit
@@ -32,18 +31,16 @@ namespace NSAnalysis.BaseUnit
string sourceDir = task["sourceFile"].ToString();
string targetDir = task["targetFile"].ToString();
// 打印信息
MyBase.TraceWriteLine($"Processing Task - Source: {sourceDir}, Target: {targetDir}, ModelCode: {modelCode} Position: {position}");
if (Directory.Exists(sourceDir))
{
ProcessDirectory(sourceDir, targetDir, modelCode, modelName,position);
ProcessDirectory(sourceDir, targetDir, modelCode, modelName, position);
}
else
{
MyBase.TraceWriteLine($"源文件地址不存在或错误: {sourceDir}");
//记录到数据库
try
{
@@ -70,9 +67,6 @@ namespace NSAnalysis.BaseUnit
{
MyBase.TraceWriteLine($"源文件地址不存在或错误: {sourceDir}");
}
}
}
}
@@ -92,13 +86,12 @@ namespace NSAnalysis.BaseUnit
return dt;
}
private void ProcessDirectory(string sourceDir, string targetDir, string modelCode, string modelName, string position )
private void ProcessDirectory(string sourceDir, string targetDir, string modelCode, string modelName, string position)
{
// 匹配信息
string matchStr = $"{modelCode}_{position}";
MyBase.TraceWriteLine($"Matching files with: {matchStr}");
// 判断目标目录是否存在,如果不存在记录到日志
// 确保目标目录存在
@@ -202,9 +195,6 @@ namespace NSAnalysis.BaseUnit
//string testPath = @"D:\CJLR\DATA\Input\LLL\K0902906.csv";
//bool result = MatchCsvValue(testPath, "X540_L", 3, 1);
//MyBase.TraceWriteLine($"匹配结果: {result}");
}
// 编写一个打印 DataTable 对象的方法,输入是对象
-2
View File
@@ -4,7 +4,6 @@ using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Threading.Tasks;
namespace NSAnalysis.DAL
{
@@ -163,7 +162,6 @@ namespace NSAnalysis.DAL
return dt;
}
public bool CheckTaskExit(string strModelsName, string strModelsCode, string strReadType)
{
DataTable dt = new DataTable();
+2 -2
View File
@@ -133,8 +133,8 @@ namespace NSAnalysis
bLanguage = FileIni.ReadBool(ConfigDfn.strConfigFile, strSection, "Language", 0);
bRememberMe = FileIni.ReadBool(ConfigDfn.strConfigFile, strSection, "RememberMe", 0);
strNextSenseCSVEH3Path = FileIni.ReadString(ConfigDfn.strConfigFile, strSection, "NextsenseCSVEH3Path");
strNextSenseCSVEHYPath = FileIni.ReadString(ConfigDfn.strConfigFile, strSection, "NextsenseCSVEHYPath");
strNextSenseSelfMeasurePath = FileIni.ReadString(ConfigDfn.strConfigFile, strSection, "NextseneSelfMeasurePath");
strPwd = FileIni.ReadString(ConfigDfn.strConfigFile, strSection, "Password");
iCreateReportFlag = FileIni.ReadInt(ConfigDfn.strConfigFile, strSection, "CreateReportFlag");
strUploadPath = FileIni.ReadString(ConfigDfn.strConfigFile, strSection, "tavascanUploadPath");
+168 -696
View File
File diff suppressed because it is too large Load Diff
-3
View File
@@ -77,9 +77,6 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>bin\x64\Debug\Covert.dll</HintPath>
</Reference>
<Reference Include="DAL">
<HintPath>..\DAL\bin\Debug\DAL.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.13.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
+3 -3
View File
@@ -83,9 +83,9 @@ namespace NSAnalysis
#endregion Log文件
//Application.Run(new FormMain());
FileSorter fileSorter = new FileSorter();
fileSorter.test();
Application.Run(new FormMain());
//FileSorter fileSorter = new FileSorter();
//fileSorter.test();
//if (gAuthorizationMode == 1)
// {
Binary file not shown.
File diff suppressed because it is too large Load Diff
@@ -7,8 +7,8 @@ Language =1
;0 = 英语
RememberMe=1
NextsenseCSVEH3Path=D:\cherytestEH3
NextsenseCSVEHYPath=D:\cherytestEHY
NextseneSelfMeasurePath=D:\test
ReportCSVPath=D:\QMLTest
Binary file not shown.
Binary file not shown.
+2 -2
View File
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="13.0.3" targetFramework="net46" />
<package id="NLog" version="5.3.3" targetFramework="net47" />
<package id="Newtonsoft.Json" version="13.0.3" targetFramework="net46" />
<package id="NLog" version="5.3.3" targetFramework="net47" />
</packages>