Microsoft excel functions returns a wrong result

Yamen Jeribi 41 Reputation points
2021-08-16T14:05:47.153+00:00

Hi All,

I use this dll "Microsoft.Office.Interop.Excel" in order to evaluate my excel functions.

 public dynamic EvaluteFormula(string expressionFormula)
     {
         Application excelApp = new Application();
         return excelApp.Evaluate(expressionFormula);
     }

When I use TODAY/NOW function, I don't get the expected result instead I get a number, for example the result of :

  • TODAY() is 44424.
  • NOW() is 44424.613976967594

Has anyone ideas how to solve this problem ?

I encountered this problem with date related functions, I don't know if this issues applies on other functions.

Thanks!

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,157 questions
Excel Management
Excel Management
Excel: A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.Management: The act or process of organizing, handling, directing or controlling something.
1,742 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 118.7K Reputation points
    2021-08-16T17:10:52.117+00:00

    Use a specially designed function — FromOADate:

    double example = 44424.613976967594;
    DateTime date = DateTime.FromOADate( example );
    
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Castorix31 86,491 Reputation points
    2021-08-16T15:04:17.32+00:00

    Dates in Excel are from 1899

    Test :

                    var nResult = EvaluteFormula("TODAY()");
                    DateTime dt = new DateTime(1899, 12, 30);
                    DateTime dtToday = dt.AddDays(nResult);
    

    and I get :

    dtToday = {16/08/2021 00:00:00}

    0 comments No comments

  2. Paul 0 Reputation points
    2024-12-19T18:25:33.39+00:00

    you can force the date formatting by adding a text command that specifies the required formatting try using

    =TEXT(TODAY(),"DDD DD MMM YYYY")

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.