Export the column headers and contents of a browse into a csv file

abaliga

New Member
I am looking to export the contents of a browse, column labels and data into a CSV file.

I got the column labels part done,

How to get all the data that is displayed in a browse now?
This includes all types of columns, which are regular browse query buffer fields, and calculated ones.

Any way to have all rows in a browse exported somehow?:confused:
 

jongpau

Member
Solution 1: Create a new buffer on the table in the browse and then do a for each on that and export the data

Solution 2: Do a for each on the table in the browse and export the data (refresh the browse afterwards)

Solution 3: Position the browse in Row 1 of the browse with a GET-FIRST on the browse query then parse all the rows with a GET-NEXT on the browse query and for each row loop through the columns using a counter and NUM-COLUMNS - I suppose you have already done that to get the labels - get the column name, then get the field handle in the buffer used in the browse query, get the data using the field handle and export it to the file. Put a SKIP at the end of each line. Stop when your query is OFF-END.
 

abaliga

New Member
I wrote a different utility using just the browse handle to dump column-labels and each row data into a csv file.

Below is the code if it helps anyone in future.

/* input parameters */
define input parameter hlBrowse as handle no-undo.

/* stream */
define stream stDownload.

/* variables */
define variable lvCntr as integer no-undo.
define variable lvColumnHdl as handle no-undo.
define variable lvExportLine as character no-undo.
define variable lvHaveRow as logical no-undo.
define variable lvLine as integer no-undo.

status default "Processing data for export please wait....".

output stream stDownload to value("dump.csv").

/* build column label list for put stream */
do lvCntr = 1 to hlBrowse:num-columns:
if lvCntr = 1
then
lvExportLine = hlBrowse:get-browse-column(lvCntr):label.
else
lvExportLine = lvExportLine + "," +
hlBrowse:get-browse-column(lvCntr):label.
end. /* do lvCntr */
/* cannot use export statement due to limitations with
sending dynamic generated code */
put stream stDownload unformatted
lvExportLine.
put stream stDownload unformatted
"~n".
lvLine = 1.
repeat:
if lvLine = 1
then
lvHaveRow = hlBrowse:select-row(1).
else
lvHaveRow = hlBrowse:select-next-row().
if lvHaveRow = no
then
leave.
lvLine = lvLine + 1.
lvExportLine = "".
do lvCntr = 1 to hlBrowse:num-columns:
lvColumnHdl = hlBrowse:get-browse-column(lvCntr).
if lvCntr = 1
then
lvExportLine = lvColumnHdl:screen-value.
else
lvExportLine = lvExportLine + "," + lvColumnHdl:screen-value.
end. /* do lvCntr */
put stream stDownload unformatted
lvExportLine.
put stream stDownload unformatted
"~n".
end. /* repeat */
output stream stDownload close.
 
Top