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 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); }
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); }
/// <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 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"); } } } }
public void Load(string ChangesFilePath) { using (GenericParser parser = new GenericParser()) { parser.SetDataSource(ChangesFilePath); parser.ColumnDelimiter = ','; parser.FirstRowHasHeader = true; parser.TextQualifier = '\"'; //parser.SkipStartingDataRows = 10; //parser.MaxBufferSize = 4096; //parser.MaxRows = 500; int ColumnCount = 0; string[] ReservedColumnNames = new string[] { "table", "elementType", "elementName" }; while (parser.Read()) { // get the column count on the first pass if (ColumnCount == 0) { ColumnCount = parser.ColumnCount; } string Table = parser["table"]; string ElementType = parser["elementType"]; string ElementName = parser["elementName"]; // determine the type of change JsonChange change = null; if (ElementType.Equals("column", StringComparison.CurrentCultureIgnoreCase)) { change = new JsonChangeColumn() { Table = Table, Column = ElementName }; } if (ElementType.Equals("measure", StringComparison.CurrentCultureIgnoreCase)) { change = new JsonChangeMeasure() { Table = Table, Measure = ElementName }; } ; // load the additional columns for (int ColumnIndex = 0; ColumnIndex < ColumnCount; ColumnIndex++) { string ColumnName = parser.GetColumnName(ColumnIndex); // ignore reserved columns names if (ReservedColumnNames.Contains <string>(ColumnName, StringComparer.CurrentCultureIgnoreCase)) { continue; } // add the attribute string Value = parser[ColumnIndex]; if (!String.IsNullOrEmpty(Value)) { change.Attributes.Add(new JsonAttribute(ColumnName, parser[ColumnIndex])); } } this.Add(change); } } }