虽然我仍然想知道为什么Interop在数据表中使用Excel公式时引发异常,
通过输入Excel.Range.CopyFromRecordSet,我找到了一个可能的答案。像这样:
ADODB.Recordset BridgeCalcRecordset = ConvertToRecordset(dt);
insertBridgeCalcTableRange.CopyFromRecordset(BridgeCalcRecordset);
我用以下方法解决了这个问题:
insertBridgeCalcTableRange.Formula = insertBridgeCalcTableRange.Value;
我的代码是相同的,但我正在使用此代码将我的数据集转换为ADODB记录集(他们说这样也更快):
网络明星在
http://www.nullskull.com/q/10057748/hi-all.aspx
static public ADODB.Recordset ConvertToRecordset(DataTable inTable)
{
ADODB.Recordset result = new ADODB.Recordset();
result.CursorLocation = ADODB.CursorLocationEnum.adUseClient;
ADODB.Fields resultFields = result.Fields;
System.Data.DataColumnCollection inColumns = inTable.Columns;
foreach (DataColumn inColumn in inColumns)
{
resultFields.Append(inColumn.ColumnName
, TranslateType(inColumn.DataType)
, inColumn.MaxLength
, inColumn.AllowDBNull ? ADODB.FieldAttributeEnum.adFldIsNullable :
ADODB.FieldAttributeEnum.adFldUnspecified
, null);
}
result.Open(System.Reflection.Missing.Value
, System.Reflection.Missing.Value
, ADODB.CursorTypeEnum.adOpenStatic
, ADODB.LockTypeEnum.adLockOptimistic, 0);
foreach (DataRow dr in inTable.Rows)
{
result.AddNew(System.Reflection.Missing.Value,
System.Reflection.Missing.Value);
for (int columnIndex = 0; columnIndex < inColumns.Count; columnIndex++)
{
resultFields[columnIndex].Value = dr[columnIndex];
}
}
return result;
}
static ADODB.DataTypeEnum TranslateType(Type columnType)
{
switch (columnType.UnderlyingSystemType.ToString())
{
case "System.Boolean":
return ADODB.DataTypeEnum.adBoolean;
case "System.Byte":
return ADODB.DataTypeEnum.adUnsignedTinyInt;
case "System.Char":
return ADODB.DataTypeEnum.adChar;
case "System.DateTime":
return ADODB.DataTypeEnum.adDate;
case "System.Decimal":
return ADODB.DataTypeEnum.adCurrency;
case "System.Double":
return ADODB.DataTypeEnum.adDouble;
case "System.Int16":
return ADODB.DataTypeEnum.adSmallInt;
case "System.Int32":
return ADODB.DataTypeEnum.adInteger;
case "System.Int64":
return ADODB.DataTypeEnum.adBigInt;
case "System.SByte":
return ADODB.DataTypeEnum.adTinyInt;
case "System.Single":
return ADODB.DataTypeEnum.adSingle;
case "System.UInt16":
return ADODB.DataTypeEnum.adUnsignedSmallInt;
case "System.UInt32":
return ADODB.DataTypeEnum.adUnsignedInt;
case "System.UInt64":
return ADODB.DataTypeEnum.adUnsignedBigInt;
case "System.String":
default:
return ADODB.DataTypeEnum.adVarChar;
}
}