使用电子表格工具,是否有任何方法可以获得列的“可能”数据类型,不包括标题行(如果存在)以及合理容忍稀疏人口而不必自己做一个样本...有没有办法做到这一点?不包括标题行的Excel列数据类型
因此,例如,如果我有一个excel行像
| Customers | Sales Item | Sale Date | Contact | Quantity |
| IBM | Keyboard | 28-10-2011 | | 2 |
| MS | Mouse | 27-09-2011 | joe | 5 |
我希望看到
字符串,字符串,日期时间,字符串,数字
编辑
所以我最终不得不像@Tim Anderson所建议的那样进行采样,但我需要处理稀疏数据的情况,并且默认t o当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;
}
}
}
我相信这可以进一步改善,所以请随时发布改进,但这是我现在需要的。
谢谢@Tim Anderson。事实上,我最终做了类似的事情,尽管我还需要对一些行进行抽样以处理稀疏数据。如果有人需要做类似的事情,我会编辑我的文章。 –