Exporting Data To The Excel Sheet In AX 2012
static void RBExportToExcel(Args _args)
{
SysExcelApplication xlsApplication;
SysExcelWorkBooks xlsWorkBookCollection;
SysExcelWorkBook xlsWorkBook;
SysExcelWorkSheets xlsWorkSheetCollection;
SysExcelWorkSheet xlsWorkSheet;
SysExcelRange xlsRange;
CustTable custTable;
int row = 1;
str fileName;
fileName = "C:\\RBTest.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("Account Num"); xlsWorkSheet.cells().item(row,2).value("Name");
row++;
while select custTable
{ 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();
info(strfmt("Total no of rows imported %1", row));
}
No comments:
Post a Comment