当前位置:   article > 正文

C#Excel数据读取_c# 读取excel

c# 读取excel

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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

在这里插入图片描述
数据读取

			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
                }
            }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

在这里插入图片描述
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/698803
推荐阅读
相关标签
  

闽ICP备14008679号