简体   繁体   English

通过C#使用LINEST创建Excel工作表

[英]Create Excel Sheet with LINEST via C#

I have a C# application which has to create an Excel file with formulas. 我有一个C#应用程序,必须使用公式创建一个Excel文件。 Simple formulas work fine. 简单的公式可以正常工作。 But when I use LINEST(...) I get an exception. 但是当我使用LINEST(...)时,我得到一个例外。

Init objects: 初始化对象:

using Microsoft.Office.Interop.Excel;

var Application xlApp = new Microsoft.Office.Interop.Excel.Application();
Workbook xlWorkBook = xlApp.Workbooks.Add(misValue);
var currentWorksheet = xlWorkBook.Worksheets.get_Item(1);

string myLinestFormulaString ="LINEST(LN(C1:C10);$A1:$A10;TRUE;FALSE)";

The line below causes an exception HRESULT: 0x800A03EC 下面的行导致异常HRESULT:0x800A03EC

currentWorksheet.Cells[12, 3].Formula = myLinestFormulaString; // Write to C12

I know the HRESULT means that there is something wrong with the formula string. 我知道HRESULT表示公式字符串有问题。


Edit: Issue solved. 编辑:问题已解决。 I use the German version of Excel. 我使用德语版的Excel。 In C# I had to replace ';' 在C#中,我必须替换';' with commas. 用逗号。

Excel's COM interface use the American list separators. Excel的COM界面使用美国列表分隔符。 That's why you need , instead of ; 这就是为什么你需要,而不是; in your formular. 在您的配方师中。

Try: 尝试:

string myLinestFormulaString = "LINEST(LN(C1:C10),$A1:$A10,TRUE,FALSE)";

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

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