Monday, 7 December 2015


Read from an Excel through X++ code




static void readExcelFiles(Args _args)

{

    SysExcelApplication         application;

    SysExcelWorkbooks           workbooks;

    SysExcelWorkbook            workbook;

    SysExcelWorksheets          worksheets;

    SysExcelWorksheet           worksheet;

    SysExcelCells               cells;

    Filename                    filename;

    COMVariantType              type;

    int                         rowNo;



    rowNo   = 2;//If the excel file having header.

    application = SysExcelApplication::construct();

    workbooks = application.workbooks();

    filename = @"C:\Users\eswar.t\Desktop\item.xlsx";//Excel file path.

    try

    {

        workbooks.open(filename);

    }

    catch (Exception::Error)

    {

        throw error("File cannot be opened.");

    }

    workbook = workbooks.item(1);

    worksheets = workbook.worksheets();

    worksheet = worksheets.itemFromNum(1);//which shows excel sheet number.

    cells = worksheet.cells();



    type = cells.item(rowNo, 1).value().variantType();

    while (type != COMVariantType::VT_EMPTY)//loop through given excel column.

    {

        type = cells.item(rowNo+1, 1).value().variantType();//It will find variant type of column from given Excel sheet(ReadIntegerFile.xlsx) tiil it get empty.

        info(strFmt("%1",real2int(cells.item(rowNo,1).value().double())));

        rowNo++;//To get next column number in the loop.

    }

    application.quit();

}

No comments:

Post a Comment