/// <summary> /// Read csv from Text /// </summary> /// <param name="text">Input Text</param> /// <param name="encoding">Encoding</param> /// <param name="delimeter">Delimeter between parameters in files</param> /// <param name="quote">Brackets around text</param> /// <param name="emptyLineBehavior">Determines how empty lines are interpreted when reading CSV files</param> /// <returns>true if file read seccessfully</returns> public bool ReadFromText(string text, Encoding encoding, char delimeter = ',', char quote = '"', EmptyLineBehavior emptyLineBehavior = EmptyLineBehavior.NoColumns) { try { Rows.Clear(); var columns = new List <string>(); using (var mem = new MemoryStream()) using (StreamWriter writer = new StreamWriter(mem, encoding)) { writer.Write(text); writer.Flush(); mem.Position = 0; using (var reader = new CsvFileReader(mem, encoding ?? Encoding.UTF8, emptyLineBehavior)) { reader.Delimiter = delimeter; reader.Quote = quote; while (reader.ReadRow(columns)) { var cols = Columns.Count; while (Columns.Count < columns.Count) { //Избегаем Null var dc = new DataColumn(String.Format("Column{0}", cols++), typeof(string)) { AllowDBNull = false, DefaultValue = string.Empty }; Columns.Add(dc); } Rows.Add(columns.ToArray <object>()); } } } FileName = ""; return(true); } catch (Exception ex) { } return(false); }
private static IEnumerable <List <string> > ReadEnumerableCsv(Stream stream, Encoding encoding = null, char delimeter = ',', char quote = '"', EmptyLineBehavior emptyLineBehavior = EmptyLineBehavior.NoColumns) { using ( var csvfile = new CsvFileReader(stream, encoding, emptyLineBehavior) { Delimiter = delimeter, Quote = quote }) { var columns = new List <string>(); while (csvfile.ReadRow(columns)) { yield return(new List <string>(columns)); } } }
/// <summary> /// Read csv file /// </summary> /// <param name="filename">Path to file</param> /// <param name="encoding">Encoding</param> /// <param name="delimeter">Delimeter between parameters in files</param> /// <param name="quote">Brackets around text</param> /// <param name="emptyLineBehavior">Determines how empty lines are interpreted when reading CSV files</param> /// <returns>true if file read seccessfully</returns> public bool ReadFromFile(string filename, Encoding encoding, char delimeter = ',', char quote = '"', EmptyLineBehavior emptyLineBehavior = EmptyLineBehavior.NoColumns) { try { Rows.Clear(); var columns = new List <string>(); using (var reader = new CsvFileReader(filename, encoding ?? Encoding.UTF8, emptyLineBehavior)) { reader.Delimiter = delimeter; reader.Quote = quote; while (reader.ReadRow(columns)) { var cols = Columns.Count; while (Columns.Count < columns.Count) { //Избегаем Null var dc = new DataColumn(String.Format("Column{0}", cols++), typeof(string)) { AllowDBNull = false, DefaultValue = string.Empty }; Columns.Add(dc); } Rows.Add(columns.ToArray <object>()); } } FileName = filename; return(true); } catch (Exception ex) { //throw new Exception("Error while load csv file",ex); //MessageBox.Show(String.Format("Error reading from {0}.\r\n\r\n{1}", filename, ex.Message)); } return(false); }
/// <summary> /// Read csv file /// </summary> /// <param name="filename">Path to file</param> /// <param name="encoding">Encoding</param> /// <param name="delimeter">Delimeter between parameters in files</param> /// <param name="quote">Brackets around text</param> /// <param name="emptyLineBehavior">Determines how empty lines are interpreted when reading CSV files</param> /// <returns>true if file read seccessfully</returns> public bool ReadFromFile(string filename, Encoding encoding=null,char delimeter = ',', char quote = '"', EmptyLineBehavior emptyLineBehavior = EmptyLineBehavior.NoColumns) { try { this.Rows.Clear(); var columns = new List<string>(); using (var reader = new CsvFileReader(filename,encoding??Encoding.UTF8 ,emptyLineBehavior)) { reader.Delimiter = delimeter; reader.Quote = quote; while (reader.ReadRow(columns)) { var cols = Columns.Count; while (this.Columns.Count < columns.Count) { //Избегаем Null var dc = new DataColumn(String.Format("Column{0}", cols++), typeof(string)) { AllowDBNull = false, DefaultValue = "" }; this.Columns.Add(dc); } this.Rows.Add(columns.ToArray<object>()); } } FileName = filename; return true; } catch (Exception ex) { //throw new Exception("Error while load csv file",ex); //MessageBox.Show(String.Format("Error reading from {0}.\r\n\r\n{1}", filename, ex.Message)); } return false; }