不包括标题行的Excel列数据types

使用电子表格devise,有没有什么办法可以获得列的“可能的”数据types,不包括标题行(如果存在的话)和合理容忍稀疏的人口,而不必自己做一个样本…有没有办法去做这个?

所以例如,如果我有一个像excel行

| Customers | Sales Item | Sale Date | Contact | Quantity | | IBM | Keyboard | 28-10-2011 | | 2 | | MS | Mouse | 27-09-2011 | joe | 5 | 

我期望看到

string,string,date时间,string,数字

编辑

所以我最终不得不像@Tim Anderson所build议的那样进行采样,但是我需要处理稀疏数据的情况,并且在col中冲突的types时默认为string。 (这是在一个循环遍历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; } } } 

我相信这可以进一步改善,所以请随时张贴改善,但这是我现在需要的。

SpreadsheetGear中不存在辅助方法或其他API来自动返回一列值的“可能的数据types”。 要实现这样的事情并不是很困难,以满足您自己的特定需求,但是如果不对数据进行“抽样”,则无法做到这一点。 下面是一个非常简单的方法,它接受范围来检查和一个布尔值,指示范围是否包含标题行。 它所做的只是检查第一行数据来确定types; 你可能想要build立一些更强大的东西:

 private SpreadsheetGear.ValueType[] GetColumnTypes(IRange range, bool hasHeader) { SpreadsheetGear.ValueType[] columnTypes = new SpreadsheetGear.ValueType[range.ColumnCount]; for (int i = 0; i < range.ColumnCount; i++) { columnTypes[i] = range[hasHeader ? 1 : 0, i].ValueType; } return columnTypes; } 

但是,您应该注意的一件事是,SpreadsheetGear使用与Excel相同的基本内部数据types,并在检查IRange.ValueType(包括Empty,Error,Logical,Number,Text)时返回这些types。 注意没有DateTime。 在您的示例中,这会影响“销售date”列中返回的值types,因为date/时间实际上是作为表示date/时间序列号的双精度存储在Excel和SpreadsheetGear中的。 所以这种types的值将返回数字,而不是像DateTime的东西。 它们在单元格中显示为“date”的事实仅仅是单元格的NumberFormat函数。

在从不使用电子表格,但在Excel中,我使用这个UDF

 Function GetType(rg As Range) As String If IsNumeric(rg.Value) Then GetType = "Numeric" ElseIf IsDate(rg.Value) Then GetType = "Date Time" Else GetType = "String" End If End Function 

我相信这是可以适应的

[]的

这里是另一个基于error handling程序和VBAtypes转换的尝试:

 Function probableType(vInput As Variant) Dim vResult As Variant 'set error handler to resume (the procedure will check the error number) On Error Resume Next 'check if it is an integer vResult = CInt(vInput) If Err.Number = 0 Then probableType = "Integer" Exit Function End If Err.Clear 'check if it is a date vResult = CDate(vInput) If Err.Number = 0 Then probableType = "Date" Exit Function End If Err.Clear 'else this is probably a string probableType = "String" End Function 

可以用这个子testing:

 Sub uniTest() MsgBox probableType("12/12/12") MsgBox probableType("12") MsgBox probableType("myTest") End Sub 

您可以将其与Excel VBA的所有转换函数进行概括( 请参阅ozgrid上的此链接 )