forked from ExcelDataReader/ExcelDataReader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatasetHelpers.cs
More file actions
92 lines (83 loc) · 2.89 KB
/
Copy pathDatasetHelpers.cs
File metadata and controls
92 lines (83 loc) · 2.89 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.Collections.Generic;
using System.Data;
namespace ExcelDataReader.Desktop
{
/// <summary>
/// Helpers class
/// </summary>
internal static class DatasetHelpers
{
internal static void FixDataTypes(DataSet dataset)
{
var tables = new List<DataTable>(dataset.Tables.Count);
bool convert = false;
foreach (DataTable table in dataset.Tables)
{
if ( table.Rows.Count == 0)
{
tables.Add(table);
continue;
}
DataTable newTable = null;
for (int i = 0; i < table.Columns.Count; i++)
{
Type type = null;
foreach (DataRow row in table.Rows)
{
if (row.IsNull(i))
continue;
var curType = row[i].GetType();
if (curType != type)
{
if (type == null)
type = curType;
else
{
type = null;
break;
}
}
}
if (type != null)
{
convert = true;
if (newTable == null)
newTable = table.Clone();
newTable.Columns[i].DataType = type;
}
}
if (newTable != null)
{
newTable.BeginLoadData();
foreach (DataRow row in table.Rows)
{
newTable.ImportRow(row);
}
newTable.EndLoadData();
tables.Add(newTable);
}
else tables.Add(table);
}
if (convert)
{
dataset.Tables.Clear();
dataset.Tables.AddRange(tables.ToArray());
}
}
//public static void AddColumn(DataTable table, string columnName)
//{
// //if a colum already exists with the name append _i to the duplicates
// var adjustedColumnName = columnName;
// var column = table.Columns[columnName];
// var i = 1;
// while (column != null)
// {
// adjustedColumnName = string.Format("{0}_{1}", columnName, i);
// column = table.Columns[adjustedColumnName];
// i++;
// }
// table.Columns.Add(adjustedColumnName, typeof(Object));
//}
}
}