www.pat www.pat - 1 month ago 9
C# Question

Create Excel Sheet with LINEST via C#

I have a C# application which has to create an Excel file with formulas.
Simple formulas work fine.
But when I use LINEST(...) I get an exception.

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

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


I know the HRESULT means that there is something wrong with the formula string.




Edit: Issue solved.
I use the German version of Excel. In C# I had to replace ';' with commas.

Answer

Excel's COM interface use the American list separators. That's why you need , instead of ; in your formular.

Try:

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