Download a CSV with Column Headings

This Q&A forum allows users to post and respond to "How Do I Do ....." questions. Please do not use to report (suspected) errors - you must use your regional help desk for this. The information contained in this forum has not been validated by LANSA and, as such, LANSA cannot guarantee the accuracy of the information.
Post Reply
atostaine
Posts: 369
Joined: Wed Jan 20, 2016 7:38 am

Download a CSV with Column Headings

Post by atostaine » Wed Apr 17, 2019 12:35 am

Currently using transform_list to download a file using the download CSV example. But I need to have the first line be a column heading.

I can write the stream file with the headings first but I there is no option to append for transform_list.

The appendage list option in transform_file looks like it's to make the CSV wider, not taller. IOW, entrys from each list are appended to create one line in the CSV.

Server is IBM i. I guess I could use system_command to append two files before downloading. I could make the list all alpha fields and write the entries to the list using asString. Any other suggestions?

Thanks, Art
Art Tostaine

jyoung
Posts: 637
Joined: Thu Jan 21, 2016 6:43 am
Location: Oklahoma City, OK USA

Re: Download a CSV with Column Headings

Post by jyoung » Wed Apr 17, 2019 1:31 am

Hey Art, is this in VLWeb where the CSV is downloaded via a browser?

If so, we do this by using *RESOURCE response.

Code: Select all

srvroutine name(Download) response(*RESOURCE #response) session(*REQUIRED)

* write the header
#response.ContentString += ('"Week Ended","Check Number","Days Worked","Hours Worked","Gross Wages"' + #CRLF).AsNativeString

selectlist named(#Checks)

* format values
#lWeekEndDate := #CHDWP.AsDate( CCYYMMDD ).AsDisplayString( MMsDDsCCYY )
#lCheckNumber := #COM_OWNER.FormatCheckNumber( #CKN #CHPMTT )
#lDaysWorked := #TIDW.AsString
#lHoursWorked := #TIHRW.AsString
#lGrossWages := "$" + #CHPG.AsDisplayString( EditCode_N )

#ContentString := ('"&1","&2","&3","&4","&5"').Substitute( #lWeekEndDate #lCheckNumber #lDaysWorked #lHoursWorked #lGrossWages )

* add the string to the response
#response.ContentString += (#ContentString + #CRLF).AsNativeString
endselect

#response.AttachmentFileName := ("&1-&2_CheckHistory.csv").Substitute( #wk_OfficeNumber.AsString #wk_ControlNumber.AsString )
#response.ContentType := "text/csv"

endroutine
A few things to note:
  1. This requires a custom CRLF field that we define as a PRIM_DC.UnicodeString and set the value as #CRLF := (13).AsUnicodeString + (10).AsUnicodeString
  2. ContentString is a PRIM_ALPH
  3. When invoking, use target(*NEW) as in define_com class(#ASCCHIFilterModule.Download) name(#Download) target(New). This will force the browser to open a new window to download the file. Typically the window closes after the download is complete.
There is probably an easier way to do this, but it was been working out well for us.

Joe

atostaine
Posts: 369
Joined: Wed Jan 20, 2016 7:38 am

Re: Download a CSV with Column Headings

Post by atostaine » Wed Apr 17, 2019 1:39 am

Oh I didn't know you could send strings to the response. The example showed transform_list. This should work for me. Thanks!
Art Tostaine

Post Reply