mardi 8 février 2011

How to export Data from Dynamics AX to an Excel file

How it works
1. Use SysExcelApplication class to create excel file.
2. Use SysExcelWorkbooks and SysExcelWorkbook to create a blank workbook(by
default 3 worksheets will be available).
3. Use SysExcelWorkSheets to select worksheet for writing data.
4. SysExcelCells to select the cells in the excel for writing the data.
5. SysExcelCell to write the data in the selected cells.
6. Once you done with write operation use SysExcelApplication.visible to open
file. 

Try the following code :)

static void ExportDataToExcelFile(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

    worksheet = worksheets.itemFromNum(1);//Selects the first worksheet in the workbook to insert data
    cells = worksheet.cells();
    cells.range('A:A').numberFormat('@');  // numberFormat ‘@’ is to insert data as Text

    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
}

Happy Daxing!

Aucun commentaire:

Enregistrer un commentaire