C#Excel编程-始终重新确认范围内的指定数据类型
我正在使用MicrosoftExcel类作为我正在编写的程序的一部分。我试图向Excel单元格中添加十进制值(item.Amount);但是,我收到以下错误:C#Excel编程-始终重新确认范围内的指定数据类型,c#,excel,C#,Excel,我正在使用MicrosoftExcel类作为我正在编写的程序的一部分。我试图向Excel单元格中添加十进制值(item.Amount);但是,我收到以下错误: + $exception {"Operator '+=' cannot be applied to operands of type 'double' and 'decimal'"} System.Exception {Microsoft.CSharp.RuntimeBinder.RuntimeBinderExceptio
+ $exception {"Operator '+=' cannot be applied to operands of type 'double' and 'decimal'"} System.Exception {Microsoft.CSharp.RuntimeBinder.RuntimeBinderException}
我理解所说的内容,但我不知道如何强制程序将Excel数据视为十进制数据而不是双精度数据。
下面这行给我带来了麻烦:
foreach (SalesItem item in store.salesPLUItems)
{
this.sheet.Range[item.colNumber + row].Value2 += item.amount;
if (item is CreditSales && item.colNumber != "")
{
this.sheet.Range[((CreditSales)item).countCol + row].Value += ((CreditSales)item).itemCount;
}
}
我尝试了以下添加:
// ensure Excel sees value as decimal
this.sheet.Range[item.colNumber + row].Value = Convert.ToDecimal(this.sheet.Range[item.colNumber + row].Value);
this.sheet.Range[item.colNumber + row].Value2 += item.amount;
没有成功。以下两项都不会编译
Convert.ToDecimal( this.sheet.Range[item.colNumber + row].Value) += item.amount;
(this.sheet.Range[item.colNumber + row].Value as Decimal) += item.amount;
我试着投下一个,相当于双倍。然而,我仍然收到错误,只是在不同的一行;角色正好相反-Excel范围中的值被视为十进制,item.amount被视为双精度
谢谢你的建议。试过两个角色吗?对不起,我不太清楚你的意思。如果您的意思是同时铸造Range.value和item.amount,我不确定这是否有帮助。item.amount已经是一个小数(最终的值实际上应该是一个小数),我只需要它将Excel单元格视为一个小数即可。还是我误解了?谢谢。我的意思是你们应该试着用右手选角,但当我现在读到我自己的评论时,我也觉得很模棱两可,很抱歉。我的意思是,你试过铸造物品吗。数量加倍?…+=(双倍)项目金额;我可以通过使用+而不是+=来修复它,并在右侧显式地强制转换Range.value。不确定这是否正是你的意思,但不管怎样,你的评论都明确地将我指向了决议。谢谢