How to export table data in to csv file in AX 2012

A CSV is a comma separated values file, which allows data to be saved in a table structured format. Traditionally they take the form of a text file containing information separated by commas.

You can export table data outside of AX into file which separates value of field with a comma. e.g., “Mohammed”, “Raziq”, “Ali”, “Hussain”,”OU”, “MCA”,”27″. Row data are separated by commas.

Lets proceed with an example to understand how to export table into CSV file. The following code writes selected table data into file with proper validations.

CommaTextIo class plays major role in writing/reading file csv in AX 2012

public void exportCSVFile()
{
CommaTextIo file;
container line;
FileIoPermission perm;
#define.filename(@’filepath’)
#File
perm = new FileIoPermission(#filename, #io_write);
perm.assert();
file = new CommaTextIo(#filename, #io_write);
if (!file || file.status() != IO_Status::Ok)
{
throw error(“@SYS19358”);//throws error if file path is not found
}

select * from custTable where custTable.AccountNum == ‘0010’;

if(custTable)
{
line = [custTable.name(),custTable.custGroup,custTable.creditMax];
file.writeExp(line);
}

}
Assign null value to file object once you are done with writing.
file = null;
Hope you understood this simple code to write data into CSV file.

Thanks & Happy DaXing……..

Advertisements

2 responses

  1. awesome, this helped a lot.. thanks much for sharing..

    Liked by 1 person

  2. how to add new worksheet to csv from code, please help

    Like

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

%d bloggers like this: