Saturday, 20 December 2014

How To Export Data Into Excel - Ax 2012

Hello Guys,

In this post i'm showing how to export data into excel.

In this example i'm exporting all number sequence list  in current legal entity .

Example : vend id,custid,paymentjournalnum

Step1 :Copy and Paste the below code in AOT-> Job
Step2: Run the Job
step3: Go to "C" drive and then find the excel sheet Named as "Numseqlist"

static void Numberseq_List(Args _args)
{
   SysExcelApplication  xlsApplication;
   SysExcelWorkBooks    xlsWorkBookCollection;
   SysExcelWorkBook     xlsWorkBook;
   SysExcelWorkSheets   xlsWorkSheetCollection;
   SysExcelWorkSheet    xlsWorkSheet;
   SysExcelRange        xlsRange;
    NumberSequenceReference NumberSequenceReference;
    NumberSequenceTable NumberSequenceTable;
    NumberSequenceDatatype NumberSequenceDatatype;
   int                  row = 1;
   str                  fileName;
    CompanyInfo companyInfo;
   ;
   //Filename
   fileName = "C:\\Numseqlist.xlsx";
   //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);
   //Excel columns captions
   xlsWorkSheet.cells().item(row,1).value("NumberSeqId");
   xlsWorkSheet.cells().item(row,2).value("Format");
   xlsWorkSheet.cells().item(row,3).value("Name");
   row++;
   //Fill Excel with NumberseqId,Format,Name fields (only 10000 records)
    companyInfo.RecId = CompanyInfo::findDataArea(curext(),false).RecId;
   while select NumberSequenceReference where NumberSequenceReference.numbersequencescope == companyInfo.RecId
    {
        select numbersequence from NumberSequenceTable where NumberSequenceTable.recid == NumberSequenceReference .numbersequenceId;
        select firstonly1  NumberSequenceDatatype where NumberSequenceDatatype.RecId == NumberSequenceReference.NumberSequenceDatatype;
       if(row == 10000)
        break;
       xlsWorkSheet.cells().item(row,1).value(NumberSequenceTable.NumberSequence);
        xlsWorkSheet.cells().item(row,2).value(NumberSequenceTable.Format);
       xlsWorkSheet.cells().item(row,3).value(NumberSequenceDatatype.referenceLabelForDisplay());
       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();
}


@Rahul Talasila

1 comment:

Adding a newline into a string in C# and X++

Below is the sample code we can use for  adding a newline after every occurrence of "@" symbol in the string in C#   using System...