网上资料
截至解决问题为止,我在网络上查找的相关资料都是使用DataTable或是通过Workbook,我使用过程中发现作为一个初学者,这些dll文件的使用并不像我想的那么方便,我只需要存储数据,并不需要使用excel里面的填充颜色,字体颜色等等功能。
转化展示
转化方法
每个数字以“,”间隔开来,再保存成csv文件的形式即可
代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace 数据保存csv或是xls
{
class Program
{
static void Main(string[] args)
{
Test ts = new Test();
ts.GetData();
//StringBuilder sb = new StringBuilder();
//sb.AppendLine("1,22222222,3,4,5");
//StreamWriter sw = new StreamWriter("From.csv", true, Encoding.UTF8);
//sw.WriteLine(sb);
//sw.Flush();
//sw.Close();
Console.ReadKey();
}
class Test
{
public void GetData()
{
string str = @"\19\09\02\0-1000\测试log";
//string path = Directory.GetCurrentDirectory() + "\\Test" + str;//文件路径
Console.WriteLine(str);
var files = Directory.GetFiles(str, "*txt");
foreach (string file in files)
{
string strData = "";
Console.WriteLine(file);
string[] strLines = File.ReadAllLines(file);//读取文件内容
strData += (TsMainValue(strLines, "Get Scaned MLB SN:", 0) + ",");
strData += (TsMainValue(strLines, "IR:", 0) + ",");
strData += (TsMainValue(strLines, "TESTVDC:", 0) + ",");
strData += (TsMainValue(strLines, "CHARGE CHECK:", 0) + ",");
strData += (TsMainValue(strLines, "ONIN NO WALL:", 0) + ",");
strData += (TsMainValue(strLines, "SIDEWALL NO WALL:", 0) + ",");
strData += (TsMainValue(strLines, "ONIN WALL:", 0) + ",");
strData += (TsMainValue(strLines, "SIDEWALL WALL:", 0) + ",");
strData += (TsMainValue(strLines, "CLIFF0:", 0) + ",");
strData += (TsMainValue(strLines, "Magnet 0:", 0) + ",");
strData += (TsMainValue(strLines, "CLIFF1:", 0) + ",");
strData += (TsMainValue(strLines, "Magnet 1:", 0) + ",");
strData += (TsMainValue(strLines, "COLLISION:", 1) + ",");
Console.WriteLine(strData);
StringBuilder sb = new StringBuilder();
sb.AppendLine(strData);
StreamWriter sw = new StreamWriter("From.csv", true, Encoding.UTF8);
sw.WriteLine(sb);
sw.Flush();
sw.Close();
Console.ReadKey();
}
}
private string TsMainValue(string[] strLines, string strValue, int iCount)
{
for (int i = 0; i < strLines.Length; i++)
{
if ((strLines[i].Contains(strValue)) && (iCount == 0))
{
string strData = "";
if ((strLines[i].Contains("_")) && (!strValue.Contains("_")))
{
strData = strLines[i].Replace(strValue, "").Replace("_", ",");
}
else
{
strData = strLines[i].Replace(strValue, "");
}
return strData;
}
else if ((strLines[i].Contains(strValue)) && (iCount == 1))
{
string strData = "";
if ((strLines[i].Contains("_")) && (!strValue.Contains("_")))
{
string[] strArr = strLines[i].Replace(strValue, "").Split('_');
strData = (
strArr[2] + "," + strArr[5] + "," + strArr[8] + "," + strArr[9] + "," +
strArr[10] + "," + strArr[11] + "," + strArr[13] + "," + strArr[14] + "," + strArr[19] + "," +
strArr[22] + "," + strArr[25] + "," + strArr[28] + "," + strArr[29] + "," +
strArr[30] + "," + strArr[31] + "," + strArr[33] + "," + strArr[34] + "," + strArr[39]);
}
else
{
strData = strLines[i].Replace(strValue, "");
}
return strData;
}
}
return null;
}
}
}
}