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
VSK98VSK98 

Upload CSV File

Upload .csv file in the vf page
sandy sfdcsandy sfdc
Hi Siva

Try this,
 
<apex:page controller="Upload_CSV_File_Controller">
    <apex:form >
        <apex:pagemessages />
        <apex:pageBlock >
            <apex:pageBlockSection columns="4"> 
                  <apex:inputFile value="{!csvFileBody}"  filename="{!csvAsString}"/>
                  <apex:commandButton value="Upload" action="{!UploadCsvFile}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
        <apex:pageBlock >
           <apex:pageblocktable value="{!LeadList}" var="ld">
              <apex:column value="{!ld.FirstName}" />
              <apex:column value="{!ld.LastName}" />
              <apex:column value="{!ld.Company}" />
              <apex:column value="{!ld.Phone}" />
             </apex:pageblocktable>
     </apex:pageBlock>
   </apex:form>
</apex:page>

Controller:

public with sharing class Upload_CSV_File_Controller {
public Blob csvFileBody{get;set;}
public string csvAsString{get;set;}
public String[] csvFileLines{get;set;}
public List<Lead> LeadList{get;set;}
  public Upload_CSV_File_Controller(){
    csvFileLines = new String[]{};
    LeadList = New List<Lead>(); 
  }
  
  public void UploadCsvFile(){
       try{
           csvAsString = csvFileBody.toString();
           csvFileLines = csvAsString.split('\n'); 
            for(Integer i=1;i<csvFileLines.size();i++){
               Lead LeadObj = new Lead() ;
               string[] csvRecordData = csvFileLines[i].split(',');
               LeadObj.FirstName = csvRecordData[0] ;             
               LeadObj.LastName = csvRecordData[1];
               LeadObj.Company = csvRecordData[2];
               LeadObj.Phone = csvRecordData[3];   
               Leadlist.add(LeadObj);   
           }
        insert Leadlist;
        }
        catch (Exception e)
        {
            ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured while importin data Please make sure input csv file is correct');
            ApexPages.addMessage(errorMessage);
        }  
  }

}

 
VSK98VSK98
Hi Sandy,

It's very nice.................but actually i  have been trying the upload the multiple files using single upload button in UI...........