public static void ReadCsv(string filePath, string connectString, Action<string> callBack = null,
            Action completed = null)
        {
            _connectString = connectString;
            _callBack = callBack;
            _completed = completed;
            _sb.Clear();

            var headers = new List<string>();

            using (var reader = new ElencySolutions.CsvHelper.CsvReader(filePath, Encoding.Default))
            {
                if (reader.ReadNextRecord())
                {
                    headers = reader.Fields;
                }

                DataTable table = new DataTable("NPIRecords");
                headers.ForEach(x => table.Columns.Add(new DataColumn(Column(x))));

                //CreateTable(table);


                int buffer = 0;
                while (reader.ReadNextRecord())
                {
                    var row = table.NewRow();
                    row.ItemArray = reader.Fields.ToArray();
                    table.Rows.Add(row);
                    buffer++;
                    if (buffer >= _maxBuffer)
                    {
                        BulkCopy(table);
                        buffer = 0;
                        table.Rows.Clear();
                    }
                }

                if (table.Rows.Count > 0)
                {
                    BulkCopy(table);
                }
            }

            if (_completed != null) _completed();
        }
        public static List<Mapper> GetMappers()
        {
            var mappers = new List<Mapper>();

            if (!File.Exists(path))
                return mappers;

            using (CsvReader reader = new CsvReader(path, Encoding.Default))
            {
                while (reader.ReadNextRecord())
                {
                    mappers.Add(new Mapper()
                    {
                        Category = reader.Fields[CategoryIndex],
                        RegexPattern = reader.Fields[RegexPatternIndex]
                    });
                }
            }

            return mappers;
        }
Beispiel #3
0
        public void CsvWriter_WriteDataTableToString()
        {
            if (File.Exists(FilePath))
                File.Delete(FilePath);

            DataTable table = new DataTable();

            using (CsvReader reader = new CsvReader(Encoding.Default, TEST_DATA_5))
            {
                table = reader.ReadIntoDataTable();
            }

            string content = string.Empty;

            using (CsvWriter writer = new CsvWriter())
            {
                content = writer.WriteCsv(table, Encoding.Default);
            }

            File.Delete(FilePath);
            Assert.IsTrue(string.Compare(content, TEST_DATA_5) == 0);
        }
Beispiel #4
0
        public void CsvWriter_WriteDataTableToStream()
        {
            string content = string.Empty;

            using (MemoryStream memoryStream = new MemoryStream())
            {
                DataTable table = new DataTable();

                using (CsvReader reader = new CsvReader(Encoding.Default, TEST_DATA_5))
                {
                    table = reader.ReadIntoDataTable();
                }

                using (CsvWriter writer = new CsvWriter())
                {
                    writer.WriteCsv(table, memoryStream);

                    using (StreamReader reader = new StreamReader(memoryStream))
                    {
                        content = reader.ReadToEnd();
                    }
                }

            }

            Assert.IsTrue(string.Compare(content, TEST_DATA_5) == 0);
        }
Beispiel #5
0
        public void CsvWriter_WriteDataTableToFile()
        {
            if (File.Exists(FilePath))
                File.Delete(FilePath);

            DataTable table = new DataTable();

            using (CsvReader reader = new CsvReader(Encoding.Default, TEST_DATA_5))
            {
                table = reader.ReadIntoDataTable();
            }

            using (CsvWriter writer = new CsvWriter())
            {
                writer.WriteCsv(table, FilePath);
            }

            CsvFile csvFile = CreateCsvFileFromDataTable(table);
            VerifyTestData5(csvFile.Headers, csvFile.Records);
            File.Delete(FilePath);
        }
Beispiel #6
0
        public void CsvReader_TestReadIntoDataTableWithTypes()
        {
            DataTable dataTable = new DataTable();

            using (CsvReader reader = new CsvReader(Encoding.Default, TEST_DATA_1) { HasHeaderRow = true })
            {
                dataTable = reader.ReadIntoDataTable(new[] { typeof(int), typeof(string), typeof(DateTime) });
            }

            CsvFile file = CreateCsvFileFromDataTable(dataTable);
            VerifyTestData1(file.Headers, file.Records);
        }
Beispiel #7
0
        public void CsvReader_TestReadingFromStringWithSampleData6()
        {
            using (CsvReader reader = new CsvReader(Encoding.Default, TEST_DATA_6))
            {
                List<List<string>> records = new List<List<string>>();

                while (reader.ReadNextRecord())
                    records.Add(reader.Fields);

                Assert.IsTrue(records.Count == 2);

                CsvFile csvFile = CreateCsvFile(records[0], records[1]);
                VerifyTestData6(csvFile.Headers, csvFile.Records);
            }
        }
Beispiel #8
0
        public void CsvReader_TestReadingFromStream()
        {
            using (MemoryStream memoryStream = new MemoryStream(TEST_DATA_1.Length))
            {
                using (StreamWriter streamWriter = new StreamWriter(memoryStream))
                {
                    streamWriter.Write(TEST_DATA_1);
                    streamWriter.Flush();

                    using (CsvReader reader = new CsvReader(memoryStream, Encoding.Default))
                    {
                        List<List<string>> records = new List<List<string>>();

                        while (reader.ReadNextRecord())
                            records.Add(reader.Fields);

                        Assert.IsTrue(records.Count == 2);

                        CsvFile csvFile = CreateCsvFile(records[0], records[1]);
                        VerifyTestData1(csvFile.Headers, csvFile.Records);
                    }
                }
            }
        }
Beispiel #9
0
        public void CsvReader_TestReadingFromFile()
        {
            File.WriteAllText(FilePath, TEST_DATA_1, Encoding.Default);

            using (CsvReader reader = new CsvReader(FilePath, Encoding.Default))
            {
                List<List<string>> records = new List<List<string>>();

                while (reader.ReadNextRecord())
                    records.Add(reader.Fields);

                Assert.IsTrue(records.Count == 2);

                CsvFile csvFile = CreateCsvFile(records[0], records[1]);
                VerifyTestData1(csvFile.Headers, csvFile.Records);
            }

            File.Delete(FilePath);
        }
Beispiel #10
0
        public void CsvReader_TestColumnTrimming()
        {
            using (CsvReader reader = new CsvReader(Encoding.Default, TEST_DATA_6) { TrimColumns = true})
            {
                List<List<string>> records = new List<List<string>>();

                while (reader.ReadNextRecord())
                    records.Add(reader.Fields);

                Assert.IsTrue(records.Count == 2);

                CsvFile csvFile = CreateCsvFile(records[0], records[1]);
                VerifyTestData6Trimmed(csvFile.Headers, csvFile.Records);
            }
        }
Beispiel #11
0
        public void CsvFile_PopulateFromFileWithoutHeader()
        {
            CsvFile csvFile1 = new CsvFile();
            using (CsvReader reader = new CsvReader(Encoding.Default, TEST_DATA_5))
            {
                List<List<string>> records = new List<List<string>>();

                while (reader.ReadNextRecord())
                    records.Add(reader.Fields);

                csvFile1 = CreateCsvFile(records[0], records[1]);
            }

            if (File.Exists(FilePath))
                File.Delete(FilePath);

            using (CsvWriter writer = new CsvWriter())
            {
                writer.WriteCsv(csvFile1, FilePath, Encoding.Default);
            }

            CsvFile file = new CsvFile();
            file.Populate(FilePath, false);
            VerifyTestData5Alternative(file.Records);

            File.Delete(FilePath);
        }
        private void ImportButton_Click(object sender, RoutedEventArgs e)
        {
            var dlg = new Microsoft.Win32.OpenFileDialog();

            dlg.FileName = "data"; // Default file name
            dlg.DefaultExt = ".csv"; // Default file extension
            dlg.Filter = "Text documents (.csv)|*.csv"; // Filter files by extension

            Nullable<bool> result = dlg.ShowDialog();

            if (!result.HasValue || !result.Value)
                return;

            string filename = dlg.FileName;

            var records = new List<List<string>>();

            using (CsvReader reader = new CsvReader(filename, Encoding.Default))
            {
                while (reader.ReadNextRecord())
                    records.Add(reader.Fields);
            }

            if (records[0][0] == "Number")
                records.Remove(records[0]);

            //number,date,acc,amount,transactiontype,notes

            csvFile = new CsvFile();

            var headers = new List<string> { "Number", "Date", "Account", "Amount", "Transaction Type", "Notes", "Category", "Sub Category" };

            headers.ForEach(header => csvFile.Headers.Add(header));

            foreach (var fields in records)
            {
                var record = new CsvRecord();

                fields.ForEach(field => record.Fields.Add(field));

                SetCategory(record);

                csvFile.Records.Add(record);
            }
        }
Beispiel #13
0
        //读取压力值到内存中
        public void GetPressure()
        {
            try
            {
                using (CsvReader reader = new CsvReader(path, Encoding.Default))
                {
                    FrameInfo frameInfo = new FrameInfo();
                    int frameIndex = -1;
                    int tmpRow = 0, tmpCol = 0, tmpPressure = 0, totalPressure = 0, totalNonZero = 0, maxPressure = 0;
                    //int mPosX = 0, mPosY = 0;
                    bool isBackFrame = false;
                    while (reader.ReadNextRecord())
                    {
                        if (reader.Fields[0] == "-10")
                        {
                            if (isBackFrame)
                                backFrames.Add(frameInfo);
                            else
                                allFrames.Add(frameInfo);
                            break;
                        }
                        else if (reader.Fields[0] == "-1")
                        {
                            if (frameIndex >= 0)
                            {
                                if (isBackFrame)
                                    backFrames.Add(frameInfo);
                                else
                                    allFrames.Add(frameInfo);
                            }
                            isBackFrame = false;
                            frameInfo = new FrameInfo();
                            frameIndex = frameIndex + 1;
                            totalPressure = 0;
                            totalNonZero = 0;
                            maxPressure = 0;
                            frameInfo.frameIndex = Int32.Parse(reader.Fields[1]);
                            continue;
                        }
                        else if (reader.Fields[0] == "-2")
                        {
                            if (isBackFrame)
                                backFrames.Add(frameInfo);
                            else
                                allFrames.Add(frameInfo);
                            isBackFrame = true;
                            frameInfo = new FrameInfo();
                            tmpRow = 0;
                            totalPressure = 0;
                            totalNonZero = 0;
                            maxPressure = 0;
                            frameInfo.frameIndex = frameIndex;
                            continue;
                        }
                        else if (frameIndex >= 0 && isBackFrame == false)
                        {
                            List<string> tmpRowInfo = reader.Fields;
                            for (int i = 0; i < tmpRowInfo.Count; i++)
                            {
                                string tmp = tmpRowInfo[i];
                                if (tmp.Contains("."))
                                    tmp = tmp.Substring(0, tmp.IndexOf("."));
                                if (tmp != "-1")
                                {
                                    if (i == 0)
                                    {
                                        tmpRow = Int32.Parse(tmp);
                                    }
                                    else if (i % 2 == 1)
                                    {
                                        tmpCol = Int32.Parse(tmp);
                                        totalNonZero = totalNonZero + 1;
                                    }
                                    else
                                    {
                                        tmpPressure = Int32.Parse(tmp);
                                        totalPressure = totalPressure + tmpPressure;
                                        frameInfo.pressureValue[tmpRow, tmpCol] = tmpPressure;
                                        if (tmpPressure > maxPressure)
                                        {
                                            maxPressure = tmpPressure;
                                            //mPosX = tmpRow; mPosY = tmpCol;
                                        }
                                    }
                                }
                                else
                                    break;
                            }
                            frameInfo.Area = totalNonZero;
                            frameInfo.Pressure = totalPressure;
                            frameInfo.MaxPressure = maxPressure;
                        }
                        else if (frameIndex >= 0 && isBackFrame == true)
                        {
                            List<string> tmpRowInfo = reader.Fields;
                            for (int i = 0; i < tmpRowInfo.Count; i++)
                            {
                                string tmp = tmpRowInfo[i];
                                if (tmp.Contains("."))
                                    tmp = tmp.Substring(0, tmp.IndexOf("."));
                                //if (tmp != "-1")
                                {
                                    tmpPressure = Int32.Parse(tmp);
                                    frameInfo.pressureValue[tmpRow, i] = tmpPressure;
                                    totalPressure = totalPressure + tmpPressure;
                                    if (tmp != "0")
                                        totalNonZero = totalNonZero + 1;
                                }
                            }
                            tmpRow = tmpRow + 1;
                            //frameInfo.averagePress = totalPressure * 1.0 / totalNonZero;
                        }
                    }
                }

                //剔除孤立点
                for (int i = 0; i < backFrames.Count; i++)
                {
                    DelIsolated(ref backFrames[i].pressureValue);
                }
                for (int i = 0; i < allFrames.Count; i++)
                {
                    DelIsolated(ref allFrames[i].pressureValue);
                }

                for (int i = 0; i < allFrames.Count; i++)
                {
                    for (int j = 0; j < backFrames.Count - 1; j++)
                    {
                        if (i > backFrames[j].frameIndex && i <= backFrames[j + 1].frameIndex)
                            allFrames[i].backIndex = j + 1;
                        else if (i < backFrames[0].frameIndex)
                            allFrames[i].backIndex = 0;
                        else if (i > backFrames[backFrames.Count - 1].frameIndex)
                            allFrames[i].backIndex = backFrames.Count - 1;
                    }
                }

                //计算背景边缘
                for (int i = 0; i < backFrames.Count; i++)
                {
                    FrameInfo finfo = backFrames[i];
                    //finfo.CalBound();
                    CalBackBound(ref finfo);
                }
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.Message);
            }
        }