Most of the CSV export functionality.

Still to do: extracting the fields for each record.