Export Data To .CSV File
Magic Version: Generic. This hasn't really changed in a LONG time!
A default magic export program will generate a "columnar" text file. That is to say, each field that is output will be output to the same horizontal column number, and there will be a lot of spaces output to pad each field. If you would like to output a .CSV file, do the following.
These instructions assume that you are starting with a default magic export program.
- Create a virtual field to use for output, long enough to hold the largest CSV record.
- Delete the existing "Class=1 / Detail / Text-based" form
- Create a new "Class=1 / Detail / Text-based" form, wide enough to hold the virtual field.
- Put the virtual field on the form.
- In record suffix, build the CSV output by concatenating each field into the virtual field.
- Output the form to the export I/O entry.
As an example, let's say that you would like to create a .CSV file with the following fields:
| Name |
Alpha |
30 |
|
| Date Of Birth |
Date |
4 |
'MM/DD/YYYY' |
| Age |
Number |
3 |
|
| Zip Code |
Alpha |
10 |
|
The record main will look like this:
Select Real 1 Name ("A")
Select Real 2 Date Of Birth ("B")
Select Real 3 Age ("C")
Select Real 4 Zip Code ("D")
Select Virt 1 vFor Output ("E")
The record suffix will look like this:
Update E Expr=Trim(A) & ','
Update E Expr=Trim(E) & Trim(dstr(B,'MM/DD/YYYY')) & ','
Update E Expr=Trim(E) & Trim(str(C,'3')) & ','
Update E Expr=Trim(E) & Trim(D)
Outpuf Form=3 I/O=1
If you need quotes around strings, you will have to concatenate them as necessary. The previous example would then look like this:
Update E Expr='"' & Trim(A) & '",'
Update E Expr=Trim(E) & Trim(dstr(B,'MM/DD/YYYY')) & ','
Update E Expr=Trim(E) & Trim(str(C,'3')) & ','
Update E Expr=Trim(E) & '"' & Trim(D) & '"'
Outpuf Form=3 I/O=1
Categories
CategoryHowTo
There are no comments on this page. [Add comment]