Wednesday, 18 March 2015

Export table data to excel file using SysExcelApplication object in AX 2012

Write the following code in a job :

static void exportToExcelFile(Args _args)
{
    InventTable inventTable;
    SysExcelApplication application;
    SysExcelWorkbooks workbooks;
    SysExcelWorkbook workbook;
    SysExcelWorksheets worksheets;
    SysExcelWorksheet worksheet;
    SysExcelCells cells;
    SysExcelCell cell;
    Filename    fileName;
    int row;
 
    fileName = "C:\\desktop\\itemDetails.xlsx";

    application = SysExcelApplication::construct();
    workbooks = application.workbooks();
    workbook = workbooks.add();

    worksheets = workbook.worksheets();
    worksheet = worksheets.itemFromNum(1);
    cells = worksheet.cells();
    cells.range('A:A').numberFormat('@');

    cell = cells.item(1,1);
    cell.value("Item");
    cell = cells.item(1,2);
    cell.value("Name");
    row = 1;

    while select inventTable
    {
    row++;
    cell = cells.item(row, 1);
    cell.value(inventTable.ItemId);
    cell = cells.item(row, 2);

    cell.value(inventTable::find(inventTable.ItemId).itemName());
    }

    workbook.saveAs(fileName);
    workbook.comObject().save();
    workbook.saved(true);
}

No comments:

Post a Comment