+ Start a Discussion
shrey.tyagi88@tcs.comshrey.tyagi88@tcs.com 

upload data in salesforce custom object from an excel sheet!!!! Plz help

Hi all, I have to upload data from an excel sheet into a custom object of salesforce .I've never done anything like this before.Please tell me , how to start with it? Thanks.
Chamil MadusankaChamil Madusanka

Try this code. This code is from on of blog. But I couldn't find thee blog link. I have the code from that blog. I will post the code here. This uploading code is support for CSV files.

 

<apex:page sidebar="false" controller="FileUploader">
   <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"> <b>Note: Please use the standard template to upload Accounts. <a href="{!URLFOR($Resource.UploadTemplate)}" target="_blank"> Click here </a> to download the template. </b> </font>
             </center>  
      
     <apex:commandButton value="Access" action="{!fileAccess}"/>
      <apex:pageblocktable value="{!uploadedAccounts}" var="acc" rendered="{!NOT(ISNULL(uploadedAccounts))}">
          <apex:column headerValue="Account Name">
              <apex:outputField value="{!acc.Name}"/>
          </apex:column>
          <apex:column headerValue="Shipping Street">
              <apex:outputField value="{!acc.ShippingStreet}"/>
          </apex:column>
          <apex:column headerValue="Shipping City">
              <apex:outputField value="{!acc.ShippingCity}"/>
          </apex:column>
          <apex:column headerValue="Shipping State">
              <apex:outputField value="{!acc.ShippingState}"/>
          </apex:column>
         <!-- <apex:column headerValue="Shipping Postal Code">
              <apex:outputField value="{!acc.ShippingPostalCode}"/>
          </apex:column>
          <apex:column headerValue="Shipping Country">
              <apex:outputField value="{!acc.ShippingCountry}"/>
          </apex:column>-->
      </apex:pageblocktable> 
      
      </apex:pageBlock>       
   </apex:form>   
</apex:page>

 

public class FileUploader 
{

    public PageReference fileAccess() {
        return null;
    }


public FileUploader(){

}
    public string nameFile{get;set;}
    public Blob contentFile{get;set;}
    String[] filelines = new String[]{};
    List<Account> accstoupload;
    
    public Pagereference ReadFile()
    {
        nameFile=contentFile.toString();
        filelines = nameFile.split('\n');
        accstoupload = new List<Account>();
        for (Integer i=1;i<filelines.size();i++)
        {
            String[] inputvalues = new String[]{};
            inputvalues = filelines[i].split(',');
            
            Account a = new Account();
            a.Name = inputvalues[0];
            a.ShippingStreet = inputvalues[1];       
            a.ShippingCity = inputvalues[2];
            a.ShippingState = inputvalues[3];
          //  a.ShippingPostalCode = inputvalues[4];
           // a.ShippingCountry = inputvalues[5];

            accstoupload.add(a);
        }
        try{
        insert accstoupload;
        }
        catch (Exception e)
        {
            ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured. Please check the template or try again later');
            ApexPages.addMessage(errormsg);
        }    
        return null;
    }
    
    public List<Account> getuploadedAccounts()
    {
        if (accstoupload!= NULL)
            if (accstoupload.size() > 0)
                return accstoupload;
            else
                return null;                    
        else
            return null;
    }
    
  /* public pageReference fileAccess(){
         Document lstDoc = [select id,name,Body from Document where name = 'test'];
         
         System.Debug('DOC NAME :: '+lstDoc.name);   
         System.Debug('DOCBODY :: '+lstDoc.Body);  
         
         
         return null; 
    }  */
    
    public static testMethod void testReadFile1() {
    
    Document lstDoc = [select id,name,Body from Document where name = 'test'];
         
        // System.Debug('DOC NAME :: '+lstDoc.name);   
         //System.Debug('DOCBODY :: '+lstDoc.Body);  
       FileUploader file=new FileUploader();
       Blob content= lstDoc.Body;
     file.nameFile=content.toString();
    }
    
    public static testMethod void testReadFile2() {
        
    }
          
}

 

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

Chamil MadusankaChamil Madusanka

Here is the blog link

 

http://www.forcetree.com/2010/08/read-and-insert-records-from-csv-file.html

 

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

eugene_bmeugene_bm

You can also try to use the Apex Data Loader v.23.

Its another application that you can use to upload multiple data into salesforce.

 

"Apex Data Loader is an application for the bulk import or export of data. Use it
to insert, update, delete, or extract Salesforce records. Apex Data Loader
can move data into or out of any Salesforce object."

 

- Excerpt from data loader help

 

 

Check out this URL:
http://wiki.developerforce.com/page/Apex_Data_Loader

 

 

 

 

 

 

PavanReddyPavanReddy

its nice post ...Thanq u so much its helped me lot n lot ...bunch of thanks to u  chamil........