读取csv 文件 c#
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ReadCSV { class Program { public static void Main(string[] args) { List<Contract> list = ReadCSV(@"C:\Users\Administrator\Source\Repos\ReadCSV\ReadCSV\bin\Debug\EE4.csv"); } public static List<Contract> ReadCSV( string path) { List<Contract> list = new List<Contract>(); StreamReader sr = new StreamReader("EE6.csv", Encoding.UTF8); String line; while ((line = sr.ReadLine()) != null) { if (line.Contains("DisplayName")) continue; list.Add(readlineToContract(line, 15)); } return list; } public static Contract readlineToContract(string line, int count) { Contract contract = new Contract(); string[] strArray = line.Split(','); if (count == strArray.Count()) { contract.Name = strArray[4]; contract.DisplayName = strArray[5]; contract.Type = strArray[8]; contract.Category = strArray[9]; contract.State = strArray[10]; contract.Stage = strArray[11]; } else { //deal with doublue " contains , List<string> temp = new List<string>(); for (int i = 0; i < line.ToArray().Length; i++) { if (line[i] != '"') { int index = line.IndexOf(",", i); if (index == -1) temp.Add(line.Substring(i)); else { temp.Add(line.Substring(i, index - i)); i = index; } } else if (line[i] == ',') { temp.Add(string.Empty); } else if (line[i] == '"') { int index = line.IndexOf("\"", i+1); temp.Add(line.Substring(i+1, index - i-1)); i = index; } } if (line.EndsWith(",")) temp.Add(string.Empty); contract.Name = temp[4]; contract.DisplayName = temp[5]; contract.Type = temp[8]; contract.Category = temp[9]; contract.State = temp[10]; contract.Stage = temp[11]; } return contract; } } class Contract { public string Name { set; get; } public string DisplayName { set; get; } public string Type { set; get; } public string Category { set; get; } public string State { set; get; } public string Stage { set; get; } } }
I'm fine, it's ok
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析