How to read Excelfile using x++ code in ax 2012

Hi Guys,
The following code will helps you to read Excel file using x++ code.

  static void howToReadExcelSheet(Args _args)
  {
            SysExcelApplication                    application;
            SysExcelWorkbooks                     workbooks;
            SysExcelWorkbook                      workbook;
            SysExcelWorksheets                    worksheets;
            SysExcelWorksheet                      worksheet;
            SysExcelCells                               cells;
            COMVariantType                         type;
            int                                                  row;
            FileName                                      filename;
            date                                               expiryDate,warrentyDate;
            AccountNum                                accountNum;    
   
            row = 1;
            application = SysExcelApplication::construct();
            workbooks   = application.workbooks();
            filename = @'C:\TestFolder\test.xlsx';
            try
            {
                workbooks.open(filename);
            }
            catch (Exception::Error)
            {
                throw error("File cannot be opened.");
            }

            workbook        = workbooks.item(1);
            worksheets      = workbook.worksheets();
            worksheet       = worksheets.itemFromNum(1);
            cells           = worksheet.cells();
            do
            {
                  row++;
                  accountNum    = cells.item(row,1).value().bStr();
                  expiryDate    = cells.item(row,2).value().date();
                  warrentyDate  = cells.item(row,3).value().date();
               
                type = cells.item(row+1, 1).value().variantType();
            }
            while(type != COMVariantType::VT_EMPTY);
            application.quit();
            info(strFmt(" No.of rows: %1",row));
        }

Thanks.

Comments

Popular posts from this blog

x++ code to add hyperlink to a field in D365FO

x++ code to get Vendor Email Address based on Purpose in Dynamics 365 for Operations

x++ code to get Customer Address based on purpose in ax 2012