Here is sample code to upload CSV file and insert CSV data in salesforce objects.
In this Code we are inserting data in contact object using standard template. First Column of CSV is Name, second column is LastName and third one is Title.
Visualforce Page
<apex:page controller="ReadandInsertController">
<apex:form >
<apex:pagemessages />
<apex:pageBlock >
<apex:actionRegion >
<apex:inputFile value="{!UploadedFileContent}" filename="{!FileName}" />
<apex:commandButton action="{!UploadRecords}" value="Upload File"/>
</apex:actionRegion>
<apex:pageblocktable value="{!uploadedContact}" id="Contable" var="con">
<apex:column headerValue="Name">
<apex:outputText value="{!con.FirstName }"/>
</apex:column>
<apex:column headerValue="LastName">
<apex:outputText value="{!con.lastName }"/>
</apex:column>
<apex:column headerValue="Title">
<apex:outputText value="{!con.Email}"/>
</apex:column>
</apex:pageblocktable>
</apex:pageBlock>
</apex:form>
</apex:page>
Apex Class
public class ReadandInsertController {
public string FileName{get;set;}
public Blob UploadedFileContent{get;set;}
List<string> fileRows;
public List<Contact > Contoupload {get;set;}
public Pagereference UploadRecords(){
fileRows = new list<string>();
FileName=UploadedFileContent.tostring();
fileRows = FileName.split('\n');
Contoupload = new List<Contact >();
for (Integer i=1;i<fileRows .size();i++){
String[] CSV = new String[]{};
CSV = fileRows [i].split(',');
Contact con = new Contact ();
con .FirstName = CSV [0];
con.LastName=CSV [1];
con.email=CSV [2];
Contoupload.add(con);
}
insert Contoupload;
return null;
}
public List<Contact > getuploadedContact(){
if (Contoupload!= NULL){
if (Contoupload.size() > 0){
return Contoupload;
}else{
return null;
}
}else{
return null;
}
}
}
| Read CSV and insert CSV records in salesforce |
Visualforce Page
<apex:page controller="ReadandInsertController">
<apex:form >
<apex:pagemessages />
<apex:pageBlock >
<apex:actionRegion >
<apex:inputFile value="{!UploadedFileContent}" filename="{!FileName}" />
<apex:commandButton action="{!UploadRecords}" value="Upload File"/>
</apex:actionRegion>
<apex:pageblocktable value="{!uploadedContact}" id="Contable" var="con">
<apex:column headerValue="Name">
<apex:outputText value="{!con.FirstName }"/>
</apex:column>
<apex:column headerValue="LastName">
<apex:outputText value="{!con.lastName }"/>
</apex:column>
<apex:column headerValue="Title">
<apex:outputText value="{!con.Email}"/>
</apex:column>
</apex:pageblocktable>
</apex:pageBlock>
</apex:form>
</apex:page>
Apex Class
public class ReadandInsertController {
public string FileName{get;set;}
public Blob UploadedFileContent{get;set;}
List<string> fileRows;
public List<Contact > Contoupload {get;set;}
public Pagereference UploadRecords(){
fileRows = new list<string>();
FileName=UploadedFileContent.tostring();
fileRows = FileName.split('\n');
Contoupload = new List<Contact >();
for (Integer i=1;i<fileRows .size();i++){
String[] CSV = new String[]{};
CSV = fileRows [i].split(',');
Contact con = new Contact ();
con .FirstName = CSV [0];
con.LastName=CSV [1];
con.email=CSV [2];
Contoupload.add(con);
}
insert Contoupload;
return null;
}
public List<Contact > getuploadedContact(){
if (Contoupload!= NULL){
if (Contoupload.size() > 0){
return Contoupload;
}else{
return null;
}
}else{
return null;
}
}
}