AX2012 Read And Import Excel File using X++ Code


X++ code to read the Excel file 


static void RB_ReadExcel(Args _args)
{
    SysExcelApplication application;
    SysExcelWorkbooks workbooks;
    SysExcelWorkbook workbook;
    SysExcelWorksheets worksheets;
    SysExcelWorksheet worksheet;
    SysExcelCells cells;
    COMVariantType type;
    FilenameOpen                    filename;

    int row = 1; // if the excel has the header
    TransDate     empJoiningDate;
    real          salary;
    str           empId;
    int           totNoOfLeaves;

    ;

    application = SysExcelApplication::construct();
    workbooks = application.workbooks();
    filename  = "C:\\Users\\balakrishnanr\\Desktop\\emp.xlsx"; // file path

    try
    {
        workbooks.open(filename);
    }
    catch (Exception::Error)
    {
        throw error("File not found");
    }
    workbook = workbooks.item(1);
    worksheets = workbook.worksheets();
    worksheet = worksheets.itemFromNum(1);
    cells = worksheet.cells();

    //Iterate through cells and get the values
    do
    {
    //Incrementing the row line to next Row
    row++;
    empId           = cells.item(row,1).value().bStr();
    empJoiningDate  = cells.item(row,2).value().date();
    salary          = cells.item(row,3).value().double();
    totNoOfLeaves   = cells.item(row,4).value().int();


   info(strFmt("Empl Id:- %1 , DOJ :- %2 , Salary :- %3 ,  TotNoLeaves :-   %4",empId,empJoiningDate,salary,totNoOfLeaves));

    // Loads the next row into the variant type and validating that its is empty or not
    type = cells.item(row+1, 1).value().variantType();
    }
    while (type != COMVariantType::VT_EMPTY);

    // quits the application
    application.quit();

}

1 comment:

Install and Configure Zpl Printer on D365 F&O

  Setup Zpl Printer On D365 F&O Posted on Recently, I have had an opportunity review the possibilities to print license plates within D3...