单击导入数据按钮,弹出选择导入数据的目录窗体,如图
选中文件,点击打开便成功导入数据了,下面是导入的数据,如图
//执行导入数据方法
private void ImportDataRecordingDataClick()
{
try
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "文本文档|*.txt";
//如果用户没有选择文件并确定则直接返回
if (openFileDialog.ShowDialog() != true)
{
return;
}
//获得文件名包括路径
string filePath = openFileDialog.FileName;
string line = string.Empty;
//UnicodeEncoding.GetEncoding("GB2312")为简体中文编码格式
System.IO.StreamReader streamReader = new System.IO.StreamReader(filePath,UnicodeEncoding.GetEncoding("GB2312"));
//清空数据
storageImportData.Clear();
// 从文件读取并显示行,直到文件的末尾
while ((line = streamReader.ReadLine()) != null)
{
//将读取的字符串按"制表符/t“和””“分割成数组
string[] arrayLine = line.Split(new char[] { '\t' }, StringSplitOptions.RemoveEmptyEntries);
if (arrayLine.Length == 7)
{
storageImportData.Add(new DataRecordingModel
{
RealtimeTime = arrayLine[0],
Channel = arrayLine[1],
MessageID = arrayLine[2],
EventType = arrayLine[3],
MessageType = arrayLine[4],
MessageLength = arrayLine[5],
RealtimeData = arrayLine[6],
});
}
}
Messenger.Default.Send("共导入" + storageImportData.Count + "条数据", MessageCenter.MSG_PROMPTMESSAGE)
}
catch (Exception e)
{
Messenger.Default.Send("数据异常", MessageCenter.MSG_PROMPTMESSAGE);
}
}