/// <summary> /// Reads the CSV. /// </summary> /// <param name="fileName">Name of the file.</param> /// <param name="fileSchema">The file schema.</param> /// <param name="count">The maximal count of readed rows.</param> /// <returns>The two dimensional string list parsed from the csv file.</returns> /// <remarks>Documented by Dev05, 2007-08-31</remarks> public static List <List <string> > ReadCSV(string fileName, FileSchema fileSchema, int count) { if (fileSchema == null) { throw new ArgumentNullException(); } List <List <string> > data = new List <List <string> >(); using (StreamReader stream = new StreamReader(fileName, fileSchema.Encoding)) { //swallow the first line (headers) - CsvReader has a problem with multiple headers with the same name if (fileSchema.hasHeaders && !stream.EndOfStream) { stream.ReadLine(); } //initialize the csvreader class and read out the values CsvReader csv = new CsvReader(stream, false, fileSchema.Delimiter, fileSchema.Quote, fileSchema.Escape, fileSchema.Comment, fileSchema.trimSpaces); csv.DefaultParseErrorAction = ParseErrorAction.AdvanceToNextLine; csv.MissingFieldAction = MissingFieldAction.ReplaceByEmpty; csv.SupportsMultiline = false; int fieldCount = csv.FieldCount; try { while (csv.ReadNextRecord() && csv.CurrentRecordIndex < count) { List <string> row = new List <string>(); for (int i = 0; i < fieldCount; i++) { //[ML-968] Remove control characters and add field to row row.Add(System.Text.RegularExpressions.Regex.Replace(csv[i], @"[\p{C}]", string.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase)); } data.Add(row); } } catch { throw; } finally { if (stream != null) { stream.Close(); } } } return(data); }
/// <summary> /// Reads the CSV. /// </summary> /// <param name="fileName">Name of the file.</param> /// <param name="fileSchema">The file schema.</param> /// <returns>The two dimensional string list parsed from the csv file.</returns> /// <remarks>Documented by Dev05, 2007-08-31</remarks> public static List <List <string> > ReadCSV(string fileName, FileSchema fileSchema) { return(ReadCSV(fileName, fileSchema, int.MaxValue)); }
/// <summary> /// Reads the CSV. /// </summary> /// <param name="fileName">Name of the file.</param> /// <param name="fileSchema">The file schema.</param> /// <returns>The two dimensional string list parsed from the csv file.</returns> /// <remarks>Documented by Dev05, 2007-08-31</remarks> public static List<List<string>> ReadCSV(string fileName, FileSchema fileSchema) { return ReadCSV(fileName, fileSchema, int.MaxValue); }
/// <summary> /// Reads the CSV. /// </summary> /// <param name="fileName">Name of the file.</param> /// <param name="fileSchema">The file schema.</param> /// <param name="count">The maximal count of readed rows.</param> /// <returns>The two dimensional string list parsed from the csv file.</returns> /// <remarks>Documented by Dev05, 2007-08-31</remarks> public static List<List<string>> ReadCSV(string fileName, FileSchema fileSchema, int count) { if (fileSchema == null) throw new ArgumentNullException(); List<List<string>> data = new List<List<string>>(); using (StreamReader stream = new StreamReader(fileName, fileSchema.Encoding)) { //swallow the first line (headers) - CsvReader has a problem with multiple headers with the same name if (fileSchema.hasHeaders && !stream.EndOfStream) stream.ReadLine(); //initialize the csvreader class and read out the values CsvReader csv = new CsvReader(stream, false, fileSchema.Delimiter, fileSchema.Quote, fileSchema.Escape, fileSchema.Comment, fileSchema.trimSpaces); csv.DefaultParseErrorAction = ParseErrorAction.AdvanceToNextLine; csv.MissingFieldAction = MissingFieldAction.ReplaceByEmpty; csv.SupportsMultiline = false; int fieldCount = csv.FieldCount; try { while (csv.ReadNextRecord() && csv.CurrentRecordIndex < count) { List<string> row = new List<string>(); for (int i = 0; i < fieldCount; i++) //[ML-968] Remove control characters and add field to row row.Add(System.Text.RegularExpressions.Regex.Replace(csv[i], @"[\p{C}]", string.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase)); data.Add(row); } } catch { throw; } finally { if (stream != null) stream.Close(); } } return data; }
/// <summary> /// Generates the file schema for the csv parser. /// </summary> /// <returns>The Fileschema.</returns> /// <remarks>Documented by Dev02, 2007-12-14</remarks> private FileSchema GenerateFileSchema() { FileSchema fileSchema = new FileSchema(); fileSchema.Encoding = ((EncodingWrapper)comboBoxCharset.SelectedItem).Encoding; fileSchema.hasHeaders = checkBoxHeader.Checked; char delimiter; switch (cmbFileFormat.SelectedIndex) { case 1: delimiter = ';'; break; case 2: delimiter = '\t'; break; case 3: default: delimiter = ','; break; } fileSchema.Delimiter = delimiter; return fileSchema; }