public IEnumerable<string> Run() { var onlyLogErrors = bool.Parse(ConfigurationManager.AppSettings["OnlyLogErrors"]); using (GenericParser parser = new GenericParser()) { parser.SetDataSource(_command.Datafile); parser.ColumnDelimiter = ','; parser.FirstRowHasHeader = true; parser.SkipStartingDataRows = 0; parser.MaxBufferSize = 4096; parser.MaxRows = 1000000; parser.TextQualifier = '\"'; _connection.Open(); var count = 0; while (parser.Read()) { SetParameters(parser); count++; Exception exception = null; var affected = 0; try { affected = _adaptor.Execute(); } catch (Exception ex) { exception = ex; } if (exception != null) yield return exception.ToString(); else if (affected == 0) yield return count.ToString() + " : " + FailMessage(parser); else if (!onlyLogErrors) yield return count.ToString() + " : " + SuccessMessage(parser); else yield return ""; } _connection.Close(); } }
static Data ReadFieldData(string fname) { var d = new Data(); var rows = new List<double[]>(); var labels = new List<string>(); using (GenericParser parser = new GenericParser()) { parser.SetDataSource(fname); parser.ColumnDelimiter = ','; parser.FirstRowHasHeader = false; parser.TextQualifier = '\"'; while(parser.Read()) { rows.Add(StringKernalFunction(parser[0].Trim())); labels.Add(parser[1]); } } d.Features = rows.ToArray(); d.Labels = labels.ToArray(); return d; }
private void btn2_Click(object sender, EventArgs e) { //ArgumentParser argParser = new ArgumentParser(args); String dataSource = ofd1.FileName;//argParser["o"]; if (dataSource != null) { try { GenericParser parser = new GenericParser(); parser.SetDataSource(dataSource); parser.FirstRowHasHeader = true; parser.Read(); int columnCount = parser.ColumnCount; //Work out the names of the Accounts we need to sum List<string> ItemNames = new List<string>(); for (int i = 0; i < columnCount; i++) { ItemNames.Add (parser.GetColumnName(i)); } // //Work out the numbers we need to sum // List<Decimal> DecimalsToSum = new List<decimal>(); // for (int decimalCounter = 0; decimalCounter < columnCount; decimalCounter++) // { // DecimalsToSum.Add(Decimal.Parse(parser[decimalCounter])); // } // // List<Tuple<Decimal,List<String>>> SumsAndOps = new List<Tuple<Decimal,List<String>>>(); // List<Tuple<Decimal,Decimal,List<String>>> AbsSumsAndOps = new List<Tuple<Decimal,Decimal,List<String>>>(); // List<String> OpsSoFar = new List<string>(); // // callsCount = 0; // BuildPathsAndSumsTogether (SumsAndOps, AbsSumsAndOps, DecimalsToSum, OpsSoFar, 0, 0, columnCount); // SumsAndOps.Sort( (a,b) => a.Item1.CompareTo(b.Item1) ); // AbsSumsAndOps.Sort( (a,b) => a.Item1.CompareTo(b.Item1) ); //Work out the numbers we need to sum List<Decimal> DecimalsToSum = new List<decimal>(); for (int decimalCounter = 0; decimalCounter < columnCount; decimalCounter++) { DecimalsToSum.Add(Decimal.Parse(parser[decimalCounter])); } //List<Tuple<Decimal,List<String>>> SumsAndOps = new List<Tuple<Decimal,List<String>>>(); List<String> OpsSoFar = new List<string>(); callsCount = 0; maxAbsDiff = -9999; BuildPathsAndSumsTogetherMinimal(DecimalsToSum, OpsSoFar, 0, 0, columnCount); String stuff = "yarr"; Decimal outcome = maxAbsDiff; //BuildPathsAndSumsTogetherNoAbs (SumsAndOps, DecimalsToSum, OpsSoFar, 0, 0, columnCount);(SumsAndOps, DecimalsToSum, OpsSoFar, 0, 0, columnCount); //SumsAndOps.Sort( (a,b) => a.Item1.CompareTo(b.Item1) ); // int countSuccess = 0; // textBox1.Text = ""; // foreach(Tuple<Decimal,List<string>> sumWithOps in SumsAndOps) { // if(sumWithOps.Item1 == 0) { // countSuccess++; // // for(int account = 0; account < ItemNames.Count; account++) { // textBox1.Text += (ItemNames[account].PadLeft(30) + ":" // + DecimalsToSum[account].ToString().PadLeft(15).PadRight(4) // + "(" + sumWithOps.Item2[account] + ")" // + Environment.NewLine); // Console.WriteLine(ItemNames[account]+":"+DecimalsToSum[account]+":"+sumWithOps.Item2[account]); // } // textBox1.Text += ("Balances To".PadLeft(30) + ":" // + sumWithOps.Item1.ToString().PadLeft(15) // + Environment.NewLine + Environment.NewLine); // Console.WriteLine("Balances To" +":"+ sumWithOps.Item1); // Console.WriteLine(); // } // } // // if(countSuccess == 0) { // int n = 20; // //// textBox1.Text = "This combination of numbers has no possible way to balance. The " + n + " solutions closest to 0 follow." + Environment.NewLine; //// //// for(int closeResultPos = 0 ; closeResultPos < n || closeResultPos >= AbsSumsAndOps.Count; closeResultPos++) { //// Tuple<Decimal,Decimal,List<string>> sumWithOps = AbsSumsAndOps[closeResultPos]; //// for(int account = 0; account < ItemNames.Count; account++) { //// textBox1.Text += (ItemNames[account].PadLeft(30) + ":" //// + DecimalsToSum[account].ToString().PadLeft(15).PadRight(4) //// + "(" + sumWithOps.Item3[account] + ")" //// + Environment.NewLine); //// Console.WriteLine(ItemNames[account]+":"+DecimalsToSum[account]+":"+sumWithOps.Item3[account]); //// } //// //// textBox1.Text += ("Balances To".PadLeft(30) + ":" //// + sumWithOps.Item2.ToString().PadLeft(15) //// + Environment.NewLine + Environment.NewLine); //// Console.WriteLine("Balances To" +":"+ sumWithOps.Item2); //// Console.WriteLine(); //// } // } } catch (Exception ex) { textBox1.Text = ex.Message; } } }
public Dictionary <int, Gemeente> GetGemeenten() { #if DEBUG DebugWriter.WriteLine("-> GemeentenProvider::GetGemeenten"); var timer = new Stopwatch(); timer.Start(); #endif Dictionary <int, Gemeente> gemeenten = new Dictionary <int, Gemeente>(); using GenericParser gemeentenReader = new GenericParser(Config.Path + "/" + Config.Gemeentenamen) { ColumnDelimiter = ';', FirstRowHasHeader = true, MaxBufferSize = 4096 }; while (gemeentenReader.Read()) { if (gemeentenReader[2].Trim() != _taalCode) { continue; } gemeenten.Add(int.Parse(gemeentenReader[1].Trim()), new Gemeente { NaamId = int.Parse(gemeentenReader[0].Trim()), Id = int.Parse(gemeentenReader[1].Trim()), TaalCode = gemeentenReader[2].Trim(), Naam = gemeentenReader[3].Trim() }); } DebugWriter.WriteLine("Gemeenten: " + gemeenten.Values.Count); #if DEBUG timer.Stop(); TimeSpan timeTaken = timer.Elapsed; DebugWriter.WriteLine("<- GemeentenProvider::GetGemeenten: " + timeTaken.ToString(@"m\:ss\.fff")); #endif return(gemeenten); }
private static IEnumerable <SeriesEntry> ReadSeriesEntries(string file) { using (var parser = new GenericParser(file)) { parser.ColumnDelimiter = '\t'; parser.FirstRowHasHeader = true; parser.TrimResults = true; parser.MaxBufferSize = parser.MaxBufferSize * 10; while (parser.Read()) { if (!String.IsNullOrWhiteSpace(parser["Level"])) { yield return(new SeriesEntry { Id = parser["ID"], Description = parser["Description"], Reference = parser["Reference"], Title = parser["Title"], RelatedMaterial = parser["Related Material"], SeperatedMaterial = parser["Separated Material"], CoveringDates = Date.Parse(parser["Covering Dates"]).ToArray() }); } } } }
public static DataTable CsvToDataTable(string path, Dictionary <string, Type> expectedColumnsAndTypes, char seperator, string dateTimeFormat, CultureInfo cultureInfo = null) { if (cultureInfo == null) { cultureInfo = CultureInfo.InvariantCulture; } DataTable dt = new DataTable(); foreach (var pair in expectedColumnsAndTypes) { dt.Columns.Add(pair.Key, pair.Value); } using (GenericParser parser = new GenericParser()) { parser.SetDataSource(path); parser.ColumnDelimiter = seperator; parser.FirstRowHasHeader = true; //parser.SkipStartingDataRows = 10; parser.MaxBufferSize = 4096; parser.MaxRows = int.MaxValue; parser.TextQualifier = '\"'; parser.SkipEmptyRows = true; while (parser.Read()) { DataRow row = dt.NewRow(); foreach (var pair in expectedColumnsAndTypes) { //OrderUuid,Exchange,Type,Quantity,Limit,CommissionPaid,Price,Opened,Closed string value = parser[pair.Key]; if (string.IsNullOrWhiteSpace(value)) { continue; } if (pair.Value == typeof(decimal)) { row[pair.Key] = string.IsNullOrEmpty(value) ? 0 : decimal.Parse(value, cultureInfo); } else if (pair.Value == typeof(DateTime)) { row[pair.Key] = DateTime.ParseExact(value, dateTimeFormat, cultureInfo); } else { row[pair.Key] = value; } } dt.Rows.Add(row); } } return(dt); }
public IEnumerable <string> Run() { var onlyLogErrors = bool.Parse(ConfigurationManager.AppSettings["OnlyLogErrors"]); using (GenericParser parser = new GenericParser()) { parser.SetDataSource(_command.Datafile); parser.ColumnDelimiter = ','; parser.FirstRowHasHeader = true; parser.SkipStartingDataRows = 0; parser.MaxBufferSize = 4096; parser.MaxRows = 1000000; parser.TextQualifier = '\"'; _connection.Open(); var count = 0; while (parser.Read()) { SetParameters(parser); count++; Exception exception = null; var affected = 0; try { affected = _adaptor.Execute(); } catch (Exception ex) { exception = ex; } if (exception != null) { yield return(exception.ToString()); } else if (affected == 0) { yield return(count.ToString() + " : " + FailMessage(parser)); } else if (!onlyLogErrors) { yield return(count.ToString() + " : " + SuccessMessage(parser)); } else { yield return(""); } } _connection.Close(); } }
public void FindWordConcatenationInLookup(Dictionary <string, string> lookup) { using (_parser) { while (_parser.Read()) { string word = _parser[1]; FindWord(word, lookup); } } }
/// <summary> /// Tests if the document is a valid traditionalInput document. /// </summary> /// <param name="documentText">Document input text.</param> /// <returns></returns> private static bool ValidateMetadataInput(string documentText) { Contract.Requires(documentText != null, nameof(documentText)); // Determine if first row has title or abstract, and if so, its likely trying to be a metadatainput // Throw errors. var firstLineLength = Math.Max(new[] { documentText.IndexOf("\r\n", StringComparison.InvariantCulture), documentText.IndexOf('\n', StringComparison.InvariantCulture), documentText.IndexOf('\r', StringComparison.InvariantCulture) }.Min(), 0); var firstLine = documentText.Substring(0, firstLineLength); var titles = firstLine.Split(new char[] { ',', '\t' }).Select((title) => title.Trim().Trim('"').ToUpperInvariant()); var throwErrors = titles.Contains("TITLE") || titles.Contains("ABSTRACT"); try { using (GenericParser parser = new GenericParser(new StringReader(documentText))) { // NEVER have buffer issues! parser.MaxBufferSize = int.MaxValue / 4; parser.FirstRowHasHeader = true; if (documentText.Contains('\t', StringComparison.InvariantCulture)) { parser.ColumnDelimiter = '\t'; } parser.TrimResults = true; while (parser.Read()) { if (string.IsNullOrWhiteSpace(parser["title"]) && string.IsNullOrWhiteSpace(parser["abstract"])) { throw new FormatException($"Row {parser.DataRowNumber} must contain at least a title or abstract field."); } if (!string.IsNullOrWhiteSpace(parser["link"]) && !(parser["link"].StartsWith("https://", StringComparison.InvariantCultureIgnoreCase) || parser["link"].StartsWith("http://", StringComparison.InvariantCultureIgnoreCase))) { throw new FormatException($"Row {parser.DataRowNumber} has a link that is not formatted like a url."); } } } return(true); } catch (Exception ex) { if (throwErrors == true) { throw new BrowseCloudValidationException(ex.Message); } return(false); } }
private void Read() { #if DEBUG DebugWriter.WriteLine("-> StratenProvider::Read"); var timer = new Stopwatch(); timer.Start(); #endif using GenericParser stratenGemeenteReader = new GenericParser(Config.Path + "/" + Config.StraatnaamGemeenteId) { ColumnDelimiter = ';', FirstRowHasHeader = true, MaxBufferSize = 4096 }; while (stratenGemeenteReader.Read()) { if (_gemeenteStraat.ContainsKey(int.Parse(stratenGemeenteReader[1].Trim()))) { _gemeenteStraat[int.Parse(stratenGemeenteReader[1].Trim())].Add(int.Parse(stratenGemeenteReader[0].Trim())); } else { _gemeenteStraat.Add(int.Parse(stratenGemeenteReader[1].Trim()), new List <int> { int.Parse(stratenGemeenteReader[0].Trim()) }); } } DebugWriter.WriteLine("Straten/gemeenten links: " + _gemeenteStraat.Count); using GenericParser stratenReader = new GenericParser(Config.Path + "/" + Config.Straatnamen) { ColumnDelimiter = ';', FirstRowHasHeader = true, MaxBufferSize = 4096 }; while (stratenReader.Read()) { var id = stratenReader[0] != null?int.Parse(stratenReader[0].Trim()) : -1; var naam = stratenReader[1] != null ? stratenReader[1].Trim() : ""; if (id != -1 && !string.IsNullOrEmpty(naam)) { _straten.Add(id, naam); } } DebugWriter.WriteLine("Straten: " + _straten.Count); //#if DEBUG // timer.Stop(); // TimeSpan timeTaken = timer.Elapsed; // DebugWriter.WriteLine("<- StratenProvider::Read: " + timeTaken.ToString(@"m\:ss\.fff")); //#endif }
/// <summary> /// Uses the <see cref="GenericParser"/> to read the Csv file. /// </summary> private static void _GenericParser11Csv() { string s; using (GenericParser gp = new GenericParser(PerformanceTests.CSV_DATA_FILE)) { gp.MaxBufferSize = 4096; while (gp.Read()) { for (int i = 0; i < gp.ColumnCount; ++i) { s = gp[i]; } } } }
private DataTable ReadDataFromFile() { var tbl = new DataTable(); using (var parser = new GenericParser()) { parser.TextFieldType = FieldType.Delimited; parser.ColumnDelimiter = config.Delimiter[0]; parser.FirstRowHasHeader = true; parser.FirstRowSetsExpectedColumnCount = true; parser.TrimResults = false; //parser.SkipStartingDataRows = 10; parser.MaxBufferSize = 1048576; //parser.MaxRows = 500; parser.TextQualifier = '\"'; parser.EscapeCharacter = null; parser.SetDataSource(config.InputFile, Encoding.UTF8); bool doSetup = true; while (parser.Read()) { if (doSetup) { for (int i = 0; i < parser.ColumnCount; i++) { string colName = parser.GetColumnName(i); tbl.Columns.Add(colName, typeof(string)); } doSetup = false; } var row = tbl.NewRow(); for (int i = 0; i < parser.ColumnCount; i++) { string colName = parser.GetColumnName(i); row[colName] = parser[colName]; } tbl.Rows.Add(row); } } return(tbl); }
public void ReadCsv() { Dictionary <int, Team> teams = new Dictionary <int, Team>(); using GenericParser footballParser = new GenericParser(Config.Path + "/" + Config.FileName) { ColumnDelimiter = ',', FirstRowHasHeader = true, MaxBufferSize = 4096 }; while (footballParser.Read()) { var spelernaam = footballParser[0] != null ? footballParser[0].Trim() : ""; var rugnummer = footballParser[1] != null?int.Parse(footballParser[1]) : -1; var clubNaam = footballParser[2] != null ? footballParser[2].Trim() : ""; var waarde = footballParser[3] != null?int.Parse(footballParser[3].Replace(" ", "")) : -1; var stamnummer = footballParser[4] != null?int.Parse(footballParser[4]) : -1; var trainer = footballParser[5] != null ? footballParser[5].Trim() : ""; var bijnaam = footballParser[6] != null ? footballParser[6].Trim() : ""; var player = new Speler(spelernaam, rugnummer, waarde, stamnummer);; if (teams.ContainsKey(stamnummer)) { teams[stamnummer].Spelers.Add(player); } else { var team = new Team(stamnummer, clubNaam, bijnaam, trainer); team.Spelers.Add(player); teams.TryAdd(team.Stamnummer, team); } } foreach (var t in teams.Values) { dbfunctions.VoegTeamToe(t); foreach (var s in t.Spelers) { dbfunctions.VoegSpelerToe(s); } } }
static public DataTable loadCSVToDataTable(string tableName, string path) { DataTable dt = new System.Data.DataTable(tableName); using (GenericParser parser = new GenericParser()) { try { parser.SetDataSource(path); parser.ColumnDelimiter = ','; parser.FirstRowHasHeader = true; parser.MaxBufferSize = 1024; bool isfirst = true; int count = 0; while (parser.Read()) { count++; int cs = parser.ColumnCount; if (isfirst) { for (int i = 0; i < cs; i++) { dt.Columns.Add(parser.GetColumnName(i), typeof(string)); } isfirst = false; } DataRow dr = dt.NewRow(); for (int i = 0; i < cs; i++) { string val = parser[i]; dr[i] = val; } dt.Rows.Add(dr); } } catch (Exception ex) { logger.Error(ex, "Exception:"); } } return(dt); }
public static void GetAssurContrCompSubsidFromCSV(ref List <string> data, string filePath, int importId = 0) { GenericParser parser; try { string assur, contr, comp, subsid; string accs; using (parser = new GenericParser()) { parser.SetDataSource(filePath); parser.ColumnDelimiter = C.cVALSEP.ToCharArray()[0]; parser.FirstRowHasHeader = true; //parser.SkipStartingDataRows = 10; //parser.MaxBufferSize = 4096; //parser.MaxRows = 500; //parser.TextQualifier = '\"'; while (parser.Read()) { assur = parser[C.eDBTempOtherFieldsColumns.AssureurName.ToString()]; contr = parser[C.eDBTempOtherFieldsColumns.ContractId.ToString()]; comp = parser[C.eDBTempOtherFieldsColumns.Company.ToString()]; subsid = parser[C.eDBTempOtherFieldsColumns.Subsid.ToString()]; accs = importId.ToString() + C.cVALSEP + assur + C.cVALSEP + contr + C.cVALSEP + comp + C.cVALSEP + subsid + C.cVALSEP + C.cVALSEP; if (!string.IsNullOrWhiteSpace(accs) && !data.Contains(accs)) { data.Add(accs); } } } } catch (Exception ex) { log.Error(ex.Message); throw ex; } }
static void Run(string inputFile, string outputDir, bool hasHeaders, int?contentColumn, int?fileNameColumn) { if (!File.Exists(inputFile)) { throw new Exception($"Input file {inputFile} does not exist."); } if (!Directory.Exists(Path.GetDirectoryName(outputDir))) { throw new Exception($"The directory for the output {outputDir} does not exist."); } using (GenericParser parser = new GenericParser(inputFile)) { parser.ColumnDelimiter = '\t'; parser.MaxBufferSize = 4096 * 10; var itemIndex = 0; var firstItemIndex = hasHeaders ? 1 : 0; int fileNameColumnIndex = fileNameColumn ?? -1; int contentColumnIndex = contentColumn ?? 0; string fileName; string content; while (parser.Read()) { if (itemIndex >= firstItemIndex) { content = parser[contentColumnIndex]; content = content.Replace("\\r", "\r"); content = content.Replace("\\n", "\n"); content = content.Replace("\\\"", "\""); fileName = fileNameColumnIndex >= 0 ? $"{parser[fileNameColumnIndex]}.txt" : $"{itemIndex}.txt"; string outputFile = Path.Combine(outputDir, fileName); File.WriteAllText(outputFile, content); Console.Write("."); } itemIndex++; } Console.WriteLine(""); } }
/// <summary> /// Uses the <see cref="GenericParser"/> to read the FixedWidth file. /// </summary> private static void _GenericParser11FixedWidth() { string s; using (GenericParser gp = new GenericParser(PerformanceTests.FW_DATA_FILE)) { gp.MaxBufferSize = 4096; gp.ColumnWidths = new int[PerformanceTests.NUMBER_OF_COLUMNS_IN_DATA] { 5, 5, 1, 28, 42, 15, 13, 9, 9, 1, 13, 14, 13, 6 }; gp.TrimResults = true; while (gp.Read()) { for (int i = 0; i < gp.ColumnCount; ++i) { s = gp[i]; } } } }
/// <summary> /// Tests if the document is a valid simpleInput document. /// </summary> /// <param name="documentText">Document input text.</param> /// <returns></returns> private static string MakeSimpleInput(string documentText) { var rowList = new List <string>(); using (GenericParser parser = new GenericParser(new StringReader(documentText))) { // NEVER have buffer issues! parser.MaxBufferSize = int.MaxValue / 4; if (documentText.Contains('\t', StringComparison.InvariantCulture)) { parser.ColumnDelimiter = '\t'; } parser.TrimResults = true; while (parser.Read()) { if (parser.ColumnCount > 3 || parser.ColumnCount < 1) { throw new FormatException($"Row {parser.DataRowNumber} has {parser.ColumnCount} columns. It needs to have between 1 and 3 columns."); } if (!string.IsNullOrWhiteSpace(parser[2]) && !(parser[2].StartsWith("https://", StringComparison.InvariantCultureIgnoreCase) || parser[2].StartsWith("http://", StringComparison.InvariantCultureIgnoreCase))) { throw new FormatException($"Row {parser.DataRowNumber} has a third column that is not formatted like a url."); } var rowZero = parser[0].Replace('"', '\'').Replace('\t', ' ').Replace('\n', ' ').Replace('\r', ' '); var rowOne = parser[1]?.Replace('"', '\'').Replace('\t', ' ').Replace('\n', ' ').Replace('\r', ' ') ?? string.Empty; var rowTwo = parser[2]?.Replace('"', '\'').Replace('\t', ' ').Replace('\n', ' ').Replace('\r', ' ') ?? string.Empty; rowList.Add(string.Join('\t', new List <string> { $"\"{rowZero}\"", $"\"{rowOne}\"", $"\"{rowTwo}\"" })); } } return(string.Join(Environment.NewLine, rowList)); }
private static IEnumerable <Series> ReadSeries(string file, string tsvFolder) { using (var parser = new GenericParser(file)) { parser.ColumnDelimiter = '\t'; parser.FirstRowHasHeader = true; parser.TrimResults = true; parser.MaxBufferSize = parser.MaxBufferSize * 10; while (parser.Read()) { var fileName = parser["TSV files"]; yield return(new Series { File = fileName, SeriesRef = parser["Series"], Title = parser["Title"], Entries = ReadSeriesEntries(Path.Combine(tsvFolder, fileName)).ToArray() }); } } }
public List <string[]> ReadCSVFile(string path) { List <string[]> components = new List <string[]>(); try { ///Set variables to store values in string ObjectName; string ObjectType; using (GenericParser parser = new GenericParser()) { parser.SetDataSource(path); parser.ColumnDelimiter = ','; parser.FirstRowHasHeader = true; parser.MaxBufferSize = 4096; parser.MaxRows = 50000; parser.TextQualifier = '\"'; parser.SkipEmptyRows = true; while (parser.Read()) { ObjectName = parser["Object Name"]; ObjectType = parser["Object Type"]; string[] component = new string[] { ObjectName, ObjectType }; components.Add(component); } return(components); } } catch { AppendTextBox("Error reading CSV file"); SetStatus("ERROR", Brushes.Red); return(components); } }
public List <LicenseDetails> loadCSV(string Filepath, string ApplicationID) { try { List <LicenseDetails> lstLicenseData = new List <LicenseDetails>(); using (GenericParser parser = new GenericParser()) { parser.SetDataSource(Filepath); parser.ColumnDelimiter = ','; parser.FirstRowHasHeader = true; parser.TextQualifier = '\"'; while (parser.Read()) { if (parser["ApplicationID"] == ApplicationID) { LicenseDetails licenseDetails = new LicenseDetails(); licenseDetails.ComputerID = parser["ComputerID"]; licenseDetails.UserID = parser["UserID"]; licenseDetails.ApplicationID = parser["ApplicationID"]; licenseDetails.ComputerType = parser["ComputerType"]; licenseDetails.Comment = parser["Comment"]; lstLicenseData.Add(licenseDetails); } } } return(lstLicenseData); } catch (Exception) { throw new Exception("File does not exist or bad data"); } }
private void BtnLoad_Click(object sender, EventArgs e) { importData = new Model.ImportDataDataTable(); using (GenericParser parser = new GenericParser()) { parser.SetDataSource(txtImportFile.Text); parser.ColumnDelimiter = ';'; parser.FirstRowHasHeader = true; parser.SkipStartingDataRows = 0; parser.MaxBufferSize = 4096; parser.MaxRows = 500; parser.TextQualifier = '\"'; while (parser.Read()) { Model.ImportDataRow row = importData.NewImportDataRow(); row.Value = int.Parse(parser["Value"]); if (!string.IsNullOrEmpty(parser["Label1031"])) { row.Label1031 = parser["Label1031"]; if (!string.IsNullOrEmpty(row.Label1031)) { row.Label1031 = row.Label1031.Replace("\"", string.Empty).Trim(); } } if (!string.IsNullOrEmpty(parser["Label1033"])) { row.Label1033 = parser["Label1033"]; if (!string.IsNullOrEmpty(row.Label1033)) { row.Label1033 = row.Label1033.Replace("\"", string.Empty).Trim(); } } if (!string.IsNullOrEmpty(parser["Label1036"])) { row.Label1036 = parser["Label1036"]; if (!string.IsNullOrEmpty(row.Label1036)) { row.Label1036 = row.Label1036.Replace("\"", string.Empty).Trim(); } } if (!string.IsNullOrEmpty(parser["Label1040"])) { row.Label1040 = parser["Label1040"]; if (!string.IsNullOrEmpty(row.Label1040)) { row.Label1040 = row.Label1040.Replace("\"", string.Empty).Trim(); } } if (!string.IsNullOrEmpty(parser["Label3082"])) { row.Label3082 = parser["Label3082"]; if (!string.IsNullOrEmpty(row.Label3082)) { row.Label3082 = row.Label3082.Replace("\"", string.Empty).Trim(); } } importData.AddImportDataRow(row); } //while } //using grdValues.DataSource = importData; }//BtnLoad_Click
/// <summary> /// Uses the <see cref="GenericParser"/> to read the Csv file. /// </summary> private static void _GenericParser11Csv() { string s; using (GenericParser gp = new GenericParser(PerformanceTests.CSV_DATA_FILE)) { gp.MaxBufferSize = 4096; while (gp.Read()) { for (int i = 0; i < gp.ColumnCount; ++i) s = gp[i]; } } }
private void button3_Click(object sender, EventArgs e) { if (textBox1.Text != string.Empty) { string parseDate(string indate) { DateTime date1 = Convert.ToDateTime(indate); var month = date1.ToString("MMMM"); var year = date1.ToString("yyyy"); var date = date1.ToString("dd"); string desiredMonth = month; string[] MonthNames = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames; int monthIndex = Array.IndexOf(MonthNames, desiredMonth) + 1; int digits = Convert.ToString(monthIndex).Length; if (digits > 1) { return(year + monthIndex + date); } else { return(year + "0" + monthIndex + date); } } using (GenericParser parser = new GenericParser()) { parser.SetDataSource(textBox1.Text); parser.FirstRowHasHeader = true; parser.SkipStartingDataRows = 0; parser.MaxBufferSize = 4096; parser.TextQualifier = '\"'; while (parser.Read()) { IndexItem item = new IndexItem() { Index_Name = parser["Index Name"], Index_Date = parseDate(parser["Index Date"]), Open_Index_Value = parser["Open Index Value"], High_Index_Value = parser["High Index Value"], Low_Index_Value = parser["Low Index Value"], Closing_Index_Value = parser["Closing Index Value"], Volume = parser["Volume"], }; currentdateindex = item.Index_Date; indexitems.Add(item); } converttotxt2(); } } else { MessageBox.Show("Please select Index File"); } }
private void Read() { GenericParser reader = new GenericParser(Config.Path + "/" + Config.Data) { ColumnDelimiter = ';', MaxBufferSize = 40960, FirstRowHasHeader = true }; while (reader.Read()) { int linksStraatId = Convert.ToInt32(reader[6].Trim()); int rechtsStraatId = Convert.ToInt32(reader[7].Trim()); if ((linksStraatId != -9) && (rechtsStraatId != -9)) { int wegsegmentId = Convert.ToInt32(reader[0].Trim()); string geo = reader[1]; int startIndex = geo.IndexOf("(") + 1; int endIndex = geo.IndexOf(")"); string enkelInts = geo.Substring(startIndex, endIndex - startIndex); string[] puntjes = enkelInts.Split(','); List <Punt> punten = new List <Punt>(); foreach (string puntje in puntjes) { string tss = puntje.Replace('.', ','); string[] puntData = tss.Trim(' ').Split(' '); double x = Convert.ToDouble(puntData[0].Trim()); double y = Convert.ToDouble(puntData[1].Trim()); punten.Add(new Punt(x, y)); } int beginKnoopId = Convert.ToInt32(reader[4].Trim()); Knoop beginKnoop = new Knoop(beginKnoopId, punten[0]); int eindKnoopId = Convert.ToInt32(reader[5].Trim()); Knoop eindKnoop = new Knoop(eindKnoopId, punten[punten.Count - 1]); Segment nieuwSegment = new Segment(wegsegmentId, beginKnoop, eindKnoop, punten); if (_straatIdSegmenten.ContainsKey(rechtsStraatId)) { if (!_straatIdSegmenten[rechtsStraatId].Contains(nieuwSegment)) { _straatIdSegmenten[rechtsStraatId].Add(nieuwSegment); } } else { _straatIdSegmenten.Add(rechtsStraatId, new List <Segment> { nieuwSegment }); } if (_straatIdSegmenten.ContainsKey(linksStraatId)) { if (!_straatIdSegmenten[linksStraatId].Contains(nieuwSegment)) { _straatIdSegmenten[linksStraatId].Add(nieuwSegment); } } else { _straatIdSegmenten.Add(linksStraatId, new List <Segment> { nieuwSegment }); } } } }
private void GetProvinciesGemeenten(List <string> provincies, Dictionary <int, Gemeente> gemeenten) { #if DEBUG DebugWriter.WriteLine("-> RegiosProvider::ReadProvinciesGemeenten"); var timer = new Stopwatch(); timer.Start(); #endif using GenericParser provincieGemeentenReader = new GenericParser(Config.Path + "/" + Config.ProvincieInfo) { ColumnDelimiter = ';', FirstRowHasHeader = true, MaxBufferSize = 4096 }; while (provincieGemeentenReader.Read()) { var id = provincieGemeentenReader[1].Trim(); var naam = provincieGemeentenReader[3].Trim(); var taalCode = provincieGemeentenReader[2].Trim(); if (taalCode != _land.TaalCode) { continue; } if (provincies.Contains(provincieGemeentenReader[1].Trim())) { Provincie p; foreach (var regio in _land.Regios.Values) { if (regio.Provincies.ContainsKey(provincieGemeentenReader[3].Trim())) { p = regio.Provincies[provincieGemeentenReader[3].Trim()]; } else { p = new Provincie { Id = int.Parse(id), Naam = naam, TaalCode = taalCode, Regio = regio }; regio.Provincies.Add(p.Naam, p); } var key = int.Parse(provincieGemeentenReader[0].Trim()); if (gemeenten.ContainsKey(key)) { var gemeente = gemeenten[key]; if (gemeente.Provincie == null) { gemeente.Provincie = p; } else { throw new Exception("Gemeente behoort reeds tot andere provincie"); } if (!p.Gemeenten.ContainsKey(gemeente.Naam)) { p.Gemeenten.Add(gemeente.Naam, gemeente); } } } } } #if DEBUG timer.Stop(); TimeSpan timeTaken = timer.Elapsed; DebugWriter.WriteLine("<- RegiosProvider::ReadProvinciesGemeenten: " + timeTaken.ToString(@"m\:ss\.fff")); #endif }
private void Read() { GenericParser reader = new GenericParser(Config.Path + "/" + Config.Straatnamen) { ColumnDelimiter = ';', MaxBufferSize = 49600, FirstRowHasHeader = true }; while (reader.Read()) { int id = Convert.ToInt32(reader[0].Trim()); string straatNaam = reader[1].Trim(); if (_idStraatnaam.ContainsKey(id)) { continue; } _idStraatnaam.Add(id, straatNaam); } reader = new GenericParser(Config.Path + "/" + Config.StraatIdGemeenteId) { ColumnDelimiter = ';', MaxBufferSize = 49600, FirstRowHasHeader = true }; int graafIdCounter = 0; while (reader.Read()) { int gemeenteId = Convert.ToInt32(reader[1].Trim()); int straatNaamId = Convert.ToInt32(reader[0].Trim()); Graaf graaf = new Graaf(); graaf.Id = ++graafIdCounter; var map = _wrDataProvider.GetSegmentListByStraatId(straatNaamId); if (map == null) { continue; } graaf.Map = map; string straatnaam = _idStraatnaam.ContainsKey(straatNaamId) ? _idStraatnaam[straatNaamId] : null; if (straatnaam != null) { Straat s = new Straat(straatNaamId, straatnaam, gemeenteId, graaf); if (_gemIdStraten.ContainsKey(gemeenteId)) { _gemIdStraten[gemeenteId].Add(s); } else { _gemIdStraten.Add(gemeenteId, new List <Straat> { s }); } } } }
public void Convert(FileInfo file, string destinationFile) { var items = new List <string>(); using (var parser = new GenericParser(file.FullName) { FirstRowHasHeader = true, SkipEmptyRows = true, TrimResults = true, MaxBufferSize = 1024 * 32 }) { while (parser.Read()) { var tracesEntity = EntityMapper.Map(parser); var expando = new ExpandoObject(); var d = (IDictionary <string, object>)expando; foreach (var p in tracesEntity.ToExpandoObject()) { switch (p.Key) { case nameof(TracesEntry.Timestamp): d["@t"] = p.Value; continue; case nameof(TracesEntry.Message): d["@m"] = p.Value; continue; case nameof(TracesEntry.OperationId): d["@i"] = p.Value ?? Guid.NewGuid(); continue; case nameof(TracesEntry.SeverityLevel): { if (p.Value != null) { d["@l"] = MapLevel(p.Value.ToString()).ToString(); } continue; } case nameof(TracesEntry.CustomDimensions): continue; } if (string.IsNullOrWhiteSpace(p.Value?.ToString())) { continue; } d[p.Key] = p.Value; } var customProperties = new HashSet <string>(StringComparer.InvariantCultureIgnoreCase); if (tracesEntity?.CustomDimensions != null) { foreach (var kvp in tracesEntity.CustomDimensions) { string customKey = kvp.Key; if (customKey == "MessageTemplate") { customKey = "@mt"; } if (customKey == "Exception") { customKey = "@x"; } if (customKey == "Renderings") { customKey = "@r"; } if (!customProperties.Contains(kvp.Key)) { // need to track unknown custom property key if (d.Keys.Contains(kvp.Key)) { // need to avoid conflict var customDimensionKeyConflictOption = $"aiSeq:{kvp.Key}"; if (!d.Keys.Contains(customDimensionKeyConflictOption)) { d.Keys.Add(customDimensionKeyConflictOption); customKey = customDimensionKeyConflictOption; } } else { customProperties.Add(kvp.Key); customKey = kvp.Key; } } d[customKey] = kvp.Value; } } var serialized = JsonConvert.SerializeObject(d, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); items.Add(serialized); } } if (destinationFile == null) { destinationFile = file.FullName + ".clef"; } using (var sw = File.CreateText(destinationFile)) { foreach (var i in items) { sw.WriteLine(i); } sw.Flush(); } }
/// <summary> /// Parses a CSV file and loads the observables to the temporary Observables list /// </summary> /// <param name="filepath">The path to the CSV</param> private void ParseCSV(string filepath) { //If the path is empty, invalid or the file doesn't exist, stop if (string.IsNullOrWhiteSpace(filepath) || !File.Exists(filepath)) { return; } //Master try to catch any other uncaught issues try { //GenericParser written by AndrewRissing - https://github.com/AndrewRissing/GenericParsing using (GenericParser parser = new GenericParser()) { //Set the params of the parser parser.SetDataSource(filepath); parser.ColumnDelimiter = ','; parser.FirstRowHasHeader = true; parser.TextQualifier = '\"'; //Start looping through the file line by line int line = 0; while (parser.Read()) { line++; try //Try per line { //Add the parsed columns to our dictionary of name/value pairs //Column order is expected to be as per strict template order //TODO: Make more flexible and allow different column orders Dictionary <string, string> values = new Dictionary <string, string>(); for (int i = 0; i < parser.ColumnCount; i++) { values.Add(parser.GetColumnName(i), parser[i]); } //Create a new observable object from the values read in this line ObservableObject newobs = null; string errors = GenerateObservable(values, out newobs); //If generating the observable gave any errors, add them to the error list if (errors != null) { AddError(errors); } else { //Otherwise add this new observable to our storage list Observables.Add(newobs); } } catch (Exception exep) { //This will likely occur if columns are missing or have been renamed etc in the template MessageBox.Show("There was an error parsing the CSV at line " + line + ".\r\n\r\n Please make sure it is in correct format - check the template provided."); return; } } } } catch (Exception exep) { //Should only occur due to unforseen IO errors like the file being in use etc MessageBox.Show("There was an error reading the CSV:\r\n" + exep.Message); return; } //Try to add the observables to the GUI try { //Clear the box for now and set the displaymember to the right property ObservablesListBox.Items.Clear(); ObservablesListBox.DisplayMember = "DisplayTitle"; //Add each observable to the listbox foreach (ObservableObject obs in Observables) { ObservablesListBox.Items.Add(obs); } //Refresh the listbox ObservablesListBox.Refresh(); } catch (Exception exep) { //Throw an exception here if there is an error updating the GUI, shouldn't happen under normal conditions MessageBox.Show("There was an error reading the CSV:\r\n" + exep.Message); return; } }
private void Opcion2() { int Fila = 0; ListaXml = new List <cp_XML_Documento_Info>(); try { using (GenericParser parser = new GenericParser()) { parser.SetDataSource(txtRutaXml.Text); parser.ColumnDelimiter = '\t'; parser.FirstRowHasHeader = false; parser.SkipStartingDataRows = 2; parser.MaxBufferSize = 4096; parser.MaxRows = 999999999; parser.TextQualifier = '\"'; cp_XML_Documento_Info info = new cp_XML_Documento_Info(); while (parser.Read()) { if (Fila == 104) { } if ((Fila % 2) == 0) { info = new cp_XML_Documento_Info { Tipo = parser[0].ToString().ToUpper(), CodDocumento = (parser[0].ToString().ToUpper() == "FACTURA" ? "01" : parser[0].ToString().ToUpper()), Comprobante = (parser[0].ToString().ToUpper() == "FACTURA" ? "01-" : (parser[0].ToString().ToUpper() + "-")) + parser[1].ToString(), emi_Ruc = parser[2].ToString(), emi_RazonSocial = parser[3].ToString(), FechaEmision = Convert.ToDateTime(parser[4].ToString()), ClaveAcceso = parser[8].ToString() }; var Doc = busXml.GetInfo(param.IdEmpresa, info.CodDocumento, info.Comprobante, info.emi_Ruc); if (Doc != null) { info.ValidacionD = Doc.ValidacionD; info.ValidacionR = Doc.ValidacionR; info.IdDocumento = Doc.IdDocumento; info.ValidacionC = Doc.ValidacionC; } Fila++; } else { info.Total = parser[0] == null ? 0 : Convert.ToDouble(parser[0]); ListaXml.Add(info); Fila++; } } } gcDetalle.DataSource = ListaXml; lblContador.Text = ListaXml.Count.ToString(); } catch (Exception ex) { gcDetalle.DataSource = ListaXml; lblContador.Text = ListaXml.Count.ToString(); Core.Erp.Info.Log_Exception.LoggingManager.Logger.Log(Core.Erp.Info.Log_Exception.LoggingCategory.Error, ex.Message); MessageBox.Show("Ha ocurrido un error en la fila: " + Fila.ToString(), param.Nombre_sistema, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void Read() { GenericParser parser = new GenericParser(Config.Path + "/" + Config.Gemeentenamen) { ColumnDelimiter = ';', MaxBufferSize = 4096, FirstRowHasHeader = true }; while (parser.Read()) { int gemeenteNaamId = Convert.ToInt32(parser[0].Trim()); int gemeenteId = Convert.ToInt32(parser[1].Trim()); string taalCode = parser[2].Trim(); string gemeenteNaam = parser[3].Trim(); if (taalCode != _taalCode) { continue; } var nieuweGem = new Gemeente() { Id = gemeenteId, NaamId = gemeenteNaamId, TaalCode = taalCode, Naam = gemeenteNaam }; _gemeentes.Add(gemeenteId, nieuweGem); } parser = new GenericParser(Config.Path + "/" + Config.ProvincieInfo) { ColumnDelimiter = ';', MaxBufferSize = 4096, FirstRowHasHeader = true }; while (parser.Read()) { int gemeenteId = Convert.ToInt32(parser[0].Trim()); int provincieId = Convert.ToInt32(parser[1].Trim()); string taalCode = parser[2].Trim(); string provincieNaam = parser[3].Trim(); if (taalCode != _taalCode) { continue; } if (_gemeentes.ContainsKey(gemeenteId)) { _gemeentes[gemeenteId].ProvincieId = provincieId; if (!_proIdGemeenten.ContainsKey(provincieId)) { _proIdGemeenten.Add(provincieId, new List <Gemeente> { GetGemeenteById(gemeenteId) }); } else { _proIdGemeenten[provincieId].Add(GetGemeenteById(gemeenteId)); } } if (!_provincies.ContainsKey(provincieId)) { _provincies.Add(provincieId, new Provincie { Id = provincieId, Naam = provincieNaam, TaalCode = _taalCode }); } } }
private void btn2_Click(object sender, EventArgs e) { //ArgumentParser argParser = new ArgumentParser(args); String dataSource = ofd1.FileName; //argParser["o"]; if (dataSource != null) { try { GenericParser parser = new GenericParser(); parser.SetDataSource(dataSource); parser.FirstRowHasHeader = true; parser.Read(); int columnCount = parser.ColumnCount; //Work out the names of the Accounts we need to sum List <string> ItemNames = new List <string>(); for (int i = 0; i < columnCount; i++) { ItemNames.Add(parser.GetColumnName(i)); } // //Work out the numbers we need to sum // List<Decimal> DecimalsToSum = new List<decimal>(); // for (int decimalCounter = 0; decimalCounter < columnCount; decimalCounter++) // { // DecimalsToSum.Add(Decimal.Parse(parser[decimalCounter])); // } // // List<Tuple<Decimal,List<String>>> SumsAndOps = new List<Tuple<Decimal,List<String>>>(); // List<Tuple<Decimal,Decimal,List<String>>> AbsSumsAndOps = new List<Tuple<Decimal,Decimal,List<String>>>(); // List<String> OpsSoFar = new List<string>(); // // callsCount = 0; // BuildPathsAndSumsTogether (SumsAndOps, AbsSumsAndOps, DecimalsToSum, OpsSoFar, 0, 0, columnCount); // SumsAndOps.Sort( (a,b) => a.Item1.CompareTo(b.Item1) ); // AbsSumsAndOps.Sort( (a,b) => a.Item1.CompareTo(b.Item1) ); //Work out the numbers we need to sum List <Decimal> DecimalsToSum = new List <decimal>(); for (int decimalCounter = 0; decimalCounter < columnCount; decimalCounter++) { DecimalsToSum.Add(Decimal.Parse(parser[decimalCounter])); } //List<Tuple<Decimal,List<String>>> SumsAndOps = new List<Tuple<Decimal,List<String>>>(); List <String> OpsSoFar = new List <string>(); callsCount = 0; maxAbsDiff = -9999; BuildPathsAndSumsTogetherMinimal(DecimalsToSum, OpsSoFar, 0, 0, columnCount); String stuff = "yarr"; Decimal outcome = maxAbsDiff; //BuildPathsAndSumsTogetherNoAbs (SumsAndOps, DecimalsToSum, OpsSoFar, 0, 0, columnCount);(SumsAndOps, DecimalsToSum, OpsSoFar, 0, 0, columnCount); //SumsAndOps.Sort( (a,b) => a.Item1.CompareTo(b.Item1) ); // int countSuccess = 0; // textBox1.Text = ""; // foreach(Tuple<Decimal,List<string>> sumWithOps in SumsAndOps) { // if(sumWithOps.Item1 == 0) { // countSuccess++; // // for(int account = 0; account < ItemNames.Count; account++) { // textBox1.Text += (ItemNames[account].PadLeft(30) + ":" // + DecimalsToSum[account].ToString().PadLeft(15).PadRight(4) // + "(" + sumWithOps.Item2[account] + ")" // + Environment.NewLine); // Console.WriteLine(ItemNames[account]+":"+DecimalsToSum[account]+":"+sumWithOps.Item2[account]); // } // textBox1.Text += ("Balances To".PadLeft(30) + ":" // + sumWithOps.Item1.ToString().PadLeft(15) // + Environment.NewLine + Environment.NewLine); // Console.WriteLine("Balances To" +":"+ sumWithOps.Item1); // Console.WriteLine(); // } // } // // if(countSuccess == 0) { // int n = 20; // //// textBox1.Text = "This combination of numbers has no possible way to balance. The " + n + " solutions closest to 0 follow." + Environment.NewLine; //// //// for(int closeResultPos = 0 ; closeResultPos < n || closeResultPos >= AbsSumsAndOps.Count; closeResultPos++) { //// Tuple<Decimal,Decimal,List<string>> sumWithOps = AbsSumsAndOps[closeResultPos]; //// for(int account = 0; account < ItemNames.Count; account++) { //// textBox1.Text += (ItemNames[account].PadLeft(30) + ":" //// + DecimalsToSum[account].ToString().PadLeft(15).PadRight(4) //// + "(" + sumWithOps.Item3[account] + ")" //// + Environment.NewLine); //// Console.WriteLine(ItemNames[account]+":"+DecimalsToSum[account]+":"+sumWithOps.Item3[account]); //// } //// //// textBox1.Text += ("Balances To".PadLeft(30) + ":" //// + sumWithOps.Item2.ToString().PadLeft(15) //// + Environment.NewLine + Environment.NewLine); //// Console.WriteLine("Balances To" +":"+ sumWithOps.Item2); //// Console.WriteLine(); //// } // } } catch (Exception ex) { textBox1.Text = ex.Message; } } }
private void AbrirArquivoDelimitado() { if (comboBoxColumnDelimiter.SelectedItem.ToString() == "Outro") { if (string.IsNullOrEmpty(textBoxOtherDelimiter.Text)) { MessageBox.Show("Informe o delimitador desejado"); textBoxOtherDelimiter.Focus(); return; } } if (!string.IsNullOrEmpty(labelFileName.Text) && labelFileName.Text != "Nenhum arquivo selecionado") { dataGridView1.DataSource = null; dataGridView1.Refresh(); dataSet1.Tables.Clear(); using (GenericParser parser = new GenericParser()) { List <string> listHeader = new List <string>(); List <List <string> > listRows = new List <List <string> >(); parser.SetDataSource(labelFileName.Text); parser.TextFieldType = FieldType.Delimited; parser.ColumnDelimiter = GetColumnDelimiterChar(); parser.FirstRowHasHeader = checkBoxPrimeiraLinha.Checked; //parser.SkipStartingDataRows = 10; //parser.MaxBufferSize = 4096; //parser.MaxRows = 500; parser.TextQualifier = GetTextDelimiterChar(); while (parser.Read()) { List <string> listColsInRows = new List <string>(); if (listHeader.Count() == 0) { for (int i = 0; i < parser.ColumnCount; i++) { if (string.IsNullOrEmpty(parser.GetColumnName(i))) { listHeader.Add("Coluna " + (i + 1).ToString()); } else { listHeader.Add(parser.GetColumnName(i)); } } } else { if (listHeader.Count() < parser.ColumnCount) { for (int i = listHeader.Count(); i < parser.ColumnCount; i++) { if (string.IsNullOrEmpty(parser.GetColumnName(i))) { listHeader.Add("Coluna " + (i + 1).ToString()); } else { listHeader.Add(parser.GetColumnName(i)); } } } } if (listHeader.Count() > 0) { for (int i = 0; i < parser.ColumnCount; i++) { listColsInRows.Add(parser[i]); } } if (listColsInRows.Count() > 0) { listRows.Add(listColsInRows); } } DataTable dataTable = new DataTable(); foreach (string col in listHeader) { dataTable.Columns.Add(col); } foreach (List <string> row in listRows) { object[] values = new object[row.Count]; foreach (string col in row) { values[row.IndexOf(col)] = col; } dataTable.Rows.Add(values); } dataSet1.Tables.Add(dataTable); VerifyGridNumRowAndSetLabelReg(dataSet1.Tables[0].Rows.Count); dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader); dataGridView1.AutoGenerateColumns = true; dataGridView1.DataSource = dataSet1.Tables[0]; dataGridView1.Focus(); } } else { if (!string.IsNullOrEmpty(labelFileName.Text) && labelFileName.Text != "Nenhum arquivo selecionado") { { MessageBox.Show("Selecione o arquivo texto"); } } } }
private DataTable LerArquivoCSV(string caminhoParaLer) { const string formato_data = @"yyyyMMdd"; StringBuilder numeroContaConta = null; StringBuilder dataMovimentacao = null; StringBuilder numeroDocumento = null; StringBuilder historico = null; StringBuilder valorMovimentacao = null; StringBuilder tipoMovimentacao = null; DataTable tabelaRegistros = null; DataRow umRegistro = null; CultureInfo provider = null; DateTime dataSerieData; double valorSerieDouble = 0; numeroContaConta = new StringBuilder(); dataMovimentacao = new StringBuilder(); numeroDocumento = new StringBuilder(); historico = new StringBuilder(); valorMovimentacao = new StringBuilder(); tipoMovimentacao = new StringBuilder(); tabelaRegistros = CriarTabelaInputDoland(); provider = CultureInfo.InvariantCulture; using (var parser = new GenericParser()) { parser.SetDataSource(caminhoParaLer); parser.ColumnDelimiter = ';'; parser.FirstRowHasHeader = true; parser.MaxBufferSize = 4096; parser.MaxRows = 500000; valorSerieDouble = 0; while (parser.Read()) { numeroContaConta.Clear(); dataMovimentacao.Clear(); numeroDocumento.Clear(); historico.Clear(); valorMovimentacao.Clear(); tipoMovimentacao.Clear(); numeroContaConta.Append(parser[0]); dataMovimentacao.Append(parser[1]); numeroDocumento.Append(parser[2]); historico.Append(parser[3]); valorMovimentacao.Append(parser[4]); tipoMovimentacao.Append(parser[5]); umRegistro = tabelaRegistros.NewRow(); umRegistro[0] = numeroContaConta.ToString(); DateTime.TryParseExact(dataMovimentacao.ToString(), formato_data, provider, DateTimeStyles.None, out dataSerieData); umRegistro[1] = dataSerieData; umRegistro[2] = numeroDocumento.ToString(); umRegistro[3] = historico.ToString(); double.TryParse(valorMovimentacao.ToString(), out valorSerieDouble); umRegistro[4] = valorSerieDouble; umRegistro[5] = tipoMovimentacao.ToString(); tabelaRegistros.Rows.Add(umRegistro); } } //Utils.DataTableUtils.PrintDataTable(tabelaRegistros); return(tabelaRegistros); }
/// <summary> /// Uses the <see cref="GenericParser"/> to read the FixedWidth file. /// </summary> private static void _GenericParser11FixedWidth() { string s; using (GenericParser gp = new GenericParser(PerformanceTests.FW_DATA_FILE)) { gp.MaxBufferSize = 4096; gp.ColumnWidths = new int[PerformanceTests.NUMBER_OF_COLUMNS_IN_DATA] { 5, 5, 1, 28, 42, 15, 13, 9, 9, 1, 13, 14, 13, 6 }; gp.TrimResults = true; while (gp.Read()) { for (int i = 0; i < gp.ColumnCount; ++i) s = gp[i]; } } }