Wednesday, 3 April 2013
AX 2009 : Exporting data in Excel
static void SalesOrderDump(Args _args)
{
SysExcelApplication xlsApplication;
SysExcelWorkBooks xlsWorkBookCollection;
SysExcelWorkBook xlsWorkBook;
SysExcelWorkSheets xlsWorkSheetCollection;
SysExcelWorkSheet xlsWorkSheet;
SysExcelRange xlsRange;
SalesLine _salesLine;
SalesTable _salesTable;
Query q;
QueryBuildDataSource qbds;
QueryBuildRange qbr;
QueryRun qr;
int row = 1;
FileName fileName;
PaymSchedule _paymSchedule;
FileNameFilter filter = ["Excel file", "*.xlsx"];
;
fileName = winapi::getSaveFileName(infolog.hWnd(), filter , @"c:\...\desktop", "Save as Excel file","xlsx","Sales Order");
//fileName = "C:\\Test4.xlsx";
//fileName = @"C:\Users\jagadis\Desktop\tes3.xlsx";
//fileName = "C:\\Users\\arvind\\Desktop\\FilterSheet_AR.xlsx";
if(!fileName)
return;
//Check whether the document already exists
if(WinApi::fileExists(fileName))
throw error("File already exist");
//WinApi::deleteFile(fileName);
//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("Sales Order");
xlsWorkSheet.cells().item(row,2).value("Customer account");
xlsWorkSheet.cells().item(row,3).value("Invoice account");
xlsWorkSheet.cells().item(row,4).value("Status");
xlsWorkSheet.cells().item(row,5).value("Item number");
xlsWorkSheet.cells().item(row,6).value("Unit price");
xlsWorkSheet.cells().item(row,7).value("Net Amount");
row++;
q = new Query();
qbds = q.addDataSource(tablenum(SalesLine));
qr = new QueryRun(q);
//Fill Excel with CustTable AccountNum and Name fields (only 10 records)
while(qr.next())
{
_salesLine = qr.get(tablenum(SalesLine));
//if(row == 5)
// break;
xlsWorkSheet.cells().item(row,1).value(_salesLine.SalesId);
xlsWorkSheet.cells().item(row,2).value(_salesLine.CustAccount);
xlsWorkSheet.cells().item(row,3).value(_salesLine.invoiceAccount());
xlsWorkSheet.cells().item(row,4).value(enum2str(_salesLine.SalesStatus));
xlsWorkSheet.cells().item(row,5).value(_salesLine.ItemId);
xlsWorkSheet.cells().item(row,6).value(_salesLine.SalesPrice);
xlsWorkSheet.cells().item(row,7).value(_salesLine.LineAmount);
row++;
}
//Save Excel document
xlsWorkbook.saveAs(fileName);
//Open Excel document
xlsApplication.visible(true);
info("Success");
//Close Excel
//xlsApplication.quit();
//xlsApplication.finalize();
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment