static void Main(string[] args) { string line; List<EmployeeInfo> list = new List<EmployeeInfo>(); using (StreamReader file = new StreamReader(@"test.txt")) { EmployeeInfo model = null; while ((line = file.ReadLine()) != null) { char[] delimiters = new char[] { '\t' }; string[] parts = line.Split(delimiters, StringSplitOptions.RemoveEmptyEntries); model = new EmployeeInfo() { CompanyID = Int32.Parse(parts[0]), EmployeeID = parts[1], HireDate = parts[2] }; list.Add(model); } file.Close(); } // Suspend the screen. Console.ReadLine(); }
文本文件的内容是直接从Excel中拷过来的3列数据,列直接默认是有一个制表符的,就是在文本文件中按下Tab健的那个类似于空格的符号,上面取到数据后用制表符分隔获取不同列的内容。