Thursday, 9 July 2015

Import data from Excel into AX in Microsoft Dynamics AX 2012

static void mynamescopyfromexcel(Args _args)
{
SysExcelApplication application;
SysExcelWorkbooks workbooks;

SysExcelWorkbook workbook;
SysExcelWorksheets worksheets;
SysExcelWorksheet worksheet;
SysExcelCells cells;
COMVariantType type;
Name name;
FileName filename;
testTable  mynames;

int row = 1 ;

str lastname;
str firstname;
int salary;

application = SysExcelApplication::construct();
workbooks = application.workbooks();
//specify the file path that you want to read
filename = @"C:\Location\Name.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++;
    firstname = cells.item(row, 1).value().bStr();
    lastname = cells.item(row, 2).value().bStr();
    mynames.clear();
    mynames.Fname = firstname;
    mynames.Lname = lastname;
    mynames.insert();

    info("Data inserted succesfully");

    type = cells.item(row+1, 1).value().variantType();
}

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

No comments:

Post a Comment