Dynamics AX 2009 : Exporting data to Excel (Part1)

Quiet often we get the request from the clients that they wish if the reports or query data they design can be seen in excel in the Excel formatted way. Though the standard reports output can be sent to Excel  but that is not formatted . Almost all the enquiries have a button to export the grid data to Excel but that gives them limitation of only the GRID data.

We have recently given the following two solutions in different scenarios to different clients. Where the data is not very huge ( the user is expecting a couple of hundred lines as a query output then the following solution will work nice.

Dynamics AX’s classes prefixed with SysExcel actually do the export to excel. We would use these classes and create a small list of customers alongwith the itemIds they have bought or placed order for.

Open the AOT. Create a shared project e.g. SD_ExcelExport

Create the a new class as shown below :

=================================================================================

class SDExcelExport
{
}

public static void main(Args args)
{

    CustTable custTable;
    SysExcelApplication application;
    SysExcelWorkBooks    workbooks;
    SysExcelWorkBook     workbook;
    SysExcelWorksheets  worksheets;
    sysExcelWorksheet   worksheet;
    SysExcelCells       cells;
    SysExcelCell        cell;
    int                 row;

    ;

    application = SysExcelApplication::construct();
    workbooks = application.workbooks(); //gets the workbook object
    workbook = workbooks.add();  // creates a new workbook
    worksheets = workbook.worksheets(); //gets the worksheets object
    //The following selects the first worksheet in the workbook to insert data 
    worksheet = worksheets.itemFromNum(1);
    cells = worksheet.cells();
    // numberFormat ‘@’ is to insert data as Text

    cells.range('A:A').numberFormat('@');  
    while select custTable
    //The following loop will provide the data to be populated in each column
    {
        row++;
        cell = cells.item(row,1);
        cell.value(custTable.AccountNum);
        cell = cells.item(row,2);
        cell.value(custTable.Name);
        cell = cells.item(row,3);
        cell.value(CustTable.CustGroup);
        cell = cells.item(row,4);
        cell.value(CustTable.Currency);
        cell = cells.item(row,5);
        cell.value(CustTable.CreditMax);
        cell = cells.item(row,6);
        cell.value(CustTable.CreditRating);
    }
    application.visible(true);  '// opens the excel worksheet
}

 

=================================================================================

When you run the class then you will get the following output formatted

Complex queries can be written and the data can be straight away sent to Excel.

This entry was posted in Dynamics AX, AX2009. Bookmark the permalink.

8 Responses to Dynamics AX 2009 : Exporting data to Excel (Part1)

  1. Tommy says:

    How would this perform where you have like 5000 rows?

  2. Shekhar says:

    Hi,The above would perform a little slow when there is a large data to be exported. But there are always merits and demerits. I will post another solution which i have in mind in (Part 2) which will be quicker on performance but will have limitation in some other area. .. Check when I post (Part 2)

  3. Tommy says:

    Thanks. Good to know! 😀

  4. Tommy says:

    After playing around with this, I can with certainty say that this performs very poorly. Maybe it also depends on the environment I am running in, but the throughput is less than 100 lines pr minute in my scenario. Don\’t use this this if you have more than 50 lines in my opinion, or you will only annoy the user.

  5. Shekhar says:

    Hi Tommy: This is just an information about a feature available in the product and in no way guarantee about the performance. The performance of any feature depends on the many factors. I got 1241 rows in a min and 10 sec ( exported all the custtrans data to excel.) The purpose was to let other professionals know that there is something of this kind available in the product.There are many alternatives if you did not like the output for this. We can pull information in Excel through ODBC connection and Microsoft queries, Push data as described in Part2, use a third party tool

  6. Nick says:

    Can we export to excel using batch jobs? Thanks! – Nick

  7. Roxanne says:

    If some one desires to be updated with most up-to-date technologies after that he must be pay a visit this
    web page and be up to date everyday.

  8. Saikat Sarkar says:

    Hi Shekhar : In this class the exporting data is showing. If I want to save it in a specific location automatically then what I have to do. Please explain the code to save this file.

Leave a comment