Search This Blog

Monday, 27 January 2014

X++ code to Import Excel Data to Ax Tables

static void ReadExcel(Args _args)
{
    SysExcelApplication application;
    SysExcelWorkbooks   workbooks;
    SysExcelWorkbook    workbook;
    SysExcelWorksheets  worksheets;
    SysExcelWorksheet   worksheet;
    SysExcelCells       cells;
    COMVariantType      type;
    int                 row;

    CustAccount         custAccount;
   
    Name                name;
    FileName            filename;
    CustNew             custNew;
    ;

    application = SysExcelApplication::construct();
    workbooks = application.workbooks();
   
    filename = "F:\\Copy of CustTable.xls";
  custNew.clear(); 
   


   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++;
        custAccount = cells.item(row, 1).value().bStr();
        name = cells.item(row, 2).value().bStr();
        type = cells.item(row+1, 1).value().variantType();

        custNew.AccountNum=custAccount;
        custNew.Name=name;
        custNew.insert();

    } while (type != COMVariantType::VT_EMPTY);
    application.quit();
}


No comments:

Post a Comment