Is it possible to read structured data from a CSV in Genexus?

Viewed 127

Genexus has the ExcelDocument data type that allows you to read data in a tabular way from an excel file, specifying rows and columns. Is there a way to do the same with a csv file? I can open it and read it like a normal txt, but a structure would be more effective

2 Answers

As ealmeida explained you can use the Delimited ASCII files functions.

Below you can see an example on how to code both read and write operations.

ASCII File sample

1,"Jane Doe",1955-05-21
2,"John Smith",1991-10-15
3,"William Shakespeare",2005-11-30

Setup variables with parameters

&FullFileName = !'Datos.txt'
&RecordLength = 50
&FieldsDelimiter = !','
&StringDelimiter = !'"'
&DateFormat = !'ymd'
&DateSeparator = !'-'

To read Delimited ASCII

&ErrorNbr = DFROpen(&FullFileName, &RecordLength, &FieldsDelimiter, &StringDelimiter)
do while DFRNext() = 0
    &ErrorNbr = DFRGNum(&PersonNumber)
    &ErrorNbr = DFRGTxt(&PersonName)
    &ErrorNbr = DFRGDate(&PersonDOB, &DateFormat, &DateSeparator)
enddo
&ErrorNbr = DFRClose()

To write Delimited ASCII

&ErrorNbr = DFROpen(&FullFileName, &RecordLength, &FieldsDelimiter, &StringDelimiter)
for each Person
    &ErrorNbr = DFWPNum(PersonNumber, 0)
    &ErrorNbr = DFWPTxt(PersonName)
    &ErrorNbr = DFWPDate(PersonDOB, &DateFormat, &DateSeparator)
    &ErrorNbr = DFWNext()
endfor
&ErrorNbr = DFWClose()
Related