Friday, July 10, 2009

Excel Export from Axapta (through code)

Excel import/export plays a very important role in DAX. This is a simple code of Excel Export. This job represents the creation of excel file along with customer data (two fields). :)


static void Raj_WriteIntoExcel(Args _args)
{
#AviFiles
sysExcelApplication excel;
SysExcelWorkbooks books;
SysExcelWorkbook book;
SysExcelWorkSheets sheets;
SysExcelWorkSheet sheet;
com com;
CustTable custTable;
Integer row = 1, totRecord;
SysOperationProgress progress = new SysOperationProgress();


;

select count(RecId) from custTable;
totRecord = custTable.RecId;

excel = sysExcelApplication::construct();
books = excel.workbooks();
book = books.add();
sheets = excel.worksheets();
sheet = sheets.itemFromNum(1);
com = sheet.comObject();

sheet.cells().item(1,1).value("Customer Id");
sheet.cells().item(1,2).value("Customer Name");

progress.setCaption("Exporting Customer");
progress.setAnimation(#AviTransfer);
progress.setTotal(totRecord);

while select custTable
{
row++;
sheet.cells().item(row,1).value(custTable.AccountNum);
sheet.cells().item(row,2).value(custTable.Name);
progress.setText(strfmt('Exporting Customer %1.', custTable.AccountNum));
progress.setCount(row,row);
}

excel.visible(true);
}

No comments: