Tuesday, August 16, 2011

CommaTextIO Class How to Export data to .csv file via X++

I'm sure many of you out there might have already used this API within AX.

I had a requirement to spit out a .csv file from AX, Looping through Iventory Pricing and On-Hand Data.
And this code snippet from MSDN was handy for the purpose

http://msdn.microsoft.com/en-us/library/aa624902(v=ax.50).aspx

Below sample shows example of how to output the data from CustTable.

#File
CommaTextIo commaTextIo;
FileIOPermission permission;
CustTable custTable;
str fileName = @"C:\test.csv";
;
permission = new FileIOPermission(fileName,#io_write);
permission.assert();
commaTextIo = new CommaTextIo(fileName,#io_write);
while select custTable
{
    commaTextIo.write(custTable.AccountNum,custTable.Name);
}
CodeAccessPermission::revertAssert();



2 comments:

  1. By following your article, am able to write data to csv from x++. But i want to know, how to leave 2 column empty in between while writing data to csv.

    Thanks in advance.. Any answers would be appreciated..

    ReplyDelete
  2. By following your article, am able to write data to csv from x++. But i want to know, how to leave 2 column empty in between while writing data to csv.

    Thanks in advance.. Any answers would be appreciated..

    ReplyDelete