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
Vivekh88Vivekh88 

Import Excel in VF page for different fields

Hi,
I am having a requirement for importing datas through excel for Event object. I have 6 Custom fields,
Time1, Topic1, duration1 and Time2, topic2, duration2.
My excel table will be like this,
Time           Topics     Duration
12/3/2016     text1       60
10/3/2016     text2       45
i have written a class and vf for this scenario,
Apex Class:
public with sharing class ImportTopics
{
public transient Blob contentFile{get;set;}
public String nameFile{get;set;}
private String[] filelines{get;set;}
Public Event Event1{get;set;}
public String currentRecordId {get;set;}
public List<Event> acclist{get;set;}
    public ImportTopics(ApexPages.StandardController controller) {
    filelines = new String[]{};
    acclist = New List<Event>();   
    Event1 = New Event();
    Event1 = (Event)controller.getRecord();
    }
    public void readFile(){
           try{
               nameFile = contentFile.toString(); 
               filelines = nameFile.split('\n');
               for(Integer i=1;i<filelines.size();i++){
                   Event accObj = new Event();
                   string[] csvRecordData = filelines[i].split(',');
                   accObj.Id = Event1.id;
                   accObj.StartDateTime = System.now();
                   String datetimevalue = csvRecordData[0];
                   DateTime dt = DateTime.parse(datetimevalue);
                   String dtConverted = dt.format('yyyy-MM-dd hh:mm:ss');
                   
                   //fields to be updated with first rows of excel
                   accObj.Time_1__c = Datetime.ValueOf(dtConverted);
                   accObj.Proposed_Agenda_Topics1__c = csvRecordData[1];
                   accObj.Estimated_time_1__c = = csvRecordData[2];

                   //fields to be updated with second rows of excel
                   accObj.Time_2__c = Datetime.ValueOf(dtConverted);
                   accObj.Proposed_Agenda_Topics2__c = csvRecordData[1];
                   accObj.Estimated_time_2__c = = csvRecordData[2];
                   
                   
                   acclist.add(accObj); 
                    }
                  
            }
            catch(Exception e){
                ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured reading the CSV file: '+e.getMessage());
                ApexPages.addMessage(errormsg);
         }
         Upsert acclist;   
      }      
}


VF page:
<apex:page sidebar="false" standardStylesheets="false" standardcontroller="Event" extensions="ImportTopics">
     <apex:form id="theForm">
     <apex:pageBlock >
     <apex:pagemessages />
                 <apex:inputFile value="{!contentFile}" id="theFile" filename="{!nameFile}"/> <br/>
                 <apex:commandButton action="{!ReadFile}" value="Import topics" id="theButton" style="width:95px;"/>
     </apex:pageBlock>
     </apex:form>   
</apex:page>
The problem is it is updating only the first rows with first set of fields. Can anyone help me with this.?

Thanks
Vivekh