Search code examples
excelaxaptaexcel-interopx++dynamics-ax-2009

Setting range for table in Excel file


I am generating Excel file in Microsoft Dynamics AX 2009. I would like to create a table in Excel from 9th row. Even though I set a range, the table always shows at cell A1.

SysExcelRange           xlsRange;
SysExcelListObject      listObj;
#define.ListObjectName("xlGuess")

...

xlsRange = xlsWorkSheet.range("A9");
listObj = xlsWorkSheet.listObjects().add(xlsRange, #ListObjectName);

enter image description here

I've also tried to set full range like "A9:E13" and "A9:A13", but it didn't work either. Probably, it's possible to do it with COM objects but I couldn't achive it. Why it doesn't work?


Solution

  • It seems that there is a bug in the Excel wrapper of AX.
    If I changed one line to pass the actual range to the underlying COM object then it seems to work; have a look at the screenshot for what I have changed.

    enter image description here

    With a AOT job like this

    public static client void TestJob()
    {
        #define.ListObjectName("xlGuess")
    
        SysExcelApplication     xlsApp;
        SysExcelWorksheet       xlsWorksheet;
        SysExcelRange           xlsRange;
        SysExcelListObject      listObj;
        ;
    
        xlsApp = SysExcelApplication::construct();
        xlsApp.workbooks().add();
        xlsWorksheet = xlsApp.activeSheet();
        xlsRange = xlsWorksheet.range("A9:C12");
        listObj = xlsWorkSheet.listObjects().add(xlsRange, #ListObjectName);
        xlsApp.visible(true);
    }
    

    it will open an Excel file like this:

    enter image description here