Activity Forums Salesforce® Discussions How to Import Contacts from Excel Sheet Using Salesforce visualforce Page?

  • Gourav

    October 3, 2016 at 1:27 pm

    Hi Tanu,

    You can use CSV parser for this..plese refer the below example code where I have created one Visualforce page from where I am uplaoding the excel file .csv and extracting the records data and mapping with object columns

    please check and let me know if it helps you


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

    <apex:pageBlocktable value="{!mapUnmatchedSchedules}" var="mapID" title="Unmathed Lines of CSV file">
    <apex:column headervalue="Opportunity Id">
    <apex:column headervalue="Oportunnity Name">
    <apex:column headervalue="Month">
    <apex:column headervalue="Year">
    <apex:column headervalue="Actualized Amount">

    public with sharing class UploadOpportunityScheduleLineItem

    // Global variables
    public string nameFile{get;set;}
    public Blob contentFile{get;set;}
    List<Schedule__c> lstScheduleToUpdate = new List<Schedule__c>();
    public Schedule__C objSchedule{get;set;}
    //String array for taking csv data by line.
    String[] filelines = new String[]{};
    //string array for taking csv columns.
    String[] fileColumns = new String[]{};

    //set for storing all id's from csv.
    set<String> opptoupload{get;set;}
    //map for storing data from csv.
    public map<String,String> mapOppIDMonthYearToAccAmount{get;set;}
    public map<String, List<String>> mapUnmatchedSchedules{get;set;}

    //Main constructor
    public UploadOpportunityScheduleLineItem()
    //Initalizing required objects.
    objSchedule = new Schedule__c();
    opptoupload = new set<String>();
    mapOppIDMonthYearToAccAmount = new map<String,String>();
    mapUnmatchedSchedules = new map<String, List<String>>();
    //Method to read file content and check extension and file format.
    public Pagereference ReadFile()
    //If without selecting csv file you clicked on upload it will give error message.
    if(nameFile == null)
    ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'You should select csv file to upload');
    return null;
    //Taking file extension.
    String extension = nameFile.substring(nameFile.lastIndexOf('.')+1);
    //Checking if file extension is .csv.
    if(extension == 'csv' ||extension == 'CSV')
    //Spliting by new line
    filelines = nameFile.split('\n');
    //Spliting values by (,) for checking coloumn size
    fileColumns = filelines[0].split(',');
    //If column size is 5 then only it will proceed.
    if(fileColumns.size() ==5)
    for (Integer i=1;i<filelines.size();i++)
    String[] inputvalues = new String[]{};
    inputvalues = filelines[i].split(',');

    lstScheduleToUpdate = new List<Schedule__c>();
    for(Schedule__c objSchedule : [select Opportunity__r.Id ,Month__c,Year__c,
    Actualized_Amount__c from Schedule__c where
    Opportunity__c IN :opptoupload])
    String str = objSchedule.Opportunity__r.Id;
    String str1;
    str1 = str.substring(0, 15);
    if(mapOppIDMonthYearToAccAmount.containsKey(str1 + objSchedule.Month__c +objSchedule.Year__c))

    objSchedule.Actualized_Amount__c = decimal.valueOf(mapOppIDMonthYearToAccAmount.get(str1 + objSchedule.Month__c +objSchedule.Year__c).trim());
    mapUnmatchedSchedules.remove(str1 + objSchedule.Month__c +objSchedule.Year__c);
    //Checking if list is not empty then updating.
    update lstScheduleToUpdate;
    ApexPages.Message errormsg = new ApexPages.Message(,'File uploaded successfully');

    return null;
    ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'There should be 5 columns in csv file');
    return null;
    return null;
    //If file is not csv type then it will give error message.
    ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'File type should be csv type');
    return null;

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos