I researched and found many interesting csv posts, then I rewrite it to keep it here in this blog. You will find all references at the bottom.
So the scenario is very simple. Here we would like to read data from a table and export it to a csv file.
Solution
class Example_Job_CSVexport
{
public static void main(Args _args)
{
CommaStreamIo io = CommaStreamIo::constructForWrite();
str fileName = 'Cust_DE.csv',
fileContent;
CustTable custTable;
// Write header
io.writeExp(['Customer', 'Account number', 'Currency', 'DataAreaId']);
// Write line
while select custTable
where custTable.DataAreaId == 'DEMF'
{
io.writeExp([custTable.name(),
custTable.AccountNum,
custTable.Currency,
custTable.DataAreaId]);
}
// Set stream
System.IO.Stream stream = io.getStream();
stream.Position = 0;
// Set stream reader
System.IO.StreamReader sReader = new System.IO.StreamReader(stream);
// Set file contentn string
fileContent = sReader.ReadToEnd();
// Save file
File::SendStringAsFileToUser(fileContent, fileName);
}
}
When running
Explanation
These below are the types/classes we used to transfer data.
Table --> CommaStreamIO --> Stream --> StreamReader --> String ---> File
You will also note that we can use other IO classes as well.
Original source of above figure http://axrachit.blogspot.com/2017/02/x-code-to-read-csv-files-in-dynamics.html
In the next post, we will see how to import this csv back to d365FO.
Ref: https://dynamics365foroperation.blogspot.com/2018/02/create-csv-file-and-save-it.html
Thanks!!! Its very helpful.
ReplyDeleteCan i run this procedure in bach and save the file in a default path without asking the user anything?
ReplyDeleteHi, I never tried it, however I suppose it's possible for sure.
DeleteCSV file save in non editable mode.
ReplyDeleteI have generated csv file from data Gried view ,now i want to save csv file in readonly format.
please help.
Hi, can you give me some more details about your requirement? Normally, I can't see the problem to save a file both editable or read-only.
Delete