Sunday, 9 February 2014

Export Records To Excel Through Code In Ax 2012

static void ExportImportData(Args _args)
{
   SysExcelApplication      xlsApplication;
   SysExcelWorkBooks    xlsWorkBookCollection;
   SysExcelWorkBook     xlsWorkBook;
   SysExcelWorkSheets   xlsWorkSheetCollection;
   SysExcelWorkSheet    xlsWorkSheet;
   SysExcelRange           xlsRange;
   Tablename                  exportImportRecords;
   int                               row = 1;
   str                               fileName;
   ;
   //Filename
   fileName = "C:\\Libraries\Documents\exportImportRecords.xlsx";
   //Initialize Excel instance
   xlsApplication           = SysExcelApplication::construct();
   //Create Excel WorkBook and WorkSheet
   xlsWorkBookCollection    = xlsApplication.workbooks();
   xlsWorkBook              = xlsWorkBookCollection.add();
   xlsWorkSheetCollection   = xlsWorkBook.worksheets();
   xlsWorkSheet             = xlsWorkSheetCollection.itemFromNum(1);
   //Excel columns captions
   xlsWorkSheet.cells().item(row,1).value("Account Num");
   xlsWorkSheet.cells().item(row,2).value("Name");
   xlsWorkSheet.cells().item(row,3).value("Phone");
   row++;
   //Fill Excel with exportImportRecords AccountNum and Name fields (only 10 records)
   while select exportImportRecords
   {
      if(row == 10)
        break;
      xlsWorkSheet.cells().item(row,1).value(exportImportRecords.ID);
      xlsWorkSheet.cells().item(row,2).value(exportImportRecords.Name);
       xlsWorkSheet.cells().item(row,3).value(exportImportRecords.Phone);
      row++;
   }
   //Check whether the document already exists
   if(WinApi::fileExists(fileName))
      WinApi::deleteFile(fileName);
   //Open Excel document
   xlsApplication.visible(true);
   info("Export Completed");
}

No comments:

Post a Comment