Simple Import  from Excel file to Ax 2012 Table

1)first insert data in  excel file with req fields and data.

2) create table in AOT  with fields(data types)

   
3)write the code in job 
static void ImportFromExcel(Args _args)
{

SysExcelApplication application;
SysExcelWorkbooks workbooks;
SysExcelWorkbook workbook;
SysExcelWorksheets worksheets;
SysExcelWorksheet worksheet;
SysExcelCells cells;
COMVariantType type;
DLSTesTable dlsTestTable; // write the table names
FileName filename;

Dialog      dialog  = new Dialog();

DialogField dialogField;
DialogField _file;

    int row =1;
    int col;

Name name;
Address address;
int idnumber;


;
//Prompt to import excel
dialog = new Dialog("Please select the file to load");
dialog.addText("Select file:");
_file = dialog.addField(ExtendedTypeStr("FilenameOpen"));

dialog.run();

if (dialog.closedOK())
{
application = SysExcelApplication::construct();
workbooks = application.workbooks();

try
{
workbooks.open(_file.value());
}

catch (Exception::Error)
{
throw error("File cannot be opened.");
}

workbook = workbooks.item(1);
worksheets = workbook.worksheets();
worksheet = worksheets.itemFromNum(1); //Here 1 is the worksheet Number
cells = worksheet.cells();
//row++;

do
{
row++;
col = 1;

while(col <= 10)
{
switch(cells.item(1, col).value().bStr())
{
    case "IDnumber":
 IDnumber = cells.item(row, col).value().int();
break;

    case "name":
Name = cells.item(row, col).value().bStr();
break;

case "Address":
Address = cells.item(row, col).value().bStr();
break;


}
col++;
}

ttsBegin;
    dlsTestTable.IDnumber = IDnumber;
    dlsTestTable.Name = name;
    dlsTestTable.Address =address;

    dlsTestTable.insert();

ttsCommit;

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

while(type != COMVariantType::VT_EMPTY);

application.quit();
info("Import is done");
}
else
{
dialog.close();
}
}
4)Now run the job 



5)Now select the excel folder location  and select okay.you will get info-log message
then close the info-log and open the table ,then you will see data 

Thanks :))
syam 
AX Technical






















Comments

Popular posts from this blog