Page 1 of 1

Download a CSV with Column Headings

Posted: Wed Apr 17, 2019 12:35 am
by atostaine
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

Re: Download a CSV with Column Headings

Posted: Wed Apr 17, 2019 1:31 am
by jyoung
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

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

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.


Re: Download a CSV with Column Headings

Posted: Wed Apr 17, 2019 1:39 am
by atostaine
Oh I didn't know you could send strings to the response. The example showed transform_list. This should work for me. Thanks!