Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Close All
Clear All
lcFieldString = ''
lcMemo = ''
Use Getfile('dbf', 'Select DBF') && Prompts for table to be used.
lcTargetFile = Putfile('txt', 'Specify Target txt File') && Prompts for target file name.
CSVExport(lcTargetFile)
Function CSVExport(tcFilename as String, tcAlias As String)
tcAlias = Evl(tcAlias,Alias())
tcFilename = Evl(tcFilename, ForceExt(Dbf(tcAlias),"txt"))
If ADir(laDummy,tcFilename)>0
Error 'File already exists'
EndIf
If !Used(tcAlias)
Error 'No workarea selected or specified'
EndIf
Select (tcAlias)
lnFieldCount = Afields(laGetFields) && Builds array of fields from the
&& selected table.
*!* Prompt for Output file and use Low-Level functions
*!* to create it.
lnFH = Fcreate(tcFilename)
For lnCount = 1 To lnFieldCount
lcFieldString = lcFieldString + Lower(laGetFields(lnCount, 1))
If lnCount < lnFieldCount && Determines if the last field wasprocessed.
lcFieldString = lcFieldString + ','
Endif
Endfor
Fputs(lnFH, lcFieldString) && Writes string to the text file.
lcFieldString = ''
*!* Starts scanning the table and converts the fields
*!* values according to their types
Scan
* Whatever suits you here, standard for CSV should be american formats.
Set Century On
Set Mark To "/"
Set Date AMERICAN
Set Hours To 24
Set Point To "."
Set Separator To ""
For lnCount = 1 To lnFieldCount
lcType = laGetFields(lnCount, 2)
If Not lcType $ 'G' && Don't try to turn a general field into a string
lcString = Evaluate(laGetFields(lnCount, 1))
Endif
Do Case
Case Isnull(lcString)
lcString = '.null.'
lcType = 'U' && no delimiter for char,varchar or memo field, .NULL. is null without quotes.
Case lcType $ 'CMV'
* nothing to do
Case lcType $ 'IDTFBY' && process most field types via TRANSFORM
lcString = Transform(lcString)
Case lcType = 'L' && process logic fields
lcString = Iif(lcString,'.t.','.f.')
Case lcType = 'N' && process numeric fields
lcString = Str(lcString, laGetFields(lnCount, 3), laGetFields(lnCount, 4))
Case lcType = 'G' && process general fields
lcString = 'Gen' && that means don't process them, export the word 'Gen' instead
Case lcType $ 'QW' && process blob and varbinary fields
lcString = '0h'+Transform(lcString) && empty fields have 0h prefix only
Otherwise
lcString = ["UNKNOWN FIELD TYPE!"] && should never happen
Endcase
If lcType $ "MCV"
* csv convention: double double quotes
lcFieldString = lcFieldString + ["] + Strtran(lcString,["],[""]) + ["]
Else
lcFieldString = lcFieldString + lcString
Endif
If lnCount < lnFieldCount && Determines if the last field was processed.
lcFieldString = lcFieldString + ','
Endif
Endfor
Fputs(lnFH, lcFieldString) && Writes string to the text file.
lcFieldString = ''
Endscan
Fclose(lnFH)
EndFunc