Skip to content

Instantly share code, notes, and snippets.

@KorbenC
Forked from douglascayers/ApexCsvExample.java
Created April 10, 2017 07:11
Show Gist options
  • Save KorbenC/0e17d73949b327d83b0add8edf912509 to your computer and use it in GitHub Desktop.
Save KorbenC/0e17d73949b327d83b0add8edf912509 to your computer and use it in GitHub Desktop.
Apex CSV Example. Note the use of String.escapeCsv() method
String csv = 'Id,Name\n';
for ( List<Account> accts : [ SELECT id, name FROM Account LIMIT 10 ] ) {
for ( Account acct : accts ) {
csv += acct.id + ',' + acct.name.escapeCsv() + '\n';
}
}
ContentVersion file = new ContentVersion(
title = 'accounts.csv',
versionData = Blob.valueOf( csv ),
pathOnClient = '/accounts.csv'
);
insert file;
System.debug( file );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment