Monday, 6 October 2025

Read/Write CSV file

 If unquoted text values are required then CommaTextStreamIo should be replaced with TextStreamIo, which has the same methods.

Read CSV file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
        #File
        #OCCRetryCount
        #define.delimiterField(',')
        System.IO.MemoryStream  memoryStream = connector.getCurrentMemoryStream();
        CommaTextStreamIo       commaTextStreamIo = CommaTextStreamIo::constructForRead(memoryStream);
        if (commaTextStreamIo)
        {
            if (commaTextStreamIo.status())
            {
                throw error("@CLIDocuments:FileUpload_Failed");
            }
 
            commaTextStreamIo.inFieldDelimiter(#delimiterField);
            commaTextStreamIo.inRecordDelimiter(#delimiterCRLF);
                 
            container       record;
 
            while (!commaTextStreamIo.status())
            {
                record = commaTextStreamIo.read();
            }
        }

Write CSV file

1
2
3
4
5
6
        CommaTextStreamIo       commaTextStreamIo = CommaTextStreamIo::constructForWrite();
 
        commaTextStreamIo.writeExp(['row 1 col 1', 'row 1 col 2']);
        commaTextStreamIo.writeExp(['row 2 col 1', 'row 2 col 2']);
 
        File::SendFileToUser(commaTextStreamIo.getStream(), 'file name.csv');
Result:

No comments:

Post a Comment

Creating new D365FO Business Event

  The name should include the noun or phrase that corresponds with theevent, followed by the BusinessEvent suffix. New business event for Cu...