Creating a csv file using x++ in AX 2012 R3. To create a csv file as we have different - different classed can be used.
- CommaIO - Output will be - "0000168";0;"Corrugated Partition 279*333*242mm BC/F"
- CommaTextIO - Output will be - "0000168";0;"Corrugated Partition 279*333*242mm BC/F"
- ASCIIIO - Mainly used where you are dealing with ASCII values.
- TextIO - Output will be - 0000168;0;Corrugated Partition 279*333*242mm BC/F
With the CommaIO
static void CreateCSVWithCommaIO(Args _args)
{
#File
CommaIo commaIO;
FileIOPermission perm;
Dialog dlg;
DialogField dialogfield;
Filename fileName;
InventTable inventTable;
container conFile;
dlg = new Dialog(literalstr("Select file"));
dialogfield = dlg.addField(extendedTypeStr(FileNameSave), literalstr("File name"));
dlg.filenameLookupFilter(["csv","*.csv"]);
dialogfield.value(filename);
if (dlg.run())
{
filename = (dialogfield.value());
}
if (fileName)
{
try
{
perm = new FileIOPermission(fileName,#IO_Write);
perm.assert();
commaIO = new CommaIo(fileName, #IO_Write);
commaIO.outFieldDelimiter(";");
inventTable = InventTable::find("0000168");
conFile += inventTable.ItemId;
conFile += inventTable.ItemType;
conFile += inventTable.NameAlias;
commaIO.writeExp(conFile);
}
catch(Exception::Error)
{
exceptionTextFallThrough();
}
CodeAccessPermission::revertAssert();
}
}