ExtensionsDataTable.cs
1 using Microsoft.VisualBasic.FileIO;
3 using System;
4 using System.Collections.Generic;
5 using System.Data;
6 using System.Linq;
7 using System.Text;
8 using System.Threading.Tasks;
9 
10 namespace Sage.ES.S50.Extensions
11 {
12  public static class ExtensionsDataTable
13  {
14  public static void ImportCSV(this DataTable dataTable, string csvFile, string separator = ";", bool hasFieldsEnclosedInQuotes = true, bool primeraFilaDatos = true)
15  {
16  try
17  {
18  bool assignedTypes = false;
19  using (TextFieldParser csvReader = new TextFieldParser(csvFile))
20  {
21  csvReader.SetDelimiters(separator);
22  csvReader.HasFieldsEnclosedInQuotes = hasFieldsEnclosedInQuotes;
23  string[] colFields = csvReader.ReadFields();
24  foreach (string column in colFields)
25  {
26  DataColumn dataColumn = new DataColumn(column)
27  {
28  AllowDBNull = true
29  };
30  dataTable.Columns.Add(dataColumn);
31  }
32  while (!csvReader.EndOfData)
33  {
34  string[] fieldData = csvReader.ReadFields();
35  List<object> fieldList = new List<object>();
36  for (int i = 0; i <= fieldData.Length - 1; i++)
37  {
38  //dataTable.Columns[i].DataType = Type(int)
39  if (int.TryParse(fieldData[i].ToString(), out int resultInt))
40  {
41  fieldList.Add(resultInt);
42  if (!assignedTypes)
43  {
44  dataTable.Columns[i].DataType = typeof(int);
45  }
46  }
47  else if (DateTime.TryParse(fieldData[i].ToString(), out DateTime resultDateTime))
48  {
49  fieldList.Add(resultDateTime);
50  if (!assignedTypes)
51  {
52  dataTable.Columns[i].DataType = typeof(DateTime);
53  }
54  }
55  else if (fieldData[i].ToString() == "")
56  {
57  fieldList.Add(null);
58  }
59  else
60  {
61  fieldList.Add(fieldData[i]);
62  }
63  }
64 
65  dataTable.Rows.Add(fieldList.ToArray());
66  assignedTypes = true;
67 
68  }
69  //foreach (var line in csv.Split(new[] { Environment.NewLine }, StringSplitOptions.None))
70  //{
71  // if (!readHeader)
72  // {
73  // foreach (var c in line.Split(separator))
74  // dataTable.Columns.Add(c);
75  // }
76  // else
77  // {
78  // dataTable.Rows.Add(line.Split(separator));
79  // }
80  //}
81  }
82  }
83  catch (Exception)
84  {
85 
86  }
87  }
88 
89  public static void ImportCSV_Idatos(this DataTable dataTable, string csvFile, List<CargarDatos.stInfoFields> InfoFields, string separator = ";", bool hasFieldsEnclosedInQuotes = true, bool primeraFilaDatos = true)
90  {
91  try
92  {
93  using (TextFieldParser csvReader = new TextFieldParser(csvFile))
94  {
95  csvReader.SetDelimiters(separator);
96  csvReader.HasFieldsEnclosedInQuotes = hasFieldsEnclosedInQuotes;
97  string[] colFields = csvReader.ReadFields();
98  if (primeraFilaDatos)
99  {
100  foreach (string column in colFields)
101  {
102  DataColumn dataColumn = new DataColumn(column)
103  {
104  AllowDBNull = true
105  };
106  dataTable.Columns.Add(dataColumn);
107  }
108  }
109  else
110  {
111  string abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
112 
113  for (int j = 0; j < colFields.Length; j++)
114  {
115  DataColumn dataColumn = new DataColumn(abc)
116  {
117  AllowDBNull = true
118  };
119  dataTable.Columns.Add(abc.Substring(j, 1));
120  }
121  }
122 
123  while (!csvReader.EndOfData)
124  {
125  string[] fieldData = (primeraFilaDatos ? csvReader.ReadFields() : colFields);
126  primeraFilaDatos = true;
127 
128  List<object> fieldList = new List<object>();
129 
130  for (int i = 0; i <= fieldData.Length - 1; i++)
131  {
132  string valor = InfoFields[i].tipoColumna;
133  switch (valor)
134  {
135  case "entero":
136  fieldList.Add(fieldData[i]);
137  dataTable.Columns[i].DataType = typeof(Int32);
138  break;
139  case "numerico":
140  fieldList.Add(fieldData[i]);
141  dataTable.Columns[i].DataType = typeof(double);
142  break;
143  case "logico":
144  if (string.IsNullOrEmpty(fieldData[i]))
145  {
146  fieldList.Add(false);
147  }
148  else
149  {
150  fieldList.Add(fieldData[i]);
151  }
152  dataTable.Columns[i].DataType = typeof(bool);
153  break;
154  case "fecha":
155  fieldList.Add(fieldData[i]);
156  dataTable.Columns[i].DataType = typeof(DateTime);
157  break;
158  default:
159  if (fieldData[i].ToString() == "")
160  {
161  fieldList.Add(null);
162  }
163  else
164  {
165  fieldList.Add(fieldData[i]);
166  }
167  break;
168  }
169  }
170 
171  dataTable.Rows.Add(fieldList.ToArray());
172  }
173  }
174  }
175  catch (Exception)
176  {
177 
178  }
179 
180  }
181  }
182 }