3

I'm trying to create an excel file using x++ code. However, I'm getting the compile error "invalid token" even though intellisense all works correctly when typing out the code. What is the correct way to call the Add() method in x++ for the OfficeOpenXml classes (and in general c# librarys like this?)

using OfficeOpenXml;
using OfficeOpenXml.ExcelPackage;
using OfficeOpenXml.ExcelRange;

class ExcelTestClass
{
    public static void main(Args _args)
    {
        using (ExcelPackage excel = new ExcelPackage())
        {
            excel.Workbook.Worksheets.Add("Worksheet1");

            
        }
    }
}

enter image description here

rjv
  • 1,058
  • 11
  • 29

2 Answers2

3

Of course, just as I give in and ask a question on SA I stumble on the answer while googling.

The correct way to call it is following:

excel.get_Workbook().get_Worksheets().Add("Worksheet1");

From this post: https://community.dynamics.com/ax/f/microsoft-dynamics-ax-forum/178373/calling-visual-studio-c-classes-in-x/438484 Martin Dráb's answer.

rjv
  • 1,058
  • 11
  • 29
1

To add some background detail from Martin Dráb's answer: x++ does not support working with properties of objects in C#/.Net libraries directly. You have to use set and get methods to access those, which is why excel.getWorkbook() works and excel.Workbook does not.

Some other restrictions are

  • you need to use fully qualified names
  • generics are not supported

Additional information can be found at .NET Interop from X++

FH-Inway
  • 4,432
  • 1
  • 20
  • 37