using System; using System.Collections.Generic; using System.Text; namespace Apewer.Internals { internal class CsvHelper { /// 读取 .CSV 文件。 public static List> ReadCSV(string argText) { var result = new List>(); if (string.IsNullOrEmpty(argText)) return result; var rows = argText.Split('\n'); var vramarray = new List(); foreach (var row in rows) { var list = ResolveCSV(row); if (list.Count > 0) result.Add(list); } return result; } /// 解析 .CSV 文件。 private static List ResolveCSV(string argRowText) { if (string.IsNullOrEmpty(argRowText)) return new List(); var trim = argRowText.Trim(); if (string.IsNullOrEmpty(trim)) return new List(); var list = new List(); var quote = false; var cell = ""; for (int i = 0; i < argRowText.Length; i++) { var vchar = argRowText[i]; switch (vchar) { case '"': if (!quote) cell = ""; quote = !quote; break; case ',': if (quote) { cell += vchar; } else { list.Add(cell.Trim()); cell = ""; } break; default: cell += vchar; break; } } if (!string.IsNullOrEmpty(cell)) { list.Add(cell.Trim()); cell = ""; } return list; } } }