简体   繁体   English

来自HRESULT的异常:0x800A03EC,通过在公式中编写双精度型

[英]Exception from HRESULT: 0x800A03EC by writing a double in a formula

I have a written a program to Auto fill an Excel table like this: 我写了一个程序来自动填充Excel表格,如下所示:

A1: 1
A2: =A1+0,5
A3: =A2+0,5
...

If deltaValue is a number like 1 my program does work fine. 如果deltaValue是一个类似于1的数字,则我的程序可以正常工作。 But if deltaValue is for example 0.5 my program throws the exception: Exception from HRESULT: 0x800A03EC 但是,如果deltaValue是例如0.5我的程序将引发异常: Exception from HRESULT: 0x800A03EC

Does anyone know how to solve this problem? 有谁知道如何解决这个问题?

 Excel._Application xlsApp = new Excel.Application();
 xlsApp.Visible = true;

 Excel.Workbook xlsWb = xlsApp.Workbooks.Add();

 double startValue = 0.0;
 double endValue = 0.0;
 double deltaValue = 0.0;

 double.TryParse(txtStartValue.Text, out startValue);
 double.TryParse(txtEndValue.Text, out endValue);
 double.TryParse(txtDeltaValue.Text, out deltaValue);

 xlsWb.Worksheets[1].Range["A1"].value = startValue;
 string temp = string.Format("=R[-1]C+{0}", deltaValue);
 xlsWb.Worksheets[1].Range["A2"].FormulaR1C1 = string.Format("=R[-1]C+{0}", deltaValue);

 double endnumber = (int)(endValue / deltaValue);

 Excel.Range range = xlsWb.Worksheets[1].Range["A2"];

 range.AutoFill(xlsWb.Worksheets[1].Range["A2", string.Format("A{0}", endnumber)], Excel.XlAutoFillType.xlFillDefault);

Try to replace the lines 尝试更换线

string temp = string.Format("=R[-1]C+{0}", deltaValue);
xlsWb.Worksheets[1].Range["A2"].FormulaR1C1 = string.Format("=R[-1]C+{0}", deltaValue);

with

string formula = string.Format(System.Globalization.CultureInfo.GetCultureInfo("en-US"), @"=R[-1]C+{0:0.0}", deltaValue);
xlsWb.Worksheets[1].Range["A2"].FormulaR1C1 = formula;

I was able to reproduce exactly your problem in Excel in Czech language. 我能够用捷克语在Excel中准确地重现您的问题。 The Czech language uses , character as a decimal point, so when I enter =R[-1]C+0,5 in Excel it works OK. 捷克语言使用,字符作为一个小数点,所以当我进入=R[-1]C+0,5在Excel中它的工作原理确定。 But when I try do the same via Excel.Interop then I get HRESULT error. 但是当我尝试通过Excel.Interop进行相同操作时,我得到了HRESULT错误。 So the problem can be solved, if you use allways en-US . 因此,如果您始终使用en-US,则可以解决问题. as a decimal point. 作为小数点。

There used to be a rule that en-US culture should be used when invoking Excel using COM, so it is perhaps connected with that. 过去有一个规则,即使用COM调用Excel时应使用en-US文化。

(Et autant que je suis conscient, la plus belle langue du monde utilise aussi une virgule comme le séparateur décimal.) (Et autant que je suis尽心尽力,la plus belle langue du monde利用了澳大利亚和维尔京的共同成就。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM