/// <summary> /// Reads a delimited file into an array of an array of strings. /// </summary> /// <param name="stream"></param> /// <param name="delimiter"></param> /// <param name="ignoreHeader"></param> /// <returns></returns> public static string[][] ReadDelimitedFileFromStream( Stream stream, DelimiterType delimiter, bool ignoreHeader) { // converts the stream into a text reader. TextReader tr = new StreamReader(stream); // get the lines. StringReader strReader = new StringReader(tr.ReadToEnd()); List <string> lines = new List <string>(); bool isheader = ignoreHeader; while ((strReader.Peek() > -1)) { if (isheader) { isheader = false; strReader.ReadLine(); } else { lines.Add(strReader.ReadLine()); } } // get the columns. string[][] values = new string[lines.Count][]; char split = DelimitedFileHandler.GetDelimiterChar(delimiter); for (int idx = 0; idx < lines.Count; idx++) { values[idx] = lines[idx].Split(split); } return(values); }
/// <summary> /// Reads a delimited file. /// </summary> /// <param name="reporter"></param> /// <param name="file"></param> /// <param name="delimiter"></param> /// <param name="firstRowHasHeaders"></param> /// <returns></returns> public static string[][] ReadDelimitedFile( IProgressReporter reporter, Stream stream, DelimiterType delimiter, bool firstRowHasHeaders) { if (reporter == null) { reporter = EmptyProgressReporter.Instance; } char delimiterChar = DelimitedFileHandler.GetDelimiterChar(delimiter); List <string[]> delimited_data_set; //int iCounter = 0; ProgressStatus status; // Build dataset delimited_data_set = new List <string[]>(); //Add the table 'Read the delimited file System.Text.Encoding enc = null; enc = System.Text.Encoding.GetEncoding("iso-8859-1"); StringBuilder strBuild = new StringBuilder(Convert.ToInt32(stream.Length)); // report the status. status = new ProgressStatus(); status.Status = ProgressStatus.ProgressStatusEnum.Busy; status.CurrentNumber = 0; status.Message = "Opening file..."; reporter.Report(status); for (int i = 0; i <= Convert.ToInt32(stream.Length) - 1; i++) { byte[] bytes = new byte[] { Convert.ToByte(stream.ReadByte()) }; strBuild.Append(enc.GetString(bytes, 0, bytes.Length)); } string str = strBuild.ToString(); StringReader strReader = new StringReader(str); List <string> lines = new List <string>(); while ((strReader.Peek() > -1)) { lines.Add(strReader.ReadLine()); } // Now add in the Rows // report the status. status = new ProgressStatus(); status.Status = ProgressStatus.ProgressStatusEnum.Busy; status.CurrentNumber = 0; status.Message = "Reading file..."; reporter.Report(status); int startLine = 0; if (firstRowHasHeaders) { startLine = 1; } //Loop while there are rows in the delimited file for (int l = startLine; l < lines.Count; l++) { string line = lines[l]; //Add the items to the DataSet delimited_data_set.Add(line.Split(delimiterChar)); // report the status. status = new ProgressStatus(); status.Status = ProgressStatus.ProgressStatusEnum.Busy; status.CurrentNumber = l; status.TotalNumber = lines.Count - 1; status.Message = "Reading file..."; reporter.Report(status); } // report the status. status = new ProgressStatus(); status.Status = ProgressStatus.ProgressStatusEnum.Succeeded; status.CurrentNumber = 0; status.Message = "Reading file..."; reporter.Report(status); return(delimited_data_set.ToArray <string[]>()); }
/// <summary> /// Reads a delimited file into an array of an array of strings. /// </summary> /// <param name="stream"></param> /// <param name="delimiter"></param> /// <returns></returns> public static string[][] ReadDelimitedFileFromStream( Stream stream, DelimiterType delimiter) { return(DelimitedFileHandler.ReadDelimitedFileFromStream(stream, delimiter, true)); }