Beispiel #1
0
        private void btnConvert_Click(object sender, EventArgs e)
        {
            if (_salary == null)
            {
                return;
            }
            _salary.Header = null;
            _salary.Rows.Clear();

            using (Stream stream = GenerateStreamFromString(_salary.RawText))
            {
                TextFieldParser parser = new TextFieldParser(stream);
                parser.HasFieldsEnclosedInQuotes = true;
                parser.SetDelimiters(",");

                string[] fields;
                int counter = 0;
                while (!parser.EndOfData)
                {
                    fields = parser.ReadFields();
                    if (fields == null || fields.Length == 0)
                        continue;
                    if (counter == 0)
                    {
                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < fields.Length; i++)
                        {
                            string field = fields[i].Trim().Replace(" ", "").Replace(" ", "").Replace("\n", "").Replace("\r", "");
                            field = field.Replace("\"", "\"\"");
                            if (i != fields.Length - 1)
                            {
                                sb.AppendFormat("\"{0}\",", field);
                            }
                            else
                            {
                                sb.AppendFormat("\"{0}\"", field);
                            }
                        }
                        _salary.Header = sb.ToString();
                        counter++;
                    }
                    else
                    {
                        string name = fields[0].Trim().Replace(" ", "").Replace(" ", "").Replace("\n", "");
                        if (name.StartsWith("\"") && name.EndsWith("\""))
                        {
                        }
                        else
                        {
                            name = "\"" + name.Replace("\"", "\"\"") + "\"";
                        }
                        fields[0] = name;
                        _salary.Rows.Add(string.Join(",", fields));
                    }
                }
            }

            Render();
            btnSave.Enabled = true;
        }
Beispiel #2
0
        public void Import(string translationCSVPath)
        {
            patchDict = new Dictionary<int, string>();

            using (TextFieldParser parser = new TextFieldParser(translationCSVPath))
            {
                parser.TextFieldType = FieldType.Delimited;
                parser.SetDelimiters(",");
                parser.ReadFields(); // Skip header

                int i = 0;

                while (!parser.EndOfData)
                {
                    string[] fields = parser.ReadFields();
                    string translatedString = fields[1];
                    if (!string.IsNullOrEmpty(translatedString))
                    {
                        patchDict.Add(i, fields[1]);
                    }

                    i++;
                }
            }
        }
Beispiel #3
0
        // ファイル入力
        // readFileName:入力先のファイル名
        // return:行ごとの情報を保持するリスト
        public static List<String[]> read(String readFileName)
        {
            // 読み込んできた行を保持するインスタンス
            List<String[]> readLines = new List<String[]>();
            try
            {
                // テキストを読み込むためのインスタンス
                TextFieldParser parser = new TextFieldParser(readFileName);
                // 分割される状態をセット
                parser.TextFieldType = FieldType.Delimited;
                // 区切り文字の設定
                parser.SetDelimiters(",");

                // ファイルの終端まで処理
                while (!parser.EndOfData)
                {
                    String[] row = parser.ReadFields();	// 1行読み込み
                    readLines.Add(row);	// 行を保持
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("" + e, "error",
                   MessageBoxButtons.OK,
                   MessageBoxIcon.Error);

            }

            return readLines;
        }
Beispiel #4
0
        // TextFieldParser Method
        private static List<string> TextFieldParserMethod(string SourceFilePath)
        {
            List<string> ResultList = new List<string>();

            // Parser

            using (TextFieldParser parser = new TextFieldParser(SourceFilePath))
            {
                parser.TextFieldType = FieldType.Delimited;
                parser.SetDelimiters(",");

                // Find Index
                string[] header = parser.ReadFields();
                int physicalDeliveryOfficeNameIndex = Array.IndexOf(header, "physicalDeliveryOfficeName");
                int nameIndex = Array.IndexOf(header, "name");
                int titleIndex = Array.IndexOf(header, "title");

                string[] CurrentRow;

                while (!parser.EndOfData)
                {
                    //Processing row
                    string[] fields = parser.ReadFields();
                    CurrentRow = new string[3] { fields[physicalDeliveryOfficeNameIndex], fields[nameIndex], fields[titleIndex] };
                    if (!string.IsNullOrEmpty(CurrentRow[0]))
                    ResultList.Add(string.Join(",", CurrentRow));
                }
            }
            return ResultList;
        }
        public DataTable GetDataTableColumns(string csv_file_path)
        {
            DataTable csvData = new DataTable();
            try
            {
                using (TextFieldParser csvReader = new TextFieldParser(csv_file_path))
                {
                    csvReader.SetDelimiters(new string[] { "," });
                    csvReader.HasFieldsEnclosedInQuotes = true;
                    string[] colFields = csvReader.ReadFields();

                    for (int i = 0; i < colFields.Length; i++)
                    {
                        colFields[i] = this.RemoveSpecialCharacters(colFields[i]);
                    }

                    foreach (string column in colFields)
                    {
                        DataColumn datecolumn = new DataColumn(column);
                        datecolumn.AllowDBNull = true;
                        csvData.Columns.Add(datecolumn);
                    }
                }
            }
            catch (Exception ex)
            {
                //TO DO
            }
            return csvData;
        }
Beispiel #6
0
 /// <summary>
 /// inladen van het CSV bestand met verschillende diersoorten
 /// </summary>
 /// <returns>lijst van ingeladen dieren</returns>
 public List<Diersoort> Load()
 {
     List<Diersoort> loadedAnimals = new List<Diersoort>();
     //Get all the animal names from the local storage
     using (TextFieldParser parser = new TextFieldParser(FilePath + "broedvogels.csv"))
     {
         parser.TextFieldType = FieldType.Delimited;
         parser.SetDelimiters(",");
         while (!parser.EndOfData)
         {
             //Processing row
             string[] fields = parser.ReadFields();
             foreach (string field in fields)
             {
                 string[] columns = field.Split(';');
                 string[] startDatetime = columns[2].Split('-');
                 string[] endDatetime = columns[3].Split('-');
                 DateTime sdt = new DateTime(2000, Convert.ToInt32(startDatetime[1]), Convert.ToInt32(startDatetime[0]));
                 DateTime edt = new DateTime(2000, Convert.ToInt32(endDatetime[1]), Convert.ToInt32(endDatetime[0]));
                 Diersoort toAdd = new Vogel(
                     columns[0],
                     columns[1],
                     sdt,
                     edt,
                     Convert.ToInt32(columns[4]));
                 loadedAnimals.Add(toAdd);
             }
         }
     }
     return loadedAnimals;
 }
 /// <summary>
 /// http://stackoverflow.com/questions/16606753/populating-a-dataset-from-a-csv-file
 /// </summary>
 /// <param name="filePath"></param>
 /// <returns></returns>
 public static DataTable GetDataTabletFromCSVFile(string filePath)
 {
     DataTable csvData = new DataTable();
     try {
         using (TextFieldParser csvReader = new TextFieldParser(filePath))
         {
             csvReader.SetDelimiters(new string[] { "," });
             csvReader.HasFieldsEnclosedInQuotes = true;
             string[] colFields = csvReader.ReadFields();
             foreach (string column in colFields)
             {
                 DataColumn datecolumn = new DataColumn(column);
                 datecolumn.AllowDBNull = true;
                 csvData.Columns.Add(datecolumn);
             }
             while (!csvReader.EndOfData)
             {
                 string[] fieldData = csvReader.ReadFields();
                 //Making empty value as null
                 for (int i = 0; i < fieldData.Length; i++)
                 {
                     if (fieldData[i] == "")
                     {
                         fieldData[i] = null;
                     }
                 }
                 csvData.Rows.Add(fieldData);
             }
         }
     }
     catch (Exception ex)
     {
     }
     return csvData;
 }
        public void ReadFile()
        {
            if (FilePath == null) return;

            var parser = new TextFieldParser(FilePath)
            {
                TextFieldType = FieldType.Delimited,
                CommentTokens = new[] {"#"}
            };

            parser.SetDelimiters(",");
            parser.HasFieldsEnclosedInQuotes = false;

            parser.ReadLine();

            while (!parser.EndOfData)
            {
                var row = parser.ReadFields();
                if (row == null) continue;
                var newLine = new List<string>(row.Length);
                newLine.AddRange(row);

                _data.Add(newLine);
            }
        }
        public static IMLDataSet LoadCSVToDataSet(FileInfo fileInfo, int inputCount, int outputCount, bool randomize = true, bool headers = true)
        {
            BasicMLDataSet result = new BasicMLDataSet();
            CultureInfo CSVformat = new CultureInfo("en");

            using (TextFieldParser parser = new TextFieldParser(fileInfo.FullName))
            {
                parser.TextFieldType = FieldType.Delimited;
                parser.SetDelimiters(",");
                if (headers)
                    parser.ReadFields();
                while (!parser.EndOfData)
                {
                    //Processing row
                    string[] fields = parser.ReadFields();
                    var input = new BasicMLData(inputCount);
                    for (int i = 0; i < inputCount; i++)
                        input[i] = double.Parse(fields[i], CSVformat);
                    var ideal = new BasicMLData(outputCount);
                    for (int i = 0; i < outputCount; i++)
                        ideal[i] = double.Parse(fields[i + inputCount], CSVformat);
                    result.Add(input, ideal);
                }
            }
            var rand = new Random(DateTime.Now.Millisecond);

            return (randomize ? new BasicMLDataSet(result.OrderBy(r => rand.Next()).ToList()) : new BasicMLDataSet(result));
        }
Beispiel #10
0
        public Stock GetStock(StockName stockName, DateTime startDate, DateTime endDate)
        {
            string dir = String.Format(@"..\..\StockData\Maya");
            string filename = String.Format("{0}.csv", stockName);
            var fullPath = Path.Combine(dir, filename);

            var rates = new List<IStockEntry>();

            var parser = new TextFieldParser(fullPath) {TextFieldType = FieldType.Delimited};
            parser.SetDelimiters(",");

            //skips the first 3 lines
            parser.ReadFields();
            parser.ReadFields();
            parser.ReadFields();

            while (!parser.EndOfData)
            {
                var fields = parser.ReadFields();
                if (fields != null)
                {
                    StockEntry stockEntry = null;
                    rates.Add(stockEntry);
                }
            }

            rates.Reverse();

            var stock = new Stock(stockName, rates);

            return stock;
        }
        private DataTable DataTableFromCSV(string path)
        {
            var data = new DataTable();
            using (var parser = new TextFieldParser(path))
            {
                parser.SetDelimiters(new[] { "," });
                parser.HasFieldsEnclosedInQuotes = true;

                try
                {
                    var columns = parser.ReadFields();
                    foreach (string column in columns)
                    {
                        var dataColumn = new DataColumn(column);
                        dataColumn.AllowDBNull = true;
                        data.Columns.Add(dataColumn);
                    }

                    while (!parser.EndOfData)
                    {
                        var row = parser.ReadFields();
                        data.Rows.Add(row);
                    }
                }
                catch (MalformedLineException)
                {
                    throw;
                }
            }
            return data;
        }
 private void Continue_Click(object sender, RoutedEventArgs e)
 {
     continueButton.IsEnabled = false;
     bool flag = false;
     string[] lastDTS = {""};
     dupeListBox.Items.Clear();
     string[] dtsList = File.ReadAllLines(DTSListSuite.App.mDtsListFile);
     foreach (string line in dtsList)
     {
         if (line.Substring(0, 3) != "C/R")
         {
             TextFieldParser parser = new TextFieldParser(new StringReader(line));
             parser.HasFieldsEnclosedInQuotes = true;
             parser.SetDelimiters(",");
             string[] ArLn = { "" };
             while (!parser.EndOfData)
                 ArLn = parser.ReadFields();
             if (lastDTS.Length > 1)
             {
                 if (ArLn[1] == lastDTS[1] && ArLn[2] == lastDTS[2] && ArLn[3] == lastDTS[3])
                 {
                     dupeListBox.Items.Insert(0, ArLn[1] + " " + ArLn[2] + " " + ArLn[3] + "\tDuplicate!");
                     flag = true;
                 }
             }
             lastDTS = ArLn;
         }
     }
     if (!flag)
         dupeListBox.Items.Insert(0, "No Duplicates!");
     continueButton.IsEnabled = true;
 }
        public IEnumerable<Brand> Read()
        {
using (TextFieldParser parser = new TextFieldParser(path))
{
    parser.CommentTokens = new string[] { "#" };
    parser.SetDelimiters(new string[] { ";" });
    parser.HasFieldsEnclosedInQuotes = true;

    // Skip over header line.
    parser.ReadLine();

    while (!parser.EndOfData)
    { 
        string[] fields = parser.ReadFields();
        yield return new Brand() 
        {
            Name = fields[0],
            FactoryLocation = fields[1],
            EstablishedYear = int.Parse(fields[2]),
            Profit = double.Parse(fields[3], swedishCulture)
        };
    }
}
            
        }
        public void ReadLine_SampleWithNewlineInQuotedField()
        {
            const string input = @"Name,Birth Date
""Creed, Apollo"",1942-08-17
""Ivan 
Drago"",1961-11-03
""Robert """"Rocky"""" Balboa"",1945-07-06";

            var parserReader = new StringReader(input);
            var parser       = new NotVisualBasic.FileIO.CsvTextFieldParser(parserReader);
            var vbParser     = new Microsoft.VisualBasic.FileIO.TextFieldParser(new StringReader(input));

            vbParser.SetDelimiters(",");

            Assert.Equal(vbParser.ReadFields(), parser.ReadFields());
            Assert.Equal(vbParser.ReadFields(), parser.ReadFields());
            Assert.Equal(vbParser.ReadLine(), parserReader.ReadLine());

            // The readline should have read into the middle of the field, which changes the parsing output
            Assert.Equal(new[] { @"Drago""", "1961-11-03" }, vbParser.ReadFields());
            Assert.Equal(new[] { @"Drago""", "1961-11-03" }, parser.ReadFields());

            Assert.Equal(vbParser.ReadFields(), parser.ReadFields());
            Assert.Null(vbParser.ReadFields());
            Assert.Null(parser.ReadFields());
            Assert.True(vbParser.EndOfData);
            Assert.True(parser.EndOfData);
        }
Beispiel #15
0
        static void Main()
        {
            List<MoonPhase> moonPhases = new List<MoonPhase>();

            using (TextFieldParser parser = new TextFieldParser(@"SQMData.csv"))
            {
                parser.TextFieldType = FieldType.Delimited;
                // Значения во входном файле разделены запятыми
                parser.SetDelimiters(",");
                // Указываем что следует игнорировать строки начинающиеся с #
                parser.CommentTokens = new string[] {"#"};

                DateTime prevSkyclock = DateTime.MinValue;
                while (!parser.EndOfData)
                {
                    string[] fields = null;
                    try
                    {
                        fields = parser.ReadFields();
                    }
                    catch (MalformedLineException)
                    {
                        // Игнорируем "плохие" строки
                        continue;
                    }

                    // Поля в файле
                    //      0 - Year
                    //      1 - Month
                    //      2 - Day
                    //      3 - Local_time
                    //      4 - day_of_year
                    //      5 - hour_of_day
                    //      6 - Sky_Quality_(mag/arc_sec_**2)
                    //      7 - SQM_temperature_(Celsius)
                    //      8 - cloud_cover_(%)
                    //      9 - seeing_(1-5)
                    //     10 - transparency_(1-5)
                    //     11 - skyclock_time/date_used
                    //     12 - sunrise
                    //     13 - sunset
                    //     14 - moonrise
                    //     15 - moonset
                    //     16 - moonphase
                    DateTime skyclock = DateTime.ParseExact(fields[11], "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
                    int moonPhase = int.Parse(fields[16]);

                    if (prevSkyclock != skyclock.Date)
                    {
                        moonPhases.Add(new MoonPhase { Date = skyclock.Date, Phase = moonPhase });
                        prevSkyclock = skyclock.Date;
                    }
                }
            }

            foreach (MoonPhase phase in moonPhases)
            {
                Console.WriteLine("{0:d} - {1}", phase.Date, phase.Phase);
            }
        }
        public static List<ForexRecord> BuildRecords()
        {
            var dataFile = ConfigurationManager.AppSettings["TestDataDirectory"] + "\\ForexTrading.csv";

            var records = new List<ForexRecord>();
            using (var parser = new TextFieldParser(dataFile))
            {
                parser.TextFieldType = FieldType.Delimited;
                parser.SetDelimiters(",");

                while (!parser.EndOfData)
                {
                    var fields = parser.ReadFields();
                    if (fields == null)
                    {
                        continue;
                    }
                    records.Add(new ForexRecord
                    {
                        CurrencyPair = fields[0],
                        Date = fields[1],
                        Bid = double.Parse(fields[2]),
                        Ask = double.Parse(fields[3])
                    });
                }

            }
            return records;
        }
        public ProviderMgr()
        {
            TextFieldParser parser = new TextFieldParser(@"" + Directory.GetCurrentDirectory() + "\\dnscrypt-resolvers.csv");
            parser.TextFieldType = FieldType.Delimited;
            parser.SetDelimiters(",");

            while (!parser.EndOfData)
            {
                ProviderItem providerItem = new ProviderItem();

                string[] fields = parser.ReadFields();

                providerItem.setName(fields[0]);
                providerItem.setFullName(fields[1]);
                providerItem.setDescription(fields[2]);
                providerItem.setLocation(fields[3]);
                providerItem.setCoordinates(fields[4]);
                providerItem.setURL(fields[5]);
                providerItem.setVersion(fields[6]);
                providerItem.setDNSSEC(fields[7]);
                providerItem.setNoLogs(fields[8]);
                providerItem.setNamecoin(fields[9]);
                providerItem.setAddress(fields[10]);
                providerItem.setProviderName(fields[11]);
                providerItem.setPublicKey(fields[12]);
                providerItem.setPublicKeyTXT(fields[13]);
                providerList.Add(providerItem);
            }
            parser.Close();

            // Remove first line from CVS (Name, etc, etc)
            providerList.RemoveAt(0);
        }
Beispiel #18
0
        static void Main(string[] args)
        {
            PrintHeading();

            if (!File.Exists(Settings.Default.ImportFile))
            {
                Console.WriteLine("Could not find data file at '" + Settings.Default.ImportFile);
                return;
            }

            if (File.Exists(Settings.Default.OutputFile))
                File.Delete(Settings.Default.OutputFile);

            var parser = new TextFieldParser(Settings.Default.ImportFile);
            using (var sw = new StreamWriter(Settings.Default.OutputFile))
            {
                parser.SetDelimiters(new[] {"\t"});

                // Consume first row.
                if (!parser.EndOfData) parser.ReadFields();
                while (!parser.EndOfData)
                {
                    var fields = parser.ReadFields();

                    var call = new SatComCallIndentifier(fields);
                    Console.WriteLine(call.GetSqlInsert());
                    sw.WriteLine(call.GetSqlInsert());
                }
            }
            Console.WriteLine();
            Console.WriteLine("Outputted sql to: " + Settings.Default.OutputFile);
            Console.ReadLine();
        }
        public TradeDataEntry[] Read(string filePath)
        {
            using (var parser = new TextFieldParser(filePath))
            {
                parser.TextFieldType = FieldType.Delimited;
                parser.SetDelimiters(",");
                var tradeDataEntries = new List<TradeDataEntry>();

                while (!parser.EndOfData)
                {
                    var fields = parser.ReadFields();

                    if (fields != null)
                    {
                        if (fields.Length != 6)
                        {
                            return new TradeDataEntry[] { };
                        }

                        var date = DateTime.ParseExact(fields[0], "yyyy-M-d", null);
                        var open = double.Parse(fields[1], CultureInfo.InvariantCulture);
                        var high = double.Parse(fields[2], CultureInfo.InvariantCulture);
                        var low = double.Parse(fields[3], CultureInfo.InvariantCulture);
                        var close = double.Parse(fields[4], CultureInfo.InvariantCulture);
                        var volume = int.Parse(fields[5]);
                        tradeDataEntries.Add(new TradeDataEntry(date, open, high, low, close, volume));
                    }
                }
                return tradeDataEntries.ToArray();
            }
        }
        public void Go()
        {
            var inputPath = IO.GetInputFilePath("TextFieldParseRegex.txt");
            var line = new string('=', 40);
            Console.WriteLine(line);
            using (var parser = new TextFieldParser(inputPath))
            {
                parser.TextFieldType = FieldType.Delimited;
                parser.SetDelimiters(";");
                parser.TrimWhiteSpace = true;
                parser.HasFieldsEnclosedInQuotes = true;

                while (parser.PeekChars(1) != null)
                {
                    Console.WriteLine("Line {0}: ", parser.LineNumber);
                    var cleanFieldRowCells = parser.ReadFields().Select(
                        f => f.Trim(new[] { ' ', '"' })).ToArray();
                    for (int i = 0; i < cleanFieldRowCells.Length; ++i)
                    {
                        Console.WriteLine(
                            "Field[{0}] = [{1}]", i, cleanFieldRowCells[i]
                        );
                    }
                    Console.WriteLine(line);
                }
            }
        }
        /// <summary>
        /// Parses CSV file.
        /// </summary>
        /// <param name="file">File to parse.</param>
        /// <param name="entityNameField">Name of a field containing entity names.</param>
        /// <returns>Parsed entities</returns>
        public IEnumerable<object> Parse(string file, string entityNameField)
        {
            using (var parser = new TextFieldParser(file))
              {
            parser.SetDelimiters(new[] { "," });
            parser.HasFieldsEnclosedInQuotes = true;
            parser.TrimWhiteSpace = true;

            // Read header.
            var columns = parser.ReadFields().Select(col => col.ToLowerInvariant()).ToArray();

            entityNameField = entityNameField.ToLowerInvariant();

            if (columns.FirstOrDefault(col => col == entityNameField) == null)
            {
              throw new Exception("File '" + file + "' does not contain required field '" + entityNameField + "'");
            }

            // Key is field name, value is field value.
            var row = new Dictionary<string, string>();

            // Parse rows.
            while (!parser.EndOfData)
            {
              var values = parser.ReadFields();

              for (int i = 0; i < columns.Length; i++)
              {
            row[columns[i]] = values[i];
              }

              yield return CreateEntity(row, entityNameField);
            }
              }
        }
        public DataTable GetDataTabletFromCSVFile(string csv_file_path, string service)
        {
            DataTable csvData = new DataTable();
            try
            {
                using (TextFieldParser csvReader = new TextFieldParser(csv_file_path))
                {
                    csvReader.SetDelimiters(new string[] { "," });
                    csvReader.HasFieldsEnclosedInQuotes = true;
                    string[] colFields = csvReader.ReadFields();

                    for (int i = 0; i < colFields.Length; i++)
                    {
                        colFields[i] = this.RemoveSpecialCharacters(colFields[i]);
                    }

                    foreach (string column in colFields)
                    {
                        DataColumn datecolumn = new DataColumn(column);
                        datecolumn.AllowDBNull = true;
                        csvData.Columns.Add(datecolumn);
                    }
                    while (!csvReader.EndOfData)
                    {
                        string[] fieldData = csvReader.ReadFields();

                        for (int i = 0; i < fieldData.Length; i++)
                        {
                            if (fieldData[i] == "")
                            {
                                fieldData[i] = null;
                            }
                            else
                            {
                                switch (service)
                                {
                                    case "email":
                                        fieldData[i] = this.RemoveSpecialCharactersEmail(fieldData[i]);
                                        break;
                                    case "deduplicate":
                                        fieldData[i] = this.RemoveSpecialCharactersDeduplicate(fieldData[i]);
                                        break;
                                    default:
                                        fieldData[i] = this.RemoveSpecialCharacters(fieldData[i]);
                                        break;
                                }
                            }
                        }
                        csvData.Rows.Add(fieldData);
                    }
                }
            }
            catch (Exception ex)
            {
                //TO DO
            }
            return csvData;
        }
        private TextFieldParser CreateExpectedParser(string input, bool trimWhiteSpace, bool hasFieldsEnclosedInQuotes)
        {
            var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(new StringReader(input));

            parser.SetDelimiters(",");
            parser.TrimWhiteSpace            = trimWhiteSpace;
            parser.HasFieldsEnclosedInQuotes = hasFieldsEnclosedInQuotes;
            return(parser);
        }
        protected void importMainFileButton_Click(object sender, EventArgs e)
        {
            string fileName = @"~/App_Data/" + mainFileTextBox.Text;

            if (File.Exists(Server.MapPath(fileName)))
            {
                DataTable csvDataTable = new DataTable();
                using (TextFieldParser csvReader = new TextFieldParser(Server.MapPath(fileName)))
                {

                    csvReader.SetDelimiters(new string[] { "," });
                    csvReader.HasFieldsEnclosedInQuotes = false;
                    string[] colFields = csvReader.ReadFields();
                    //foreach (string column in colFields)
                    //{
                    //    DataColumn datacolumn = new DataColumn(column);
                    //    datacolumn.AllowDBNull = true;
                    //    csvDataTable.Columns.Add(datacolumn);
                    //}
                    csvDataTable.Columns.Add("Code", typeof(string));
                    csvDataTable.Columns.Add("Type", typeof(string));
                    csvDataTable.Columns.Add("Year", typeof(string));
                    csvDataTable.Columns.Add("Make", typeof(string));
                    csvDataTable.Columns.Add("Model", typeof(string));
                    csvDataTable.Columns.Add("Trim", typeof(string));
                    csvDataTable.Columns.Add("Drive", typeof(string));
                    csvDataTable.Columns.Add("Doors", typeof(string));
                    csvDataTable.Columns.Add("Body", typeof(string));
                    csvDataTable.Columns.Add("Wholesale", typeof(string));
                    csvDataTable.Columns.Add("Retail", typeof(string));

                    while (!csvReader.EndOfData)
                    {
                        string[] fieldData = csvReader.ReadFields();
                        //Making empty value as null
                        for (int i = 0; i < fieldData.Length; i++)
                        {
                            if (fieldData[i] == "")
                            {
                                fieldData[i] = null;
                            }
                        }
                        csvDataTable.Rows.Add(fieldData);
                    }

                }
                if (SaveMainCSVFileData(csvDataTable)>0)
                {
                    mainFileStatusLabel.Text = "Imported to data table";
                }
            }
            else
            {
                mainFileStatusLabel.Text = "File not found.";
            }
        }
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("Need a CSV.");
                Environment.Exit(1);
            }

            Console.WriteLine("Paste a valid GitHub OAuth access token (or else everything will fail.):");
            string oAuthKey = Console.ReadLine();

            if (oAuthKey == null || oAuthKey.Length != 40)
            {
                Console.WriteLine("That doesn't look like a valid access token. Exiting.");
                Environment.Exit(1);
            }

            Worker.OAuthKey = oAuthKey;

            Console.WriteLine("Creating pool of 10 worker threads.");
            List<Thread> threadPool = new List<Thread>();
            for (int i = 0; i < 10; i++)
            {
                Worker worker = new Worker();
                Thread workerThread = new Thread(new ThreadStart(worker.Run));
                workerThread.Start();
                threadPool.Add(workerThread);
            }

            Console.WriteLine("Adding work from CSV.");
            using (TextFieldParser parser = new TextFieldParser(args[0]))
            {
                parser.TextFieldType = FieldType.Delimited;
                parser.SetDelimiters(",");
                while (!parser.EndOfData)
                {
                    string[] fields = parser.ReadFields();
                    Worker.Repositories.Add(fields[0]);
                }
            }

            Console.WriteLine("Completed adding work.");
            Worker.Repositories.CompleteAdding();

            foreach (Thread workerThread in threadPool)
            {
                workerThread.Join();
            }

            Console.WriteLine("Work is completed.");

            foreach (KeyValuePair<string, string> keyValuePair in Worker.RepositoryStatistics)
            {
                Console.WriteLine("Result for {0}: {1}", keyValuePair.Key, keyValuePair.Value);
            }
        }
Beispiel #26
0
        private void openButton_Click(object sender, EventArgs e)
        {
            List<string[]> parsedData = new List<string[]>();
            try
            {

                // Detect the path of documents folder and assign it to initial directory path
                String myDocument = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                openFile.InitialDirectory = myDocument;

                if (openFile.ShowDialog() == DialogResult.OK)
                {

                    Microsoft.VisualBasic.FileIO.TextFieldParser parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(openFile.FileName);
                    parser.TextFieldType = FieldType.Delimited;
                    parser.SetDelimiters(",");
                    parser.TrimWhiteSpace = true;
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();

                    while (!parser.EndOfData)
                    {
                        string[] fields = parser.ReadFields();
                        parsedData.Add(fields);
                        Trace.WriteLine(parsedData);
                        /*foreach (string field in fields)
                        {
                            parsedData.Add(field.ToString());
                            Trace.WriteLine(field);

                        }
                        */

                    }

                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Beispiel #27
0
        public static void ImportCSV(string file)
        {
            TextFieldParser parser = new TextFieldParser(file);
            parser.TextFieldType = FieldType.Delimited;
            parser.SetDelimiters(",");
            Boolean header = true;
            while (!parser.EndOfData)
            {
                // We don't want to import the header row
                if (header)
                {
                    header = false;
                    parser.ReadFields();
                    continue;
                }

                // Vars
                DateTime dt;
                DateTime dt2 = Convert.ToDateTime("4/15/2010");
                Boolean error = false;
                string[] fields = parser.ReadFields();

                // Check for error conditions
                if (String.IsNullOrEmpty(fields[3]))
                {
                    // Score is null
                    fields[3] = "0";
                    error = true;
                }
                if (!DateTime.TryParse(fields[2], out dt))
                {
                    // Date is invalid
                    fields[2] = "05/05/55";
                    error = true;
                }
                if (dt > dt2)
                {
                    // Date is > 4/15/2010
                    error = true;
                }

                // Insert into the correct table
                if (error)
                {
                    InsertError(fields[0], fields[1], fields[2], fields[3]);
                }
                else
                {
                    Insert(fields[0], fields[1], fields[2], fields[3]);
                }
            }
            parser.Close();
        }
Beispiel #28
0
        private void openButton_Click(object sender, EventArgs e)
        {
            List <string[]> parsedData = new List <string[]>();

            try
            {
                // Detect the path of documents folder and assign it to initial directory path
                String myDocument = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                openFile.InitialDirectory = myDocument;

                if (openFile.ShowDialog() == DialogResult.OK)
                {
                    Microsoft.VisualBasic.FileIO.TextFieldParser parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(openFile.FileName);
                    parser.TextFieldType = FieldType.Delimited;
                    parser.SetDelimiters(",");
                    parser.TrimWhiteSpace = true;
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();
                    parser.ReadLine();



                    while (!parser.EndOfData)
                    {
                        string[] fields = parser.ReadFields();
                        parsedData.Add(fields);
                        Trace.WriteLine(parsedData);

                        /*foreach (string field in fields)
                         * {
                         *  parsedData.Add(field.ToString());
                         *  Trace.WriteLine(field);
                         *
                         * }
                         */
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public void Go()
        {
            var inputPath = IO.GetInputFilePath("TextFieldParserKeepWhiteSpace.txt");

            var separator = new string('=', 40);
            Console.WriteLine(separator);
            // demo only - show the input lines read from a text file 
            var text = File.ReadAllText(inputPath);
            var lines = text.Split(
                new string[] { Environment.NewLine }, 
                StringSplitOptions.None
            );

            using (var textReader = new StringReader(text))
            {
                using (var parser = new TextFieldParser(textReader))
                {
                    parser.TextFieldType = FieldType.Delimited;
                    parser.SetDelimiters(",");
                    parser.TrimWhiteSpace = true;
                    parser.HasFieldsEnclosedInQuotes = false;
                    // remove double quotes, since HasFieldsEnclosedInQuotes is false
                    var regex = new Regex(@"
                    # match double quote 
                    \""    
                    # if not immediately followed by a double quote
                    (?!\"")
                    ",
                        RegexOptions.IgnorePatternWhitespace
                    );

                    var rowStart = 0;
                    while (parser.PeekChars(1) != null)
                    {
                        Console.WriteLine(
                            "row {0}: {1}", parser.LineNumber, lines[rowStart]
                        );
                        var fields = parser.ReadFields();
                        for (int i = 0; i < fields.Length; ++i)
                        {
                            Console.WriteLine(
                                "parsed field[{0}] = [{1}]", i,
                                regex.Replace(fields[i], "")
                            );
                        }
                        ++rowStart;
                        Console.WriteLine(separator);
                    }
                }
            }


        }
Beispiel #30
0
        public MainWindow()
        {
            InitializeComponent();

            sfx.Add(new SoundPlayer(snd_click));
            sfx.Add(new SoundPlayer(snd_nav));

            try
            {
                sfx.Single(s => s.SoundLocation == snd_click).Load();
            }
            catch (Exception ex)
            {
                logError(ex.Message, EventLogEntryType.Error);
            }

            try
            {
                sfx.Single(s => s.SoundLocation == snd_nav).Load();
            }
            catch (Exception ex)
            {
                logError(ex.Message, EventLogEntryType.Error);
            }

            if (FileSystem.FileExists(LAUNCHER_CFG))
            {
                using (TextFieldParser parser = new TextFieldParser(LAUNCHER_CFG))
                {
                    parser.TextFieldType = FieldType.Delimited;
                    parser.SetDelimiters(",");
                    while (!parser.EndOfData)
                    {
                        string[] fields = parser.ReadFields();
                        if (fields.Length == 4)
                        {
                            launchers.Add(new Launcher(
                                fields[0].StartsWith("#", StringComparison.Ordinal) ? "" : fields[0],
                                fields[1],
                                fields[2],
                                fields[3]
                                ));
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show(string.Format(localized.ERROR_LAUNCHER_CONFIG, LAUNCHER_CFG), localized.ERROR, MessageBoxButton.OK, MessageBoxImage.Error);
                Environment.Exit(1);
            }
        }
        public List<string[]> parsecsv(string path)
        {
            List<string[]> parsedData = new List<string[]>();
            string[] fields;

            try
            {
                //TODO add check to see if the csv file is a valid one or validate data in each field
                TextFieldParser parser = new TextFieldParser(path);
                parser.TextFieldType = FieldType.Delimited;
                parser.SetDelimiters(",");
                parser.TrimWhiteSpace = true;//trims white space from the strings
                int numExcludedRows = 0;

                while (!parser.EndOfData)
                {
                    fields = parser.ReadFields();

                    //check to see if the row has any blank fields
                    if (fields.Contains(""))
                    {
                        //do nothing with the string
                        numExcludedRows++;
                    }
                    else //the string doesn't include blank fields
                    {
                                parsedData.Add(fields);
                    }
                }

                if (numExcludedRows > 0)
                {
                    MessageBox.Show("Some rows were incomplete, removed data for " + numExcludedRows.ToString() + " ties.", "SkyRise Canopy Creator");
                }

                //remove the headers from the List
                if (parsedData.Count() >= 2)
                {
                    parsedData.RemoveAt(0);
                }

                //close the reader
                parser.Close();

            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }

            return parsedData;
        }
Beispiel #32
0
        /// <summary>
        /// CSVを読み込みます。
        /// </summary>
        /// <param name="path">読み込みディレクトリパス</param>
        /// <param name="filename">読み込みファイル名</param>
        /// <returns>読み込んだCSVをDataTableで返却</returns>
        public DataTable CsvReader(string path, string filename)
        {
            string[] data;
            DataTable dt = new DataTable();
            TextFieldParser parser = new TextFieldParser(path + filename, encord);
            parser.TextFieldType = FieldType.Delimited;

            // 区切り文字はコンマ
            parser.SetDelimiters(",");

            //データがあるか確認します。
            if (!parser.EndOfData)
            {
                //CSVファイルから1行読み取ります。
                data = parser.ReadFields();

                //カラムの数を取得します。
                int cols = data.Length;

                try
                {
                    for (int i = 0; i < cols; i++)
                    {
                        //カラム名をセットします
                        dt.Columns.Add(data[i]);
                    }
                }
                catch (System.Data.DuplicateNameException)
                {
                    MessageBox.Show( "読み込みエラー\nチェックリストの中に重複している値がないか確認し、修正を行ってから実行しなおしてください。" );
                    //DataTable aa = new DataTable();
                    //return aa;
                }
            }

            // CSVをデータテーブルに格納
            while (!parser.EndOfData)
            {
                data = parser.ReadFields();
                DataRow row = dt.NewRow();

                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    row[i] = data[i];
                }

                dt.Rows.Add(row);
            }

            parser.Dispose();
            return dt;
        }
Beispiel #33
0
        /// <summary>
        /// CSVファイルをstring[][]型に変換する。
        /// </summary>
        /// <param name="parser"></param>
        /// <returns></returns>
        public static string[][] Open(TextFieldParser parser)
        {
            parser.TextFieldType = FieldType.Delimited;
            parser.SetDelimiters(",");

            var data = new List<string[]>();
            while (!parser.EndOfData)
            {
                data.Add(parser.ReadFields());
            }

            return data.ToArray();
        }
Beispiel #34
0
        /// <summary>
        /// Parses a big text blob into rows and columns, using the settings
        /// </summary>
        /// <param name="text">Big blob of text</param>
        /// <returns>Parsed data</returns>
        public List <string[]> Parse(string text)
        {
            // The actual _parsing_ .NET can handle. Well, VisualBasic anyway...
            using (var reader = new StringReader(text))
                using (var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(reader))
                {
                    var errors = new StringBuilder();
                    if (this.CommentCharacter != default(char))
                    {
                        parser.CommentTokens = new[] { this.CommentCharacter.ToString() }
                    }
                    ;
                    parser.SetDelimiters(this.Separator.ToString());
                    parser.HasFieldsEnclosedInQuotes = this.TextQualifier != default(char);

                    if (this.FieldWidths != null)
                    {
                        parser.TextFieldType = FieldType.FixedWidth;
                        try
                        {
                            parser.SetFieldWidths(this.FieldWidths.ToArray());
                        }
                        catch (Exception e)
                        {
                            errors.AppendLine(e.Message);
                        }
                    }

                    var ret = new List <string[]>();
                    while (!parser.EndOfData)
                    {
                        try
                        {
                            ret.Add(parser.ReadFields());
                        }
                        catch (MalformedLineException e)
                        {
                            errors.AppendFormat("Error on line {0}: {1}\n", e.LineNumber, e.Message);
                        }
                    }
                    if (errors.Length > 0)
                    {
                        MessageBox.Show(errors.ToString(), "Errors");
                    }
                    return(ret);
                }
        }
        public static DataTable DataTableFromTextFile(string location, char delimiter)
        {
            DataTable       result;
            List <string[]> data   = new List <string[]>();
            var             parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(location);

            parser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited;
            parser.SetDelimiters(new string[] { "," });

            while (!parser.EndOfData)
            {
                string[] row = parser.ReadFields();
                data.Add(row);
            }

            result = FormDataTable(data, delimiter);

            return(result);
        }
        public void ReadLine_Sample()
        {
            const string input = @"Name,Birth Date
Apollo Creed,1942-08-17
Ivan Drago,1961-11-03";

            var parserReader = new StringReader(input);
            var parser       = new NotVisualBasic.FileIO.CsvTextFieldParser(parserReader);
            var vbParser     = new Microsoft.VisualBasic.FileIO.TextFieldParser(new StringReader(input));

            vbParser.SetDelimiters(",");

            Assert.Equal(vbParser.ReadLine(), parserReader.ReadLine());
            Assert.Equal(vbParser.ReadLine(), parserReader.ReadLine());
            Assert.Equal(vbParser.ReadLine(), parserReader.ReadLine());
            Assert.Null(vbParser.ReadFields());
            Assert.Null(parser.ReadFields());
            Assert.True(vbParser.EndOfData);
            Assert.True(parser.EndOfData);
        }
Beispiel #37
0
        /// <summary>
        /// Uses the Microsoft Text Field Parser to parse the CSV file, to give a baseline against an
        /// established class library for parsing.
        /// </summary>
        private static void _TextFieldParserCsv()
        {
            string[] fields;
            string   s;

            using (VB.TextFieldParser tfp = new VB.TextFieldParser(PerformanceTests.CSV_DATA_FILE))
            {
                tfp.SetDelimiters(",");
                tfp.CommentTokens             = new string[] { "#" };
                tfp.HasFieldsEnclosedInQuotes = true;

                while (!tfp.EndOfData)
                {
                    fields = tfp.ReadFields();

                    for (int i = 0; i < fields.Length; ++i)
                    {
                        s = fields[i] as string;
                    }
                }
            }
        }
Beispiel #38
0
        private void AddDataFromFile(string data_file)
        {
            using (TextFieldParser parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(data_file))
            {
                parser.TrimWhiteSpace = true;
                parser.TextFieldType  = FieldType.Delimited;
                parser.SetDelimiters("\t");

                System.Windows.Forms.DataVisualization.Charting.Series series = null;
                while (true)
                {
                    string[] parts = parser.ReadFields();
                    if (parts == null)
                    {
                        break;
                    }

                    if (parts.Length >= 3)
                    {
                        string name  = parts[0];
                        string date  = parts[1];
                        string value = parts[2];

                        if (series == null)
                        {
                            if (chartQuotes.Series.FindByName(name) == null)
                            {
                                chartQuotes.Series.Add(name);
                                series           = chartQuotes.Series[name];
                                series.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
                            }
                            else
                            {
                                break;
                            }
                        }

                        DateTime dt;
                        double   d;
                        if (Double.TryParse(value, out d) && DateTime.TryParse(date, out dt))
                        {
                            series.Points.AddXY(dt, d);

                            if (!dateMin.HasValue)
                            {
                                dateMin = new DateTime();
                                dateMin = dt;
                            }
                            else if (dt < dateMin)
                            {
                                dateMin = dt;
                            }

                            if (!dateMax.HasValue)
                            {
                                dateMax = new DateTime();
                                dateMax = dt;
                            }
                            else if (dt > dateMax)
                            {
                                dateMax = dt;
                            }
                        }
                    }
                }
            }

            if (dateMin.HasValue && dateMax.HasValue)
            {
                chartQuotes.ChartAreas[0].AxisX.Minimum = dateMin.Value.ToOADate();
                chartQuotes.ChartAreas[0].AxisX.Maximum = dateMax.Value.ToOADate();

                dateFrom.Value = dateMin.Value;
                dateTo.Value   = dateMax.Value;
            }
        }
Beispiel #39
0
        public static void ImportCSVtoSubModel(
            string inputFn, AdminShell.AdministrationShellEnv env, AdminShell.Submodel sm, AdminShell.SubmodelRef smref)
        {
            AdminShell.SubmodelElementCollection[] propGroup = new AdminShell.SubmodelElementCollection[10];
            int i_propGroup = 0;

            var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(inputFn);

            parser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited;
            parser.SetDelimiters(";");

            string[] rows = parser.ReadFields();
            if (rows == null)
            {
                throw new InvalidOperationException(
                          $"There were no fields read from the inputFn: {inputFn}");
            }

            if ((rows[0] != "typeName" ||
                 rows[1] != "idShort" ||
                 rows[2] != "value") ||
                (rows.Length > 3 &&
                 (
                     rows[3] != "valueType" ||
                     rows[4] != "category" ||
                     rows[5] != "descriptionEN" ||
                     rows[6] != "descriptionDE"
                 )))
            {
                return;
            }

            sm.idShort = inputFn.Split('\\').Last().Replace(".csv", "");

            while (!parser.EndOfData)
            {
                rows = parser.ReadFields();

                if (rows == null)
                {
                    throw new InvalidOperationException(
                              $"There were no fields read from inputFn: {inputFn}");
                }

                switch (rows[0])
                {
                case "SubmodelElementCollection":
                    propGroup[i_propGroup] = AdminShell.SubmodelElementCollection.CreateNew(rows[1]);
                    if (i_propGroup == 0)
                    {
                        sm.Add(propGroup[0]);
                        if (rows.Length > 3)
                        {
                            if (rows[7] != "")
                            {
                                propGroup[0].semanticId = new AdminShellV20.SemanticId(
                                    AdminShell.Reference.CreateNew(
                                        "ConceptDescription", false, "IRI", rows[7]));
                            }
                        }
                        propGroup[0].kind = AdminShellV20.ModelingKind.CreateAsInstance();
                    }
                    else
                    {
                        propGroup[i_propGroup - 1].Add(propGroup[i_propGroup]);
                    }
                    i_propGroup++;
                    break;

                case "End-SubmodelElementCollection":
                    if (i_propGroup != 0)
                    {
                        i_propGroup--;
                    }
                    break;

                case "Property":
                    var p = AdminShell.Property.CreateNew(rows[1].Replace("-", "_"));
                    p.value = rows[2];
                    if (rows.Length > 3)
                    {
                        p.valueType = rows[3];
                        p.category  = rows[4];
                        if (rows[5] != "")
                        {
                            p.AddDescription("en", rows[5]);
                        }
                        if (rows[6] != "")
                        {
                            p.AddDescription("de", rows[6]);
                        }
                        p.kind = AdminShellV20.ModelingKind.CreateAsInstance();
                        if (rows[7] != "")
                        {
                            p.semanticId = new AdminShell.SemanticId(
                                AdminShell.Reference.CreateNew(
                                    "ConceptDescription", false, "IRI", rows[7]));
                        }
                    }
                    if (i_propGroup == 0)
                    {
                        sm.Add(p);
                    }
                    else
                    {
                        propGroup[i_propGroup - 1].Add(p);
                    }
                    break;
                }
            }
        }
Beispiel #40
0
        public CSVFile UploadToObject(string project, string csvFile)
        // Parses the uploaded file to a CSVFile object containing alll necessary data from the file
        {
            var nameList  = new List <string>();
            var valueList = new List <List <string> >();
            var typeList  = new List <string>();
            var path      = Path.Combine(Server.MapPath("~/App_Data/Files/"), csvFile);
            var parser    = new Microsoft.VisualBasic.FileIO.TextFieldParser(path, System.Text.Encoding.UTF7);

            parser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited;
            // delimiter set to ; - this is where we could change code to handle other delimiters as well
            parser.SetDelimiters(new string[] { ";" });
            int i = 0;

            // Parse the file content
            while (!parser.EndOfData)
            {
                // Get one row of data
                string[] row = parser.ReadFields();
                // Get the column names
                if (i == 0)
                {
                    nameList = row.ToList();
                }
                else  // get file content
                {
                    valueList.Add(row.ToList());
                    if (i == 1)
                    {
                        // Define types of the elements of the first row with data
                        typeList = row.ToList();
                        int j = 0;
                        foreach (var term in row)
                        {
                            if (double.TryParse(term, out double n))
                            {
                                typeList[j] = "N";
                            }
                            else
                            {
                                typeList[j] = "S";
                            }
                            j++;
                        }
                    }
                    else
                    {
                        // Check the types of the eleents of the following rows, change to S (string) if we detect one
                        int j = 0;
                        foreach (var term in row)
                        {
                            if (!double.TryParse(term, out double n))
                            {
                                typeList[j] = "S";
                            }
                            j++;
                        }
                    }
                }
                i++;
            }
            // Create CSVFile object, give the properties values and return
            CSVFile myFile = new CSVFile();

            myFile.Project   = project;
            myFile.NameList  = nameList;
            myFile.ValueList = valueList;
            myFile.TypeList  = typeList;
            return(myFile);
        }
Beispiel #41
0
        public static void ImportCSVtoSubModel(
            string inputFn, AdminShell.AdministrationShellEnv env, AdminShell.Submodel sm,
            AdminShell.SubmodelRef smref)
        {
            AdminShell.SubmodelElementCollection[] propGroup = new AdminShell.SubmodelElementCollection[10];
            int i_propGroup = 0;

            var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(inputFn);

            parser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited;
            parser.SetDelimiters(";");

            string[] rows = parser.ReadFields();
            if (rows == null || rows.Length < 3 ||
                rows[0] != "typeName" ||
                rows[1] != "idShort" ||
                rows[2] != "value")
            {
                return;
            }

            while (!parser.EndOfData)
            {
                rows = parser.ReadFields();
                if (rows == null || rows.Length < 1)
                {
                    continue;
                }

                switch (rows[0])
                {
                case "SubmodelElementCollection":
                    propGroup[i_propGroup] = AdminShell.SubmodelElementCollection.CreateNew(rows[1]);
                    if (i_propGroup == 0)
                    {
                        sm.Add(propGroup[0]);
                    }
                    else
                    {
                        propGroup[i_propGroup - 1].Add(propGroup[i_propGroup]);
                    }
                    i_propGroup++;
                    break;

                case "End-SubmodelElementCollection":
                    if (i_propGroup != 0)
                    {
                        i_propGroup--;
                    }
                    break;

                case "Property":
                    var p = AdminShell.Property.CreateNew(rows[1]);
                    p.valueType = "string";
                    p.value     = rows[2];

                    if (i_propGroup == 0)
                    {
                        sm.Add(p);
                    }
                    else
                    {
                        propGroup[i_propGroup - 1].Add(p);
                    }
                    break;
                }
            }
        }
Beispiel #42
0
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        var valid = true;

        if (!FileUploadControl.HasFile)
        {
            return;
        }
        var filename       = FileUploadControl.FileName;
        var s              = filename.Split('.');
        var extension      = s[s.Length - 1];
        var saveAsFileName = "UploadKtaBatchPayment_" + DateTime.Now.ToString("yyyyMMddhhmmss") + "." + extension;
        var contenttype    = FileUploadControl.PostedFile.ContentType;
        var log            = "";

        try
        {
            if (contenttype == "application/octet-stream" || contenttype == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" || contenttype == "application/vnd.ms-excel" || contenttype == "text/csv")
            {
                FileUploadControl.SaveAs(Server.MapPath(@"../../temp/xls/") + saveAsFileName);
                log += "Upload file " + filename + " success.\t" + DateTime.Now.ToString(CultureInfo.InvariantCulture) + "\n\r";
                var uploadedPath = Server.MapPath(@"../../temp/xls/") + saveAsFileName;

                var ctr    = 0;
                var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(uploadedPath);
                parser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited;
                parser.SetDelimiters(new string[] { "," });

                while (!parser.EndOfData)
                {
                    string[] row = parser.ReadFields();
                    if (ctr == 0)
                    {
                        ctr = 1;
                    }
                    else
                    {
                        if (row != null)
                        {
                            var employee_code = row[0];
                            var employee_name = row[1];
                            var start_date    = row[5];
                            var end_date      = row[6];
                            var Quota         = row[7];

                            if (employee_code != "Employee_NIK")
                            {
                                try
                                {
                                    //InsertXlsRowData(row);
                                    EmpLeaveBalance _dalEmpLeaveBalance = null;
                                    Hashtable       htParameters        = null;

                                    _dalEmpLeaveBalance = new EmpLeaveBalance();
                                    htParameters        = new Hashtable();
                                    var iNextId = 0;
                                    htParameters.Clear();
                                    htParameters["p_Employee_Code"] = employee_code;
                                    htParameters["p_START_DATE"]    = Utility.ToDateTime(start_date);
                                    htParameters["p_END_DATE"]      = Utility.ToDateTime(end_date);
                                    htParameters["p_Quota"]         = Quota;
                                    Utility.ApplyDefaultProp(htParameters);
                                    _dalEmpLeaveBalance.InsertTempBalance(htParameters, ref iNextId);

                                    log += "Insert Data : " + employee_code + " - " + employee_name + " Success.\t" +
                                           DateTime.Now.ToString(CultureInfo.InvariantCulture) + "\n\r";
                                }
                                catch (Exception exc)
                                {
                                    log += "Insert Data : " + employee_code + " - " + employee_name +
                                           " Failed.\t" + exc.InnerException.Message + "\t" +
                                           DateTime.Now.ToString(CultureInfo.InvariantCulture) + "\n\r";
                                    valid = false;
                                    // Utility.ShowMessageBox(this, Utility.LOAD_DATA_FAIL_MESSAGE, ex, null, null);
                                }
                            }
                        }
                    }
                }

                Utility.ShowMessageBox(this,
                                       valid ? "Upload Success" : "Upload Success but error in processing data. Check Log for detail info.",
                                       null, "lstblleaveimport.aspx");
                //BindGridUpload();
            }
            else
            {
                Utility.ShowMessageBox(this, "Error Excel Format", null, null);
            }
        }
        catch (Exception ex)
        {
            Utility.ShowMessageBox(this, "Upload Failed.", ex, null);
        }

        CreateLog(log);
    }
        public static void Main(string[] args)
        {
            // HARD_CODED FOR EXAMPLE ONLY - TO BE RETRIEVED FROM APP.CONFIG IN REAL PROGRAM
            string hospPath = @"C:\\events\\inbound\\OBLEN_COB_Active_Inv_Advi_Daily_.csv";
            string vendPath = @"C:\\events\\outbound\\Advi_OBlen_Active_Inv_Ack_Daily_.csv";
            List <DenialRecord> hospList = new List <DenialRecord>();
            List <DenialRecord> vendList = new List <DenialRecord>();

            //List<DenialRecord> hospExcpt = new List<DenialRecord>();  // Created at point of use for now
            //List<DenialRecord> vendExcpt = new List<DenialRecord>();  // Created at point of use for now
            using (TextFieldParser hospParser = new Microsoft.VisualBasic.FileIO.TextFieldParser(hospPath))
            {
                hospParser.TextFieldType = FieldType.Delimited;
                hospParser.SetDelimiters(",");
                hospParser.HasFieldsEnclosedInQuotes = false;
                hospParser.TrimWhiteSpace            = true;
                while (!hospParser.EndOfData)
                {
                    try
                    {
                        string[] row = hospParser.ReadFields();
                        if (row.Length <= 7)
                        {
                            DenialRecord dr = new DenialRecord(row[0], row[1], row[2], row[3], row[4], row[5], row[6]);
                            hospList.Add(dr);
                        }
                    }
                    catch (Exception e)
                    {
                        // do something
                        Console.WriteLine("Error is:  {0}", e.ToString());
                    }
                }
                hospParser.Close();
                hospParser.Dispose();
            }
            using (TextFieldParser vendParser = new Microsoft.VisualBasic.FileIO.TextFieldParser(vendPath))
            {
                vendParser.TextFieldType = FieldType.Delimited;
                vendParser.SetDelimiters(",");
                vendParser.HasFieldsEnclosedInQuotes = false;
                vendParser.TrimWhiteSpace            = true;
                while (!vendParser.EndOfData)
                {
                    try
                    {
                        string[] row = vendParser.ReadFields();
                        if (row.Length <= 7)
                        {
                            DenialRecord dr = new DenialRecord(row[0], row[1], row[2], row[3], row[4], row[5], row[6]);
                            vendList.Add(dr);
                        }
                    }
                    catch (Exception e)
                    {
                        // do something
                        Console.WriteLine("Error is:  {0}", e.ToString());
                    }
                }
                vendParser.Close();
                vendParser.Dispose();
            }
            // Compare the lists each way for denials not in the other source
            List <DenialRecord> hospExcpt = hospList.Except(vendList).ToList();
            List <DenialRecord> vendExcpt = vendList.Except(hospList).ToList();
        }