Here is a HMG function for exporting arrays to csv files. That will be very much helpful in exporting a report to another software (like Excel)
Code: Select all
#include <hmg.ch>
function array2csv( aInData )
local filename := ""
local lines := 0
local i := 0
local count1 := 0
local linedata := {}
local aData := {}
local cdata := ""
local aclinedata := {}
local fhandle := 0
local linebreak := chr(13)
local aline := {}
local cline := ''
filename := PutFile ( {{"Comma Separated Value Files (*.csv)","*.csv"}} , "Export to text file (CSV)" , , .f. )
if len(alltrim(filename)) == 0
return nil
endif
if at(".csv",lower(filename)) > 0
if .not. right(lower(filename),4) == ".csv"
filename := filename + ".csv"
endif
else
filename := filename + ".csv"
endif
if file(filename)
if .not. msgyesno("Are you sure to overwrite?","Export to text file (CSV)")
return nil
endif
endif
fhandle := fcreate(filename)
if fhandle < 0
msgstop("File "+filename+" could not be created!")
return nil
endif
for count1 := 1 to len( aInData )
asize( aclinedata, 0 )
lineData := aInData[ count1 ]
for count2 := 1 to len( linedata )
do case
case ValType(linedata[count2]) == "N"
cdata := LTrim( Str( linedata[count2] ) )
case ValType(linedata[count2]) == "D"
cdata := dtoc( linedata[count2])
case ValType(linedata[count2]) == "L"
cdata := iif(linedata[count2],"TRUE","FALSE")
otherwise
cdata := linedata[count2]
endcase
aadd(aclinedata,cdata)
next count2
aadd(adata,aclone(aclinedata))
next count1
for count1 := 1 to len(adata)
cline := ''
aline := adata[count1]
for count2 := 1 to len(aline)
cline := cline + '"' + _parsequote(aline[count2]) + '"'
if .not. count2 == len(aline)
cline := cline + ','
endif
next count2
cline := cline + linebreak
fwrite(fhandle,cline)
next count1
if fclose(fhandle)
msginfo("Exported Successfully!")
else
msgstop("Error in saving!")
endif
return nil
function _parsequote(cdata)
local i := 0
local cout := ""
for i := 1 to len(cdata)
if sub str(cdata,i,1) == '"'
cout := cout + sub str(cdata,i,1) + '"'
else
cout := cout + sub str(cdata,i,1)
endif
next i
return cout
Remove the space between sub and str.