Subject Re: csv headers
From Charlie <tm@tc.com>
Date Sun, 19 Jan 2020 16:58:03 -0500
Newsgroups dbase.getting-started

I figured out how to insert the headers in the CSV file Mervyn... Basically in the create csv program I wrote the headers before the do while loop and added a line change.  It now works great!!  Thanks for your help on this project!!!

Mervyn Bick Wrote:

> On 20/12/2019 00:48, Charlie wrote:
> > Hi... Mervyn helped me export a table to a csv file with a file called create_csv_file.prg and I am still using it.  Now I am working on a similar project except for the csv file needs headers for each row.  I've looked at this and can't figure out how to do this.  Any help?
> >
>
>
> That little program creates an output file with the pipe symbol | as a
> separator.  As such it is strictly not a .csv file and, in hindsight, I
> should have used a more descriptive name. Perhaps create_pipe_file.prg
> On the other hand, it was written specifically for you so I'm not going
> to change the name now.
>
> The revised program will take an additional parameter.  If this is set
> true the fieldnames will be added to the top of the outputfile.  If this
> parameter is omitted it defaults to false and the program behaves
> exactly as in the past.
>
> The original program made provision for a third parameter which would
> specify a delimiter.  If no value was provided then no delimiter was
> used.  Because the program now accepts 4 parameters with the fourth
> parameter set true to include the fieldnames you will need to include a
> delimiter even if it is blank if you want to include the fourth parameter.
>
> create_csv_file('mytable.dbf','myoutputfile.txt',[],true)
>
> If you only provide the first two or three parameters the program will
> work exactly as before.
>
> If you changed the date format for the output file in the original
> program you will need to make the changes to this new program as well.
>
> If you need a delimiter for the date then
>
> create_csv_file('mytable.dbf','myoutputfile.txt',["],true)
>
> Mervyn.
>
> parameters cTable,cTextfile,cDelim,bFieldnames
>  if argcount() < 2
>    msgbox('Table name and text file name required.','Error')
>    return
>  elseif argCount() = 2
>    cDelim = ''
>    bFieldnames = false
>  elseif argCount() = 3
>    bFieldnames = false
> endif  
> cCsv = cTextfile
> CRLF = chr(13)+chr(10)
> lFirst = true
> q = new query()
> q.sql = 'select * from "'+cTable+'"'
> q.active = true
> fCSV = new file()
> fCSV.create(cCSV)
> if bFieldnames
>    cLine = ""
>    for n = 1 to q.rowset.fields.size
>       cLine += q.rowset.fields[n].fieldname
>       cLine += "|"
>    next  
>    cLine = substr(cLine,1,rat("|",cLine)-1) //delete last separator
>    fCSV.write(cLine,len(cLine))
>    lfirst = false
> endif
> do while not q.rowset.endofset
>    if not lFirst = true
>       fCSV.write(CRLF,2)
>    endif  
>    cLine = ""
>    for n = 1 to q.rowset.fields.size
>       if q.rowset.fields[n].type = "CHARACTER" or q.rowset.fields[n].type = "MEMO"
>         cLine += cDelim //add delimiter
>         cLine += iif(empty(q.rowset.fields[n].value),'', trim(q.rowset.fields[n].value))
>         cLine += cDelim //add delimiter
>       elseif  q.rowset.fields[n].type = "DATE"
> //        cLine += cDelim
>         cLine += dtos(q.rowset.fields[n].value)  //see note below
> //        cLine += cDelim            
>       elseif q.rowset.fields[n].type = "LOGICAL"
>         if q.rowset.fields[n].value
>            cline += "T"
>         else
>            cLine += "F"
>         endif
>       else
>         cLine += iif(empty(q.rowset.fields[n].value),0,q.rowset.fields[n].value)
>       endif
>       cLine +="|" //add seperator
>    next
>    cLine = substr(cLine,1,rat("|",cLine)-1) //delete last separator
>    fCSV.write(cLine,len(cLine))
>    lFirst = false
>    q.rowset.next()
> enddo
> fCSV.close()
> q.active = false
>
> //set date ymd
>  //note,  dtos() produces 20110907 which matches output from COPY TO...DELIMITED.  
>  //Change to dtoc and uncomment line before and after for output acceptable by Excel
// ie with delimiters and with / separators.