1、工具背景介绍
Vector CANDelaStudio10 中关于DTC使用Vector提供的标准模板可以快速导入DTC,这个方面可以自行百度学习。今天我们介绍在拿到客户的诊断问卷调查表之后,将相关信息快速导入到模板Excel然后再导入到CDD中。
2、工具UI
1、诊断问卷中调取的规则
2、打开诊断问卷导入内容
3、处理数据,将异常数据剔除,同时处理数据
4、选择导出的文件夹
5、导出Excel
3、代码
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CANoeHelperTool
{
public partial class CDDExcelToDTC : Form
{
#region 初始化方法
public CDDExcelToDTC()
{
InitializeComponent();
tB_OpenDiag.Text = GetDesktopPath();
tB_CDD_Import.Text = GetDesktopPath();
cDDExcelToDTCDAL = new CDDExcelToDTCDAL();
}
CDDExcelToDTCDAL cDDExcelToDTCDAL;
#endregion
#region 数据处理
List<Model_CDD_DTC_Import> model_CDD_DTC_Imports;
private async void bt_Open_ClickAsync(object sender, EventArgs e)
{
string filePath = null;
string sheetName = null;
string display = null;
string lByte = null;
string description = null;
// 在UI线程中获取控件的值
this.Invoke((MethodInvoker)delegate
{
tB_OpenDiag.Text = SelectFile(tB_OpenDiag.Text);
filePath = tB_OpenDiag.Text;
sheetName = cB_SheetName.Text;
display = cB_Display.Text;
lByte = cB_LByte.Text;
description = cB_Description.Text;
});
if (string.IsNullOrEmpty(filePath))
{
return;
}
try
{
// 使用后台线程获取Excel数据
model_CDD_DTC_Imports = await Task.Run(() =>
{
return cDDExcelToDTCDAL.GetExcelModels_DTC_01(filePath, sheetName, display, lByte, description);
});
// 在UI线程更新DataGridView
this.Invoke((MethodInvoker)delegate
{
cDDExcelToDTCDAL.UpdateDataGridView(this.dataGridViewData, model_CDD_DTC_Imports);
});
}
catch (Exception ex)
{
// 处理异常情况,例如输出错误日志或提示用户更新失败
MessageBox.Show("获取Excel数据时发生异常:" + ex.Message);
}
}
List<Model_CDD_DTC_Import> model_CDD_DTC_Imports02;
private void bt_DoData_Click(object sender, EventArgs e)
{
string pattern = @"^[UC]\w{6}$";
model_CDD_DTC_Imports02 = cDDExcelToDTCDAL.FilterDTCByPattern(model_CDD_DTC_Imports, pattern);
// 在UI线程更新DataGridView
this.Invoke((MethodInvoker)delegate
{
cDDExcelToDTCDAL.UpdateDataGridView(this.dataGridViewData02, model_CDD_DTC_Imports02);
});
}
private void bt_CDD_Import_Click(object sender, EventArgs e)
{
tB_CDD_Import.Text = SelectFolder(tB_CDD_Import.Text);
}
private void bt_OutExcel_Click(object sender, EventArgs e)
{
if (model_CDD_DTC_Imports == null)
{
MessageBox.Show("model_CDD_DTC_Imports 未实例化或者为空");
}
if (cDDExcelToDTCDAL.OutExcel(model_CDD_DTC_Imports02, tB_CDD_Import.Text + tB_OutputExcelName.Text))
{
if (cDDExcelToDTCDAL.SetExcelStyle_01(tB_CDD_Import.Text + tB_OutputExcelName.Text, "DTC"))
{
MessageBox.Show("导出Excel成功");
}
else
{
MessageBox.Show("导出Excel失败");
}
//MessageBox.Show("导出Excel成功");
}
else
{
MessageBox.Show("导出Excel失败");
}
}
#endregion
#region 共用方法
private string GetDesktopPath()
{
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
return desktopPath;
}
public static string SelectFolder(string defaultFolderPath)
{
FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
folderBrowserDialog.SelectedPath = defaultFolderPath;
DialogResult result = folderBrowserDialog.ShowDialog();
if (result == DialogResult.OK)
{
return folderBrowserDialog.SelectedPath;
}
else
{
return null;
}
}
public static string SelectFile(string defaultFilePath)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = defaultFilePath;
DialogResult result = openFileDialog.ShowDialog();
if (result == DialogResult.OK)
{
return openFileDialog.FileName;
}
else
{
return null;
}
}
#endregion
}
}
4、CDD导入成功界面
5、后记
后续出如何将诊断问卷导入到CDD,和如何解析CDD
标签:CDD,string,CANoe,DTC,Text,model,tB From: https://blog.csdn.net/caoxuefei520/article/details/142151240