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 import data present in alist to excel file using apex coding

I have a batch class where in the execute method i will get a list of records. Now in final method i need to send a mail and attach the above list as an excel file. All the data in the list should be loaded into excel file. I tried but its not working. any sample code on how to convert a list data file into an excel sheet?


Hey sunny,


You can use this code sample , and for more information you can check out this blog



Code Sample


public class processCSV {



             public blob BlobFile{get;set;}
             headersList = new list<string>();
             public string RecordsInTheFile {get;set;}


             public void processingFile(){


                         //file processing
                        RecordsInTheFile = BlobFile.tostring();//taking blob file to a string

                        headersList = RecordsInTheFile.split('\r\n');//splitting at new line

                        System.debug('value of excel file :::::::::' + headersList);



On page

<apex:inputFile value="{!BlobFile}" filename="{!RecordsInTheFile}" accept=".csv" />

<apex:commandButton action="{!processingFile}" value="Upload File" id="theButton" style="width:70px;" />