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
AKS018AKS018 

Read User Uploaded Excel File and to Insert the Excel file data to salesforce

Hi,

 

         How to read the Excel file and then to insert all the data records in to salesforce object.

 

Suppose I have an Excel file with Account object information. Now I have to upload that file to visualforce page. Then after  to read all the records in Excel and to store in Account object. All these has to implement in the visualforce and Apex class.

 

 

Can anyone please help me how to approach this type of task and also provide me any Examples.

gautam_singhgautam_singh

Hi ,


I had an equivalent requiremnt wherin I had to read an CSV File and store that in the Lead Object provided all the task be done by the End User by clicking on Browse and Selecting the csv  File. I am sharing the code here ...


Important :

Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You .

Code ..

public class FileUploader 
{
    public string nameFile{get;set;}
    public Blob contentFile{get;set;}
    String[] filelines = new String[]{};
    List<Account> accstoupload;
    
    public Pagereference ReadFile()
    {
       nameFile=contentFile.toString();
       //nameFile= EncodingUtil.base64Encode(contentFile);
       //nameFile = EncodingUtil.urlEncode(nameFile, 'UTF-8');
       List<Id> idList =  new List<Id>();
       Map<String,string> map1 = new Map<string,string>();
       List<List<String>> parsedFields = parseCSV(nameFile,true);
       system.debug('size is '+parsedFields.size());
       integer k=0;
       for(List<String> e1: parsedFields){
       if(k < 5){
           system.debug('val is '+e1[0]);
        }   
        k++;
          map1.put(e1[0],e1[1]);
          idList.add(Id.valueOf(e1[0]));
       }
       system.debug('Mapval is '+map1.get('00QJ00000057xydMAA'));
      List<Lead> updateLeads = new List<Lead>();
      integer i=0;
       for(Lead le:[select id,Site_Feasible_For_Solar__c from Lead where id IN:idList]){
           string siteFeasible = map1.get(le.id);
          // try{
               le.Site_Feasible_For_Solar__c  = siteFeasible;
               le.Data_Stage__c = 'Site Verified';
               le.site_verified_date__c = system.today();
               updateLeads.add(le);
          /* }catch(ListException lee) {
                System.debug('The following exception has occurred: ' + lee.getMessage());
                PageReference pageRef = new PageReference('/apex/fileUploader');
                ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,lee.getMessage());
                ApexPages.addMessage(myMsg);
                pageRef.setRedirect(true);
                return pageRef;
                
            }*/
           i++;
       }
        system.debug('size is is '+updateLeads.size());
        try{
           update updateLeads;
         }catch(DMLException er) {
             System.debug('The following exception has occurred: ' + er.getMessage());
                PageReference pageRef1 = new PageReference('/apex/fileUploader');
                ApexPages.Message myMsg1 = new ApexPages.Message(ApexPages.Severity.ERROR,er.getMessage());
                ApexPages.addMessage(myMsg1);
                pageRef1.setRedirect(true);
                return pageRef1;
         }
        ApexPages.Message myMsg2 = new ApexPages.Message(ApexPages.Severity.INFO,'File Uploaded Successfully');
        ApexPages.addMessage(myMsg2);    
       return null;
    }  
    
    
    public static List<List<String>> parseCSV(String contents,Boolean skipHeaders) {
    List<List<String>> allFields = new List<List<String>>();

    // replace instances where a double quote begins a field containing a comma
    // in this case you get a double quote followed by a doubled double quote
    // do this for beginning and end of a field
    contents = contents.replaceAll(',"""',',"DBLQT').replaceall('""",','DBLQT",');
    // now replace all remaining double quotes - we do this so that we can reconstruct
    // fields with commas inside assuming they begin and end with a double quote
    contents = contents.replaceAll('""','DBLQT');
    // we are not attempting to handle fields with a newline inside of them
    // so, split on newline to get the spreadsheet rows
    List<String> lines = new List<String>();
    try {
        lines = contents.split('\n');
    } catch (System.ListException e) {
        System.debug('Limits exceeded?' + e.getMessage());
    }
    system.debug('Line num i s'+lines.size());
    Integer num = 0;
    for(String line : lines) {
        // check for blank CSV lines (only commas)
        if (line.replaceAll(',','').trim().length() == 0) break;
        
        List<String> fields = line.split(',');  
        List<String> cleanFields = new List<String>();
        String compositeField;
        Boolean makeCompositeField = false;
        for(String field : fields) {
            if (field.startsWith('"') && field.endsWith('"')) {
                cleanFields.add(field.replaceAll('DBLQT','"'));
            } else if (field.startsWith('"')) {
                makeCompositeField = true;
                compositeField = field;
            } else if (field.endsWith('"')) {
                compositeField += ',' + field;
                cleanFields.add(compositeField.replaceAll('DBLQT','"'));
                makeCompositeField = false;
            } else if (makeCompositeField) {
                compositeField +=  ',' + field;
            } else {
                cleanFields.add(field.replaceAll('DBLQT','"'));
            }
        }
        
        allFields.add(cleanFields);
    }
    if (skipHeaders) allFields.remove(0);
    return allFields;       
} 

@isTest(SeeAllData=true)
        public static void testUploadFile()
        {
        
        Lead LL = [select id from Lead limit 1];
            FileUploader  EWS = new FileUploader();
            EWS.nameFile = 'test.csv';
            String csvFile = 'Id,Site_Feasible_For_Solar__c\n';
csvFile += LL.id+',Yes\n';
csvFile += LL.id+',"Yes"\n';
csvFile += LL.id+',"Yes",11\n';




Blob csvBlob = Blob.valueOf(csvFile );
            
            EWS.contentFile =csvBlob;
            EWS.ReadFile();
                  
        
        
        }        
}

  and Visualforce Page

<apex:page sidebar="false" controller="FileUploader">
<apex:message />
   <apex:form >
      <apex:sectionHeader title="Upload data from CSV file"/>
      <apex:pagemessages />
      <apex:pageBlock >
             <center>
              <apex:inputFile value="{!contentFile}" filename="{!nameFile}" /> <apex:commandButton action="{!ReadFile}" value="Upload File" id="theButton" style="width:70px;"/>
              <br/> <br/> <font color="red">  </font>
             </center>  
      
      
     
      
      </apex:pageBlock>       
   </apex:form>   
</apex:page>
            

 

David Roberts 4David Roberts 4
I used this method (thank you!) but found it didn't handle fields that included "newline". The Blob must have some indication that delimits lines other than the \n char. Anyone know how to do this? I had to pre-process my spreadsheet before saving to csv to remove the line feeds.
{variation at http://www.forcetree.com/2010/08/read-and-insert-records-from-csv-file.html]