Beispiel #1
0
    public CsvParser(string raw)
    {
        originalRaw = raw;
        //Debug.Log(raw);

        string[] rawLines = raw.Split(new char[] { SPREAD_LINE_BREAK }, System.StringSplitOptions.RemoveEmptyEntries);
        for (int i = HEADER_SKIP_LINE_COUNT; i < rawLines.Length; i++)
        {
            CsvLine line = new CsvLine();
            line.cell = new List <string>();

            string[] split = cellsSeparator(rawLines[i]);

            int cntCellWithContent = 0;
            for (int j = 0; j < split.Length; j++)
            {
                if (split[j].Length > 0)
                {
                    //Debug.Log(split[j] + " (" + split[j].Length + ")");
                    cntCellWithContent++;
                }
                line.cell.Add(split[j]);
            }

            //skip line of only empty cells
            if (cntCellWithContent > 1)
            {
                lines.Add(line);
            }
        }

        Debug.Log("csv solved x" + lines.Count);
    }
        public static IList <string[]> GetCsvCacheFile(string fileName)
        {
            string key = fileName;

            if (!FileParser.__csvFilesCached.ContainsKey(key))
            {
                lock (FileParser.__csvFilesCached)
                {
                    if (!FileParser.__csvFilesCached.ContainsKey(key))
                    {
                        IList <string[]> strArrayList = (IList <string[]>) new List <string[]>();
                        using (Stream stream = FileParser.GetStream(fileName))
                        {
                            if (stream.CanSeek)
                            {
                                stream.Seek(0L, SeekOrigin.Begin);
                            }
                            using (StreamReader streamReader = new StreamReader(stream))
                            {
                                while (!streamReader.EndOfStream)
                                {
                                    string line = streamReader.ReadLine();
                                    if (!string.IsNullOrWhiteSpace(line))
                                    {
                                        strArrayList.Add(CsvLine.Split(line).ToArray <string>());
                                    }
                                }
                            }
                        }
                        FileParser.__csvFilesCached.Add(key, strArrayList);
                    }
                }
            }
            return(FileParser.__csvFilesCached[key]);
        }
Beispiel #3
0
 public CsvDataReader(TextReader reader, CsvLayout csvLayout, CsvBehaviour csvBehaviour, CultureInfo cultureInfo)
 {
     _parser     = new CsvParser(reader, csvLayout, csvBehaviour);
     _header     = _parser.Header;
     _line       = null;
     _converter  = new Converter(cultureInfo ?? CultureInfo.InvariantCulture);
     _enumerator = _parser.GetEnumerator();
     _schema     = csvLayout.Schema;
 }
 public void QueueWriteObj(CsvLine csvLine)
 {
     //this.Db.Hats.InsertOnSubmit(new Pocketailor.Model.Conversions.Hat()
     //{
     //    Retailer = csvLine.Retailer,
     //    Region = csvLine.Region,
     //    Head = csvLine.GetMeasurementOrNull(MeasurementId.Head),
     //    SizeLetter = csvLine.SizeLetter,
     //    SizeNumber = csvLine.SizeNumber,
     //});
 }
Beispiel #5
0
        /// <summary>
        /// Fills the buffer with data from the reader.
        /// </summary>
        /// <returns><see langword="true"/> if data was successfully read; otherwise, <see langword="false"/>.</returns>
        /// <exception cref="ObjectDisposedException">
        ///	The instance has been disposed of.
        /// </exception>
        private bool ReadLine()
        {
            if (EndOfStream || !_enumerator.MoveNext())
            {
                EndOfStream = true;
                return(false);
            }

            _line = _enumerator.Current;
            return(true);
        }
Beispiel #6
0
 public void QueueWriteObj(CsvLine csvLine)
 {
     //this.Db.Bras.InsertOnSubmit(new Pocketailor.Model.Conversions.Bra()
     //{
     //    Retailer = csvLine.Retailer,
     //    Region = csvLine.Region,
     //    Chest = csvLine.GetMeasurementOrNull(MeasurementId.Chest),
     //    UnderBust = csvLine.GetMeasurementOrNull(MeasurementId.UnderBust),
     //    SizeLetter = csvLine.SizeLetter,
     //    SizeNumber = csvLine.SizeNumber,
     //});
 }
Beispiel #7
0
 public void QueueWriteObj(CsvLine csvLine)
 {
     //this.Db.Shoes.InsertOnSubmit(new Shoes()
     //{
     //    Retailer = csvLine.Retailer,
     //    Region = csvLine.Region,
     //    Gender = csvLine.Gender,
     //    FootLength = csvLine.GetMeasurementOrNull(MeasurementId.FootLength),
     //    FootWidth = csvLine.GetMeasurementOrNull(MeasurementId.FootWidth),
     //    SizeLetter = csvLine.SizeLetter,
     //    SizeNumber = csvLine.SizeNumber,
     //});
 }
Beispiel #8
0
 public void QueueWriteObj(CsvLine csvLine)
 {
     //this.Db.Hosiery.InsertOnSubmit(new Hosiery()
     //{
     //    Retailer = csvLine.Retailer,
     //    Region = csvLine.Region,
     //    Waist = csvLine.GetMeasurementOrNull(MeasurementId.Waist),
     //    Hips = csvLine.GetMeasurementOrNull(MeasurementId.Hips),
     //    InsideLeg = csvLine.GetMeasurementOrNull(MeasurementId.InsideLeg),
     //    SizeLetter = csvLine.SizeLetter,
     //    SizeNumber = csvLine.SizeNumber,
     //});
 }
Beispiel #9
0
 public void QueueWriteObj(CsvLine csvLine)
 {
     //this.Db.DressSizes.InsertOnSubmit(new Pocketailor.Model.Conversions.DressSize()
     //{
     //    Retailer = csvLine.Retailer,
     //    Region = csvLine.Region,
     //    Chest = csvLine.GetMeasurementOrNull(MeasurementId.Chest),
     //    Waist = csvLine.GetMeasurementOrNull(MeasurementId.Waist),
     //    Hips = csvLine.GetMeasurementOrNull(MeasurementId.Hips),
     //    SizeLetter = csvLine.SizeLetter,
     //    SizeNumber = csvLine.SizeNumber,
     //});
 }
Beispiel #10
0
        public CsvReader(TextReader reader,
                         int bufferSize           = DefaultBufferSize,
                         CsvLayout layout         = null,
                         CsvBehaviour behaviour   = null,
                         string defaultHeaderName = null)
        {
            if (layout == null)
            {
                layout = CsvLayout.Default;
            }
            if (behaviour == null)
            {
                behaviour = CsvBehaviour.Default;
            }
            _line = CsvLine.Empty;

            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            if (bufferSize <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(bufferSize), bufferSize, ExceptionMessage.BufferSizeTooSmall);
            }

            BufferSize = bufferSize;

            if (reader is StreamReader streamReader)
            {
                Stream stream = streamReader.BaseStream;

                if (stream.CanSeek)
                {
                    // Handle bad implementations returning 0 or less
                    if (stream.Length > 0)
                    {
                        BufferSize = (int)Math.Min(bufferSize, stream.Length);
                    }
                }
            }

            CurrentRecordIndex = -1;

            _csvLayout  = layout;
            _behaviour  = behaviour;
            _parser     = new CsvParser(reader, _csvLayout, _behaviour, defaultHeaderName);
            _enumerator = _parser.GetEnumerator();
        }
 public void QueueWriteObj(CsvLine csvLine)
 {
     //this.Db.Wetsuits.InsertOnSubmit(new Wetsuit()
     //{
     //    Retailer = csvLine.Retailer,
     //    Region = csvLine.Region,
     //    Gender = csvLine.Gender,
     //    Height = csvLine.GetMeasurementOrNull(MeasurementId.Height),
     //    Chest = csvLine.GetMeasurementOrNull(MeasurementId.Chest),
     //    Waist = csvLine.GetMeasurementOrNull(MeasurementId.Waist),
     //    Hips = csvLine.GetMeasurementOrNull(MeasurementId.Hips),
     //    Weight = csvLine.GetMeasurementOrNull(MeasurementId.Weight),
     //    SizeLetter = csvLine.SizeLetter,
     //    SizeNumber = csvLine.SizeNumber,
     //});
 }
 public void QueueWriteObj(CsvLine csvLine)
 {
     //this.Db.Shirts.InsertOnSubmit(new Pocketailor.Model.Conversions.Shirt()
     //{
     //    Retailer = csvLine.Retailer,
     //    Region = csvLine.Region,
     //    Gender = csvLine.Gender,
     //    Chest = csvLine.GetMeasurementOrNull(MeasurementId.Chest),
     //    Waist = csvLine.GetMeasurementOrNull(MeasurementId.Waist),
     //    Neck = csvLine.GetMeasurementOrNull(MeasurementId.Neck),
     //    TorsoLength = csvLine.GetMeasurementOrNull(MeasurementId.TorsoLength),
     //    Sleeve = csvLine.GetMeasurementOrNull(MeasurementId.Sleeve),
     //    Hips = csvLine.GetMeasurementOrNull(MeasurementId.Hips),
     //    SizeLetter = csvLine.SizeLetter,
     //    SizeNumber = csvLine.SizeNumber,
     //});
 }
Beispiel #13
0
        /// <summary>
        /// Reads the next record.
        /// </summary>
        /// <param name="skipToNextLine">
        /// Indicates if the reader will skip directly to the next line without parsing the current one.
        /// To be used when an error occurs.
        /// </param>
        /// <returns><see langword="true"/> if a record has been successfully reads; otherwise, <see langword="false"/>.</returns>
        /// <exception cref="ObjectDisposedException">
        ///	The instance has been disposed of.
        /// </exception>
        private bool ReadNextRecord(bool skipToNextLine)
        {
            EnsureInitialize();

            _line = null;

            if (!ReadLine())
            {
                return(false);
            }

            Debug.Assert(_line != null, "_line != null");

            CurrentRecordIndex++;

            return(true);
        }
 public void QueueWriteObj(CsvLine csvLine)
 {
     //this.Db.Suits.InsertOnSubmit(new Suit()
     //{
     //    Retailer = csvLine.Retailer,
     //    Region = csvLine.Region,
     //    Gender = csvLine.Gender,
     //    Waist = csvLine.GetMeasurementOrNull(MeasurementId.Waist),
     //    Shoulder = csvLine.GetMeasurementOrNull(MeasurementId.Shoulder),
     //    Sleeve = csvLine.GetMeasurementOrNull(MeasurementId.Sleeve),
     //    Chest = csvLine.GetMeasurementOrNull(MeasurementId.Chest),
     //    InsideLeg = csvLine.GetMeasurementOrNull(MeasurementId.InsideLeg),
     //    Neck = csvLine.GetMeasurementOrNull(MeasurementId.Neck),
     //    TorsoLength = csvLine.GetMeasurementOrNull(MeasurementId.TorsoLength),
     //    Hips = csvLine.GetMeasurementOrNull(MeasurementId.Hips),
     //    SizeLetter = csvLine.SizeLetter,
     //    SizeNumber = csvLine.SizeNumber,
     //});
 }
Beispiel #15
0
    public ImportCsv(string FilePath, string separator = ",")
    //Get filepath and convert into list of strings
    {
        string CsvValues = "";

        if (!FilePath.EndsWith(".csv"))
        {
            FilePath += ".csv";
        }
        using (StreamReader CsvReader = new StreamReader(File.OpenRead(FilePath)))
        {
            CsvValues = CsvReader.ReadToEnd();
        }

        string[] CsvLines = CsvValues.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
        foreach (string CsvLine in CsvLines)
        {
            this.CsvList.Add(CsvLine.Split(separator.ToCharArray(), StringSplitOptions.RemoveEmptyEntries));
        }
    }
Beispiel #16
0
        static void Main(string[] args)
        {
            string CsvFilePath = string.Format("{0}{1}", AppDomain.CurrentDomain.BaseDirectory, "Csv.csv");
            var    CsvLines    = File.ReadAllLines(CsvFilePath);
            var    CsvGroups   = CsvLines.GroupBy(CsvLine => new
            {
                cols1 = CsvLine.Split(',')[0],
                cols2 = CsvLine.Split(',')[1],
            });

            List <DataGroup> DataList = new List <DataGroup>();

            foreach (var CsvGroup in CsvGroups)
            {
                List <Data> Datas = new List <Data>();

                foreach (var CsvLine in CsvGroup)
                {
                    string[] cols = CsvLine.Split(',');

                    Datas.Add(new Data()
                    {
                        DataCode = cols[0],
                        DataName = cols[1],
                        Colspan3 = cols[2],
                        Colspan4 = cols[3],
                        Colspan5 = cols[4],
                    });
                }

                DataList.Add(new DataGroup()
                {
                    GroupCode = CsvGroup.Key.cols1,
                    Datas     = Datas
                });
            }
            var    TicketJson   = JsonConvert.SerializeObject(DataList);
            string JsonFilePath = string.Format("{0}{1}", AppDomain.CurrentDomain.BaseDirectory, "Json.json");

            File.WriteAllText(JsonFilePath, TicketJson);
        }
Beispiel #17
0
            /// <summary>
            ///     Gets the next line.
            /// </summary>
            public CsvLine GetNextLine()
            {
                if (_text.EndOfStream || (char)_text.Peek() == '\0')
                {
                    return(null);
                }

                var isStringStarted = false;
                var csvLine         = new CsvLine();
                var curToken        = string.Empty;

                while (!_text.EndOfStream)
                {
                    var curChar = (char)_text.Read();
                    if (curChar == '\0')
                    {
                        break;
                    }

                    if (curChar == '"')
                    {
                        if (isStringStarted)
                        {
                            if (IsNextSymbolQuote())
                            {
                                curToken += '"';
                                //skip double quote
                                _text.Read();
                                continue;
                            }

                            isStringStarted = false;
                            continue;
                        }

                        if (string.IsNullOrEmpty(curToken))
                        {
                            isStringStarted = true;
                        }
                        else
                        {
                            curToken += '"';
                        }

                        continue;
                    }

                    if (!isStringStarted)
                    {
                        if (curChar == _delemiter)
                        {
                            csvLine.Add(curToken.Replace("\r", string.Empty).Replace("\n", Environment.NewLine));
                            curToken = string.Empty;
                            continue;
                        }

                        if (curChar == '\n')
                        {
                            csvLine.Add(curToken.Replace("\r", string.Empty).Replace("\n", Environment.NewLine));
                            return(csvLine);
                        }
                    }

                    curToken += curChar;
                }

                csvLine.Add(curToken);
                return(csvLine);
            }
Beispiel #18
0
        private void MappingFrm_Load(object sender, EventArgs e)
        {
            ShowWaitForm();

            dataGridView.Rows.Clear();

            DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();

            cmb.HeaderText   = "From Field";
            cmb.Name         = "cmb";
            cmb.MinimumWidth = 250;

            if (setup.type == Library.Type.SQL)
            {
                string connetionString = "Data Source=" + setup.server + ";Initial Catalog=" + setup.database + ";Integrated Security=true;";

                using (SqlConnection connection = new SqlConnection(connetionString))
                    using (SqlCommand command = connection.CreateCommand())
                    {
                        command.CommandText = "select c.name from sys.columns c inner join sys.tables t on t.object_id = c.object_id and t.name = '" + setup.sqlTable + "'";
                        connection.Open();
                        using (var reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                cmb.Items.Add(reader.GetString(0));
                            }
                        }
                    }
            }

            if (setup.type == Library.Type.CSV)
            {
                csvFieldList = new List <CsvField>();
                csvLines     = new List <CsvLine>();
                int j = 0;
                using (var reader = new StreamReader(setup.csvPath))
                {
                    List <string> csvfields = new List <string>();
                    bool          first     = true;
                    while (!reader.EndOfStream)
                    {
                        CsvLine csvLine = new CsvLine();
                        csvLine.csvFields = new List <CsvField>();
                        var      line   = reader.ReadLine();
                        string[] values = line.Split(';');
                        if (first)
                        {
                            csvfields = line.Split(';').ToList();
                            first     = false;
                        }
                        else
                        {
                            for (int i = 0; i < csvfields.Count; i++)
                            {
                                csvLine.csvFields.Add(new CsvField {
                                    key = csvfields[i], index = i, value = values[i]
                                });
                            }
                            csvLines.Add(csvLine);
                            _waitForm.updateLbl(j++);
                            _waitForm.Refresh();
                        }
                    }

                    foreach (string s in csvfields)
                    {
                        cmb.Items.Add(s);
                    }
                }
            }

            if (setup.type == Library.Type.Excel)
            {
                excelLines = new List <ExcelLine>();
                ExcelLine excelLine = new ExcelLine();

                Microsoft.Office.Interop.Excel.Application xlApp       = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel.Workbook    xlWorkbook  = xlApp.Workbooks.Open(setup.excelPath);
                Microsoft.Office.Interop.Excel._Worksheet  xlWorksheet = xlWorkbook.Sheets[1];
                Microsoft.Office.Interop.Excel.Range       xlRange     = xlWorksheet.UsedRange;

                int           rowCount = xlRange.Rows.Count;
                int           colCount = xlRange.Columns.Count;
                List <string> headers  = new List <string>();

                for (int i = 1; i <= rowCount; i++)
                {
                    for (int j = 1; j <= colCount; j++)
                    {
                        if (i == 1)
                        {
                            cmb.Items.Add(xlRange.Cells[i, j].Value2.ToString());
                            headers.Add(xlRange.Cells[i, j].Value2.ToString());
                        }
                        else
                        {
                            if (j == 1)
                            {
                                excelLine             = new ExcelLine();
                                excelLine.excelFields = new List <ExcelField>();
                            }

                            if (xlRange.Cells[i, j] != null && xlRange.Cells[i, j].Value2 != null)
                            {
                                excelLine.excelFields.Add(new ExcelField {
                                    key = headers[j - 1], index = i, value = xlRange.Cells[i, j].Value2.ToString()
                                });
                            }
                        }
                    }
                    if (i != 1)
                    {
                        excelLines.Add(excelLine);
                        _waitForm.updateLbl(i);
                        _waitForm.Refresh();
                    }
                }

                GC.Collect();
                GC.WaitForPendingFinalizers();

                Marshal.ReleaseComObject(xlRange);
                Marshal.ReleaseComObject(xlWorksheet);

                xlWorkbook.Close();
                Marshal.ReleaseComObject(xlWorkbook);

                xlApp.Quit();
                Marshal.ReleaseComObject(xlApp);
            }

            dataGridView.Columns.Add(cmb);

            try
            {
                var client = new DataToBCService.DataToBc();
                client.Url         = setup.webServiceUrl;
                client.Credentials = new NetworkCredential(setup.userName, setup.password);
                string        fields = client.GetFields(setup.function, setup.direct);
                List <string> result = fields.Split(',').ToList();
                foreach (string s in result)
                {
                    this.dataGridView.Rows.Add(s);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Not vaild setup");
                Close();
            }
        }