Wednesday, 16 November 2016






How to convert AX to Excel using class x++


this is very easy example to learn that how to use excel class.
first need to create excel in your destination, and put the path in the bold letters. and run it .

static void AxtoExcel(Args _args)
{
    SysExcelApplication      sysExcelApplication;  //this is class
    SysExcelWorkBooks        sysExcelWorkBooks;    //this is class
    SysExcelWorkBook         sysExcelWorkBook;       //this is class
    SysExcelWorkSheets       sysExcelWorkSheets;      //this is class
    SysExcelWorkSheet        sysExcelWorkSheet;       //this is class
    SysExcelRange            sysExcelRange;           //this is class
    CustTable                custTable;
    int                      row = 1;
    str                      filename;
    ;

    // name of the file

    filename = "C:\\Users\akshay\Desktop\fa.xlsx";
    sysExcelApplication = sysExcelApplication::construct();


    // create excel workbook and worksheet
    sysExcelWorkBooks =  sysExcelApplication.workbooks();
    sysExcelWorkBook  =  sysExcelWorkBooks.add();

    SysExcelWorkSheets = sysExcelApplication.worksheets();
    SysExcelWorkSheet  = SysExcelWorkSheets.itemFromNum(1);

    //excel columns captions

    SysExcelWorkSheet.cells().item(row,1).value("Account");
    SysExcelWorkSheet.cells().item(row,2).value("Name");
    row++;

    //fill the custtable to excell

    while select custtable
    {
        if(row == 20) // this means to get only 20 row
        break;

        SysExcelWorkSheet.cells().item(row,1).value(custtable.AccountNum);
        SysExcelWorkSheet.cells().item(row,2).value(custtable.Name);
        row++;
        }

//Check whether the document already exists

   if(WinApi::fileExists(fileName))
      WinApi::deleteFile(fileName);

   //Save Excel document
   sysExcelWorkBook.saveAs(fileName);

   //Open Excel document
   sysExcelApplication.visible(true);
   //Close Excel
   //xlsApplication.quit();
   //xlsApplication.finalize();
}




Thanks

Akshay




No comments:

Post a Comment