function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

how to export data in apex class to excel?


I am trying to export the data to excel sheet from apex class.

i know that we can do the export from vocabularies page.but i want that export to be done from apex class.

how can i achieve this?Please help me out.


This is one of the way but not 100% accurate. make sure account names should not contains any commas


Document objD = new Document();
List<Account> lstA = [select id,name from Account]; = 'testFile.csv';
String filecontent;
fileContent = 'Id' + ',' +'Name' +'\n';
for(Account objA : lstA){
 fileContent += + ',' +  objA.Name + '\n';  
objD.body = blob.valueof(fileContent);
objD.folderId = UserInfo.getUserId();
insert objD;
basavaraj virupakshappa 3basavaraj virupakshappa 3
how to export data in apex class to excel?