Esempio n. 1
0
        public void CSVReadAndWriteTest()
        {
            //Support List<> data type.
            Debug.Log("Auto generate data list:");
            foreach (var data in CSVConverter.ConvertColumn <List <float> >(
                         new CSVTableWriter()
                         .AddHeader(new CSVRecordWriter {
                "Item1"
            })
                         .AddHeader(new CSVRecordWriter {
                "List<float>"
            })
                         .AddRecord(new CSVRecordWriter {
                "1.25|3.33|2.5|4"
            })
                         .AddRecord(new CSVRecordWriter {
                "2.5|4|5.1"
            })
                         .GetEncodeTable()))
            {
                Debug.Log(string.Join("|", data));
            }

            //Using ValueTuple as the data type.
            Debug.Log("Auto generate data list:");
            foreach (var data in CSVConverter.Convert <ValueTuple <float, string> >(
                         new CSVTableWriter()
                         .AddHeader(new CSVRecordWriter {
                "Item1", "Item2"
            })
                         .AddHeader(new CSVRecordWriter {
                "float", "string"
            })
                         .AddRecord(new CSVRecordWriter {
                "1.25", "first"
            })
                         .AddRecord(new CSVRecordWriter {
                "2.5", "second"
            })
                         .GetEncodeTable()))
            {
                Debug.Log(data);
            }

            //Custom cell separator.
            CSVTableWriter csvTableWriter = CSVGenerator.Class2CSVTable <ExampleTestData>(';');

            Debug.Log("Auto generate csv:\n" + csvTableWriter.GetEncodeTable(NewLineStyle.NonUnix));

            string classStr = CSVGenerator.CSV2Class(csvTableWriter.GetEncodeTable(NewLineStyle.NonUnix), null, nameof(ExampleTestData), csvTableWriter.CellSeparator);

            Debug.Log("Auto generate class:\n" + classStr);

            csvTableWriter.AddRecord(new CSVRecordWriter()
                                     .Add("1")
                                     .Add("#cccccc")
                                     .Add("2")
                                     .Add("normal string")
                                     .Add("\"string with double quote")
                                     .Add("1;2;3|4;5;6")
                                     .Add("#cccccc;string content")
                                     .Add("#cccccc;string content|#ffffff;second string")
                                     .Add("#cccccc;string content|#ffffff;second string"));
            csvTableWriter.AddRecord(new CSVRecordWriter()
                                     .Add("3")
                                     .Add("#dddddd")
                                     .Add("4")
                                     .Add("string with, comma")
                                     .Add("\"string with\", comma and \"double quote")
                                     .Add("7;8;9|10;11;12|7;7;7")
                                     .Add("#dddddd;string content2")
                                     .Add("#dddddd;string content2|#eeeeee;second string2")
                                     .Add("#dddddd;string content2|#eeeeee;second string2"));
            Debug.Log("csv add data:\n" + csvTableWriter.GetEncodeTable(NewLineStyle.NonUnix));

            var dataList = CSVConverter.Convert <ExampleTestData>(csvTableWriter.GetEncodeTable(NewLineStyle.NonUnix), csvTableWriter.CellSeparator);

            Debug.Log("Auto generate data list:");
            foreach (var data in dataList)
            {
                Debug.Log(data);
            }

            Debug.Log("Auto generate data IEnumerable:");
            foreach (var data in CSVConverter.ConvertEnumerator <ExampleTestData>(csvTableWriter.GetEncodeTable(NewLineStyle.NonUnix), csvTableWriter.CellSeparator))
            {
                Debug.Log(data);
            }

            string newContent = new CSVTableWriter()
                                .AddHeader(new CSVRecordWriter {
                "Name", "Age"
            })
                                .AddHeader(new CSVRecordWriter {
                "string", "int"
            })
                                .AddRecord(new CSVRecordWriter {
                "Name1", "10"
            })
                                .AddRecord(new CSVRecordWriter {
                "Name2", "20"
            })
                                .AddRecord(new CSVRecordWriter {
                "Name3", "30"
            })
                                .AddRecord(new CSVRecordWriter {
                "Name4", "40"
            })
                                .GetEncodeTable(NewLineStyle.NonUnix);

            Debug.Log("Auto generate column data by header name:");
            foreach (var name in CSVConverter.ConvertColumn <string>(newContent, "Name"))
            {
                Debug.Log(name);
            }

            Debug.Log("Auto generate column data by header index:");
            foreach (var age in CSVConverter.ConvertColumn <int>(newContent, 1))
            {
                Debug.Log(age);
            }
        }
Esempio n. 2
0
        public static void Main(string[] args)
        {
            //Write.
            //Create a empty csv writer.
            CSVTableWriter csvTableWriter = new CSVTableWriter()
                                            //Add headers.
                                            .AddHeader("Data1")
                                            .AddHeader("Data2")
                                            .AddHeader("Data3")
                                            .AddHeader("Data4")
                                            .AddHeader("Data5")
                                            .AddHeader("Data6")
                                            //Add descriptions.
                                            .AddDescription("int")
                                            .AddDescription("Color")
                                            .AddDescription("int")
                                            .AddDescription("string")
                                            .AddDescription("string")
                                            .AddDescription("int[]")
                                            //Add records.
                                            .AddRecord(new CSVRecordWriter()
                                                       .AddCell("1")
                                                       .AddCell("#aaaaaa")
                                                       .AddCell("2")
                                                       .AddCell("normal string")
                                                       .AddCell("\"string with double quote")
                                                       .AddCell("1;2;3|4;5;6")
                                                       )
                                            .AddRecord(new CSVRecordWriter()
                                                       .AddCell("2")
                                                       .AddCell("#bbbbbb")
                                                       .AddCell("4")
                                                       .AddCell("string with, comma")
                                                       .AddCell("string\" with\", comma and \"double quote")
                                                       .AddCell("7;8;9|10;11;12|7;7;7"))
                                            .AddRecord(new CSVRecordWriter()
                                                       .AddCell("3")
                                                       .AddCell("#ccc")
                                                       .AddCell("5")
                                                       .AddCell("string with \n \\n and \r\n \\r\\n")
                                                       .AddCell("string\" with \n \\n and \", comma and \"double quote and \r\n \\r\\n")
                                                       .AddCell("7;8;9|10;11;12|7;7;7"));
            //Get csv form string.
            string csv1 = csvTableWriter.GetEncodeTable();

            Console.WriteLine("Write to csv form:\n" + csv1);
            using (StreamWriter streamWriter = new StreamWriter(Path.Combine(Environment.CurrentDirectory, "CSVExample1.csv")))
                streamWriter.Write(csv1);

            //Read
            //Create csv reader form csv content.
            CSVTableReader csvTableReader = new CSVTableReader(csv1);

            Console.WriteLine("Read from csv form:\n" + csvTableReader);

            //Write
            //Create csv writer from csv reader, custom cell separator.
            CSVTableWriter csvTableWriter2 = new CSVTableWriter(csvTableReader, ';')
                                             //Remove some row and column.
                                             .RemoveHeader(5)
                                             .RemoveDescription(5)
                                             .RemoveRecord(0);

            foreach (var record in csvTableWriter2.Records)
            {
                record.RemoveCell(5);
            }
            //Add new record. AddRecord method will set CSVRecordWriter.CellSeparator.
            csvTableWriter2.AddRecord(new CSVRecordWriter()
                                      .AddCell("100")
                                      .AddCell("#cccccc")
                                      .AddCell("5")
                                      .AddCell("string with; semicolon")
                                      .AddCell("string\" with\"; semicolon and \"double quote"));
            //Get csv form string, choose new line style.
            string csv2 = csvTableWriter2.GetEncodeTable(NewLineStyle.NonUnix);

            Console.WriteLine("Write to csv form:\n" + csv2);
            using (StreamWriter streamWriter = new StreamWriter(Path.Combine(Environment.CurrentDirectory, "CSVExample2.csv")))
                streamWriter.Write(csv2);

            Console.ReadKey();
        }