user1327073
user1327073

Reputation: 1084

how to export a foxpro cursor to a CSV file?

In visual foxpro, i have a cursor which is the result of a sql query, when i export the content of that cursor to a csv file using the statement :

 COPY TO "c:\test.csv" type DELIMITED

all data is messed up, i do not pecify any delimiter so basically foxpro takes the default, which is every column in that cursor. bow when i run the same command to an xls file, and then convert it to a csv file...it works very well:

     COPY TO "c:\test.xls" type XL5

anyone has had such issue, any one still using foxpro and doing stuff like those?

Upvotes: 1

Views: 23051

Answers (2)

Gene S
Gene S

Reputation: 2773

Personally I never liked the built-in DBF to CSV converters. They always seemed to do things I did not want them to do. So I just wrote my own. Here is some code to get you started.

LOCAL lnFields

SELECT DBF
lnFieldCount = AFIELDS(laFields)
lnHandle = FOPEN("filename.csv", 1)
ASSERT lnHandle > 0 MESSAGE "Unable to create CSV file"

SCAN
    lcRow = ""
    FOR lnFields = 1 TO lnFieldCount
    IF INLIST(laFields[lnFields,2], 'C', 'M')
            lcRow = lcRow + IIF(EMPTY(lcRow), "", ",") + '"' + ;
                            STRTRAN(EVALUATE(laFields[lnFields,1]),'"', '""') + '"'
        ELSE
            lcRow = lcRow + IIF(EMPTY(lcRow), "", ",") + ;
                            TRANSFORM(EVALUATE(laFields[lnFields,1]))
        ENDIF
    ENDFOR

    FWRITE(lnHandle, lcRow)
ENDSCAN
FCLOSE(lnHandle)

Upvotes: 1

Tamar E. Granor
Tamar E. Granor

Reputation: 3937

Have you tried using TYPE CSV in the COPY TO command?

Upvotes: 4

Related Questions