是在已经知道文件保持路径的基础上。
string fullPath3 = @"F:\xiaoxiong\1111.csv";//文件格式必须是csv,不能是xls不然乱码,CSV是文本文件,用记事本就能打开,XLS是二进制的文件只有用EXCEL才能打
private List<double> buffer1 = new List<double>();
private void ReadText_Click(object sender, RoutedEventArgs e)
{
FileStream fs = new FileStream(fullPath3, FileMode.Open, FileAccess.Read, FileShare.None);
StreamReader sr = new StreamReader(fs, System.Text.Encoding.GetEncoding(936));//, System.Text.Encoding.GetEncoding(936)
string str;
while (sr.Peek()>=0)
{
str = sr.ReadLine();
string[] strSplit = str.Split(',');
double[] val = new double[strSplit.GetLength(0)];//strSplit.GetLength(0)表示 str这行的有多少元素,下标从零开始
for (int i = 0; i < strSplit.GetLength(0); i++)
{
if (double.TryParse(strSplit[i], out val[i]))//TryParse将数字的字符串表示形式转换为他的等效双精度浮点数
buffer1.Add(val[i]);
}
}
sr.Close();
}
参考:参考了方法3,如果会用其他几种方法,可以留言啊,相互学习。
c#对逗号分隔的字符串警醒提取
string[] strSplit = str.Split(',');
double[] val = new double[strSplit.GetLength(0)];//strSplit.GetLength(0)表示 str这行的有多少元素,下标从零开始
for (int i = 0; i < strSplit.GetLength(0); i++)
{
if (double.TryParse(strSplit[i], out val[i]))//TryParse将数字的字符串表示形式转换为他的等效双精度浮点数
buffer1.Add(val[i]);
可以参照:https://www.cnblogs.com/huachen/p/3382363.html
https://www.cnblogs.com/tommy-huang/p/6004928.html
CSV和xls的区别