Import data into tables from CSV file in AX:

Hi,
This code helps you to import data into tables from CSV files.
static void loadCSVFile(Args _args)
{
Dialog dialog;
DialogField dialogFileName;
SysOperationProgress simpleProgress;
Filename filename;
FileIOPermission permission;
TextIO textIO;
ExcelFileImport ExcelFileImport;
str s11;
int i;
Container c1,c2;
CompanyInfo companyInfoLoc = CompanyInfo::find();
Container filterCriteria;
#File
#avifiles
;

dialog = new Dialog(“Importing Text File”);
dialogFileName = dialog.addField(typeid(Filenameopen), “File Name”);
filterCriteria = [‘*.txt’];
filterCriteria = dialog.filenameLookupFilter(filterCriteria);
dialog.run();
if (dialog.run())
filename = dialogFileName.value();
if(!filename)
{
info(“Filename must be filled”);
throw(“”);
}
permission = new fileIOpermission(filename,#io_read);
permission.assert();
textIO = new TextIO(filename,#io_read);
textIO.inFieldDelimiter(‘,’);///Change the Delimeter if it is , or ; etc
simpleProgress = SysOperationProgress::newGeneral(#aviUpdate, ‘Importing sales data’,100);
if(textIO)
{
while(textIO.status() == IO_Status::Ok)
{
c1 = textIO.read();
s11 = conpeek(c1,1);
if(strlen(s11) > 1)
{
ExcelFileImport.itemid = Conpeek(c1,1);
ExcelFileImport.name = Conpeek(c1,2);
ExcelFileImport.insert();
i++;
simpleProgress.incCount();
simpleprogress.setText(strfmt(“Lines imported: %1”, i));
info(strfmt(“Item : %1 has been created”,ExcelFileImport.itemid));
sleep(10);
}
}
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s