赞
踩
C#Excel数据读取
数据写入
string fileDir; string fileNameExl; FileStream dataFile; //string data = "1\t2";//换列 //string data = "1\n2";//换行 fileDir = "D:\\Model\\"; fileNameExl = fileDir + "数据输出" + ".xls"; dataFile = new FileStream(fileNameExl, FileMode.Append, FileAccess.Write); string data = "Row" + "\t" + "Column" + "\t" + "FuzhuDianNum" + "\n"; for (int i = 0; i < FuzhuxianColumn.Count; i++) { data = data + Convert.ToDouble(FuzhuxianRow[i]).ToString() + "\t" + Convert.ToDouble(FuzhuxianColumn[i]).ToString() + "\t" + Convert.ToInt32(FuzhuDianNum[i]).ToString() + "\n"; } StreamWriter writer = new StreamWriter(dataFile); writer.Write(data); writer.Flush(); writer.Close(); StringBuilder sb = new StringBuilder(""); sb.Append(Environment.NewLine);
数据读取
fileDir = "D:\\Model\\"; fileNameExl = fileDir + "数据输出" + ".xls"; //dataFile = new FileStream(fileNameExl, FileMode.Append, FileAccess.Read); StreamReader reader = new StreamReader(fileNameExl, Encoding.Default); string str = null;//先声明一个字符串 String d=""; while ((str = reader.ReadLine()) != null)//判断读取到的字符串是为null,如果为null,说明已经读取到文件末尾 { d = reader.ReadToEnd(); MessageBox.Show(d.ToString()); } d = System.Text.RegularExpressions.Regex.Replace(d, @"\s+", ","); //替换字符必须在原字符串中没有出现 string[] arr = d.Split(','); //将读取的字符串以空格分割,读取记录 for (int i = 0; i < arr.Length-1; i++) { MessageBox.Show(arr[i].ToString()); if ((i + 1) % 3 == 0) { ExcelPointNum_Read.Add(arr[i]);//编号 } if (i % 3 == 0) { ExcelRow_Read.Add(arr[i]);//Row } if ((i - 1) % 3 == 0) { ExcelColumn_Read.Add(arr[i]);//Column } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。