Reading CSV file using X++
Static void ReadCSVFile(Args _args)
{
#File
IO iO;
FilenameOpen fileName = "C:\\abcd.csv";
Container con;
boolean firstRecordHdr = true;
Str data1, data2;
;
iO = new CommaTextIo(filename,#IO_Read);
//we can use below option also incase we are using AX through citrix to open file.
//iO = SysDataExpImp::openfileClient(Filename,ReadWrite::read,FileType::Comma);
if (! iO || iO.status() != IO_Status::Ok)
{
throw error("@SYS19358");
}
while (iO.status() == IO_Status::Ok)
{
con = iO.read();
if (con)
{
if (firstRecordHdr)
{
firstRecordHdr = false;
}
else
{
data1 = conpeek(con, 1);//To peek record
data2 = conpeek(con, 2);
info(strfmt('%1--%2',data1,data2));
}
}
}
}
{
#File
IO iO;
FilenameOpen fileName = "C:\\abcd.csv";
Container con;
boolean firstRecordHdr = true;
Str data1, data2;
;
iO = new CommaTextIo(filename,#IO_Read);
//we can use below option also incase we are using AX through citrix to open file.
//iO = SysDataExpImp::openfileClient(Filename,ReadWrite::read,FileType::Comma);
if (! iO || iO.status() != IO_Status::Ok)
{
throw error("@SYS19358");
}
while (iO.status() == IO_Status::Ok)
{
con = iO.read();
if (con)
{
if (firstRecordHdr)
{
firstRecordHdr = false;
}
else
{
data1 = conpeek(con, 1);//To peek record
data2 = conpeek(con, 2);
info(strfmt('%1--%2',data1,data2));
}
}
}
}
Writing to CSV file using X++
Static void WriteCSVFile(Args _args)
{
CommaTextIo file;
Container con;
CustTable custTable;
FileIoPermission permission;
#define.filename('C:\\Temp\\accounts.csv')
#File
permission = new FileIoPermission(#filename, #io_write);
permission = new FileIoPermission(#filename, #io_write);
permission.assert();
file = new CommaTextIo(#filename,
#io_write);
if (!file || file.status() !=
IO_Status::Ok)
{
throw
error("File cannot be opened.");
}
while select custTable
{
con = [custTable.CustAccount,
custTable.CustGroup];
file.writeExp(con);
}
CodeAccessPermission::revertAssert();
info(strFmt("File %1
created.", #filename));
}
Comments
Post a Comment