Friday, May 27, 2016

X++ code to Export data to Excel sheet in ax 2012


void clicked()
{
   SysExcelApplication  xlsApplication;
   SysExcelWorkBooks    xlsWorkBookCollection;
   SysExcelWorkBook     xlsWorkBook;
   SysExcelWorkSheets   xlsWorkSheetCollection;
   SysExcelWorkSheet    xlsWorkSheet;
   SysExcelRange        xlsRange;
   VendTable            vendtable;
   LogisticsElectronicAddress LogisticsElectronicAddress;
   str contact;
   int recordscount;
   LedgerJournalTrans  ledgerJournalTran;
   LedgerJournalTable  ljt;
   VendBankAccount VendBankAccount;


   int                  row = 1;
   str                  fileName;
   ;
    super();


   // recordsCount = ledgerJournalTrans_ds.recordsMarked().lastIndex();  // Total number of marked records.
    //ledgerJournalTran    = ledgerJournalTrans_ds.getFirst(1);
   ljt= element.args().record();

   //Filename
   fileName = @"C:\Users\shivakumar.p\Desktop\TableExcel";
   //Initialize Excel instance
   xlsApplication           = SysExcelApplication::construct();
   //Open Excel document
   //xlsApplication.visible(true);
   //Create Excel WorkBook and WorkSheet
   xlsWorkBookCollection    = xlsApplication.workbooks();
   xlsWorkBook              = xlsWorkBookCollection.add();
   xlsWorkSheetCollection   = xlsWorkBook.worksheets();
   xlsWorkSheet             = xlsWorkSheetCollection.itemFromNum(1);
     while select LedgerJournalTran where LedgerJournalTran.JournalNum==ljt.JournalNum //join vendtable
    {
   //Excel columns captions
   xlsWorkSheet.cells().item(row,1).value(date2str(LedgerJournalTran.TransDate,213,DateDay::Digits2,DateSeparator::Slash,DateMonth::Digits2,DateSeparator::Slash,DateYear::Digits4));

   xlsWorkSheet.cells().item(row,2).value(LedgerJournalTran.offsetAccountName());
   xlsWorkSheet.cells().item(row,3).value(LedgerJournalTran.AmountCurDebit);
        vendtable =VendTable::find(DimensionAttributeValueCombination::find(LedgerJournalTran.LedgerDimension).DisplayValue);
   xlsWorkSheet.cells().item(row,4).value(vendtable.AccountNum);
   xlsWorkSheet.cells().item(row,5).value(LedgerJournalTran.AccountName());


   xlsWorkSheet.cells().item(row,6).value(enum2str(LedgerJournalTran.OffsetAccountType));
   xlsWorkSheet.cells().item(row,7).value(VendTable::find(DimensionAttributeValueCombination::find(LedgerJournalTran.LedgerDimension).DisplayValue).bankAccountNum());
   xlsWorkSheet.cells().item(row,8).value(VendTable::find(DimensionAttributeValueCombination::find(LedgerJournalTran.LedgerDimension).DisplayValue).email());
   xlsWorkSheet.cells().item(row,9).value(VendTable::find(DimensionAttributeValueCombination::find(LedgerJournalTran.LedgerDimension).DisplayValue).phone());
   xlsWorkSheet.cells().item(row,10).value(LedgerJournalTran.Txt);

   row++;
         //ledgerJournalTran = ledgerJournalTrans_ds.getNext();
    }
   //Fill Excel with CustTable AccountNum and Name fields (only 10 records)
   /*while select custTable
   {
      if(row == 10)
        break;
      xlsWorkSheet.cells().item(row,1).value(custTable.AccountNum);
      xlsWorkSheet.cells().item(row,2).value(custTable.Name());
      row++;
   }
   */
   //Check whether the document already exists
   if(WinApi::fileExists(fileName))
      WinApi::deleteFile(fileName);
   //Save Excel document
   xlsWorkbook.saveAs(fileName);
   //Open Excel document
   xlsApplication.visible(true);
   //Close Excel
   //xlsApplication.quit();
   //xlsApplication.finalize();



}

2 comments:

  1. Hi Shiva,

    Am getting the following error while saving this excel file using the code .

    Method 'saveAs' in COM object of class '_Workbook' returned error code 0x800A03EC () which means: Microsoft Excel cannot access the file 'C:\Desktop\'. There are several possible reasons:

    Regards,
    Mani

    ReplyDelete
  2. This does not work with Office 2016

    ReplyDelete

Export a copy of the standard user acceptance testing (UAT) database

 Reference link: Export a copy of the standard user acceptance testing (UAT) database - Finance & Operations | Dynamics 365 | Microsoft ...