Exemplo n.º 1
0
        public static List <string[]> ParseClipboardData()
        {
            List <string[]> clipboardData = new List <string[]>();

            // get the data and set the parsing method based on the format
            // currently works with CSV and Text DataFormats
            IDataObject dataObj = System.Windows.Clipboard.GetDataObject();

            if (dataObj != null)
            {
                string[] formats = dataObj.GetFormats();
                if (formats.Contains(DataFormats.CommaSeparatedValue))
                {
                    string clipboardString = (string)dataObj.GetData(DataFormats.CommaSeparatedValue);
                    {
                        // EO: Subject to error when a CRLF is included as part of the data but it work for the moment and I will let it like it is
                        // WARNING ! Subject to errors
                        string[] lines = clipboardString.Split(new string[] { "\r\n" }, StringSplitOptions.None);

                        string[] lineValues;
                        foreach (string line in lines)
                        {
                            lineValues = CsvHelper.ParseLineCommaSeparated(line);
                            if (lineValues != null)
                            {
                                clipboardData.Add(lineValues);
                            }
                        }
                    }
                }
                else if (formats.Contains(DataFormats.Text))
                {
                    string clipboardString = (string)dataObj.GetData(DataFormats.Text);
                    clipboardData = CsvHelper.ParseText(clipboardString);
                }
            }

            return(clipboardData);
        }
Exemplo n.º 2
0
        // ******************************************************************
        public static LineSeparator GuessCsvSeparator(string oneLine)
        {
            List <Tuple <LineSeparator, int> > listOfLineSeparatorAndThereFirstLineSeparatedValueCount = new List <Tuple <LineSeparator, int> >();

            listOfLineSeparatorAndThereFirstLineSeparatedValueCount.Add(new Tuple <LineSeparator, int>(LineSeparator.Tab, CsvHelper.ParseLineTabSeparated(oneLine).Count()));
            listOfLineSeparatorAndThereFirstLineSeparatedValueCount.Add(new Tuple <LineSeparator, int>(LineSeparator.Semicolon, CsvHelper.ParseLineSemicolonSeparated(oneLine).Count()));
            listOfLineSeparatorAndThereFirstLineSeparatedValueCount.Add(new Tuple <LineSeparator, int>(LineSeparator.Comma, CsvHelper.ParseLineCommaSeparated(oneLine).Count()));

            Tuple <LineSeparator, int> bestBet = listOfLineSeparatorAndThereFirstLineSeparatedValueCount.MaxBy((n) => n.Item2).Select(x => x).First();

            if (bestBet != null && bestBet.Item2 > 1)
            {
                return(bestBet.Item1);
            }

            return(LineSeparator.Unknown);
        }