Export data from table to excel sheet by using x++ code in AX2012

 Export data to excel in ax2012



static void Main(Args    _args)

{


    SysExcelApplication                 application;

    SysExcelWorkbooks                   workbooks;

    SysExcelWorkbook                    workbook;

    SysExcelWorksheets                  worksheets;

    SysExcelWorksheet                   worksheet;

    SysExcelCells                       cells;

    SysExcelCell                        cell;

    SysExcelFont                        font;

    int                                 row;

    Dialog                              dialog;

    DialogField                         dialogfield;

    Wre_CreditsDebitsTable              creditsAndDebits;

    Filename                            filename;


   

    application             =           SysExcelApplication::construct();

    workbooks               =           application.workbooks();

    workbook                =           workbooks.add();

    worksheets              =           workbook.worksheets();

    worksheet               =           worksheets.itemFromNum(1);

    workbooks               =           application.workbooks();

    dialog                  =           new Dialog("FileOpen");

    dialogfield             =           dialog.addField(extendedTypeStr(Filenameopen), "File Name");

    dialog.run();



    if (dialog.run())

    {

        filename = (dialogfield.value());

    }


    try

    {

        workbooks.open(filename);

    }


    catch (Exception::Error)

    {

        throw error("File cannot be opened.");

    }

    cells                   =   worksheet.cells();

    cells.range('A:A').numberFormat('@');



   

    cell                    =   cells.item(1, 1);

    cell.value("Serial number");

    font                    =   cell.font();

    font.bold(true);


    cell                    =   cells.item(1, 2);

    cell.value("Name");

    font            =   cell.font();

    font.bold(true);

    cell                    =   cells.item(1, 3);

    cell.value("Gender");

    font            =   cell.font();

    font.bold(true);

    cell                    =   cells.item(1, 4);

    cell.value("Date of birth");

    font            =   cell.font();

    font.bold(true);

    cell                    =   cells.item(1, 5);

    cell.value("Person");

    font            =   cell.font();

    font.bold(true);

    cell                    =   cells.item(1, 6);

    cell.value("Person gender");

    font            =   cell.font();

    font.bold(true);

    cell                    =   cells.item(1, 7);

    cell.value("Contact");

    font            =   cell.font();

    font.bold(true);

    cell                    =   cells.item(1, 8);

    cell.value("Amount");

    font            =   cell.font();

    font.bold(true);

    cell                    =   cells.item(1, 9);

    cell.value("Loan taken date");

    font            =   cell.font();

    font.bold(true);

    cell                    =   cells.item(1, 10);

    cell.value("Reason");

    font            =   cell.font();

    font.bold(true);

    cell                    =   cells.item(1, 11);

    cell.value("Invoice");

    font            =   cell.font();

    

    

    

    row                     =   1;


    while select creditsAndDebits

    {

        row++;

        cell                =   cells.item(row, 1);

        cell.value(creditsanddebits.wre_srnumber);


        cell                =   cells.item(row,2);

        cell.value(creditsanddebits.wre_name);


        cell                =   cells.item(row,3);

        cell.value(enum2str(creditsanddebits.wre_gender));


        cell                =   cells.item(row,4);

        cell.value(creditsanddebits.wre_dob);


        cell                =   cells.item(row,5);

        cell.value(creditsanddebits.wre_person);


        cell                =   cells.item(row,6);

        cell.value(enum2str(creditsanddebits.wre_persongender));


        cell                =   cells.item(row,7);

        cell.value(creditsanddebits.wre_contact);


        cell                =   cells.item(row,8);

        cell.value(creditsanddebits.wre_amount);


        cell                =   cells.item(row,9);

        cell.value(creditsanddebits.wre_loandate);


        cell                =   cells.item(row,10);

        cell.value(creditsanddebits.wre_reason);


        cell                =   cells.item(row,11);

        cell.value(enum2str(creditsanddebits.wre_clearance));

        

}


application.visible(true);

}

Comments