Apex CSV Example. Note the use of String.escapeCsv() method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Code snippet for my blog post about the many ways to export Salesforce data to CSV: https://douglascayers.com/2016/03/20/salesforce-easy-ways-to-export-data-as-csv/