C#读取txt文件、excel文件

yu-beng / 2023-07-20 / 原文

1、 TXT

文本内容较小:

  将txt读取返回为 string

string str1 = File.ReadAllText(@"c:\temp\a.txt"); 

  将txt读取返回为 string[ ]

string[] strs1 = File.ReadAllLines(@"c:\temp\a.txt"); 

 

文本较多:采用流的方式Stream

//采用流的方式读,适合较多文本内容
StreamReader sR1 = new StreamReader(filesPath);//先实例化StreamReader类
json = sR1.ReadToEnd(); //文本内容 全部读完
sR1.Close();

 

 

2、EXCEL

 

 

 

 

 

 

参考: