blob: 70526436b5690196f08b964bdd00b054e99d24ca (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Drawing;
using Google.Protobuf;
using Tango.BL.Entities;
using Tango.BL.Enumerations;
using Tango.PMR.Stubs;
using Tango.PMR.Diagnostics;
using Tango.FSE.Common.Connection;
using Tango.FSE.Common.Diagnostics;
using Tango.FSE.Procedures;
namespace Tango.FSE.Procedures.Examples.Csv
{
#region Example
//CSV Model must contain properties, not plain fields.
public class Person
{
public String Name { get; set; }
public int Age { get; set; }
}
public class Program
{
//This example demonstrates CSV file writing and reading.
//The csv reading method also accepts a byte array so you can also read a CSV file from resource.
public void OnExecute(IProcedureContext context)
{
//Initialize a list of person model (see Person.csx).
List<Person> persons = new List<Person>();
for (int i = 0; i < 100; i++)
{
Person p = new Person();
p.Name = "Person " + i;
p.Age = i;
persons.Add(p);
}
//Request the user to choose the csv file location.
String csvFile = context.RequestFileSave("Select CSV File Location", "*.csv", "persons.csv");
if (csvFile != null) //Check if user selected a file and pressed 'OK'.
{
//Write the persons list to the selected csv file.
context.WriteCsv<Person>(csvFile, persons);
//Test csv file for reading...
persons = context.ReadCsv<Person>(csvFile);
//Write the reading result to the output window.
context.WriteLine(persons);
}
}
}
#endregion
}
|