使用电子表格,有什么方法可以获取列的“可能”数据类型,不包括标题行(如果存在),并且可以合理地容忍稀疏人口,而不必自己做样本......是否已经有办法了去做这个?
所以例如,如果我有一个像
| Customers | Sales Item | Sale Date | Contact | Quantity |
| IBM | Keyboard | 28-10-2011 | | 2 |
| MS | Mouse | 27-09-2011 | joe | 5 |
我希望看到
字符串、字符串、日期时间、字符串、数字
编辑
所以我最终不得不像@Tim Anderson 建议的那样进行采样,但是我需要处理稀疏数据的情况,并且当 col 中的类型冲突时默认为字符串。(这在遍历 cols 的循环中调用,我不能发布它,因为它包含一些 IP)DataValueType 只是一个本地枚举,rowcount 是要采样的行数,因为我已经在采样,所以我只是忽略第 0 行如果它是标题行。
private DataType GetDataTypeFromColRange(IRange range, int rowcount, int col)
{
var dtlist = GetValueTypes(range, rowcount, col).Distinct();
// If conflicting types for the col default to string.
if (dtlist.Count() != 1)
{
return new DataType(DataTypeValue.String);
}
else
{
return new DataType(dtlist.First());
}
}
private IEnumerable<DataTypeValue> GetValueTypes(IRange range, int rowcount, int col)
{
for (int i = 1; i < rowcount; i++)
{
switch (range[i, col].ValueType)
{
case SpreadsheetGear.ValueType.Text:
yield return DataTypeValue.String;
break;
case SpreadsheetGear.ValueType.Number:
if (range[i, col].NumberFormatType == NumberFormatType.Date || range[i, col].NumberFormatType == NumberFormatType.DateTime)
{
yield return DataTypeValue.Date;
}
else
{
yield return DataTypeValue.Numeric;
}
break;
case SpreadsheetGear.ValueType.Logical:
yield return DataTypeValue.Bool;
break;
default: // ignore empty or errored cells.
continue;
}
}
}
我相信这可以进一步改进,所以请随时发布改进,但这正是我现在需要的。