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
V MANJUV MANJU 

How to display the CSV file data on VF page & Also how to Insert the CSV file(records) into object ??

I Want code plz
V MANJUV MANJU
Rockzz am getting error like :Error: csvFileReaderController Compile Error: Illegal assignment from LIST<SObject> to LIST<account> at line 11 column 5
SFDC_DevloperSFDC_Devloper
Hi Manju...   try below code....

Csv file format used in this example:
User-added image

Output screen shot:
User-added image

Visualforce page:
<apex:page controller="csvFileReader">
    <apex:form >  <!-- csv reader demo -->
        <apex:pageBlock >
            <apex:panelGrid columns="2" >
                  <apex:inputFile value="{!csvFileBody}"  filename="{!csvAsString}"/>
                  <apex:commandButton value="Read csv" action="{!readcsvFile}"/>
            </apex:panelGrid>
        </apex:pageBlock>
        <apex:pageBlock >
           <apex:pageblocktable value="{!sObjectList}" var="rec">
              <apex:column value="{!rec.name}" />
              <apex:column value="{!rec.AccountNumber}" />
              <apex:column value="{!rec.Accountsource}" />
              <apex:column value="{!rec.Type}" />
              <apex:column value="{!rec.Website}" />
        </apex:pageblocktable>
     </apex:pageBlock>
   </apex:form>
</apex:page>

Controller:

Public with sharing class csvFileReader{
public Blob csvFileBody{get;set;}
Public string csvAsString{get;set;}
Public String[] csvfilelines{get;set;}
Public String[] inputvalues{get;set;}
Public List<string> fieldList{get;set;}
Public List<account> sObjectList{get;set;}
  public csvFileReader(){
    csvfilelines = new String[]{};
    fieldList = New List<string>();
    sObjectList = New List<sObject>();
  }

  Public void readcsvFile(){
       csvAsString = csvFileBody.toString();
       csvfilelines = csvAsString.split('\n');
       inputvalues = new String[]{};
       for(string st:csvfilelines[0].split(','))
           fieldList.add(st); 
     
       for(Integer i=1;i<csvfilelines.size();i++){
           Account accRec = new Account() ;
           string[] csvRecordData = csvfilelines[i].split(',');
           accRec.name = csvRecordData[0] ;           
           accRec.accountnumber = csvRecordData[1];
           accRec.Type = csvRecordData[2];
           accRec.website = csvRecordData[3];
           accRec.AccountSource = csvRecordData[4];                                                                             
           sObjectList.add(accRec); 
       }
  }
}


Inserting, Updating, or Deleting Data Using Data Loader:
http://www.salesforce.com/us/developer/docs/dataLoader/Content/inserting_updating_or_deleting_data.htm



Thanks,
Rockzz
SFDC_DevloperSFDC_Devloper
Hi Manju,

   My side its working fine...I am not getting any error ...

Thanks,
Rockzz
SFDC_DevloperSFDC_Devloper
Hi Manju,

Please check below links,

https://developer.salesforce.com/forums/ForumsMain?id=906F0000000981XIAQ

https://developer.salesforce.com/forums/ForumsMain?id=906F00000008vO5IAI


If this solves your problem, kindly mark it as the best answer.

Thanks,
Rockzz
SoundarSoundar
Dear Rockzz,

I am also similiar requirement, but small change is there. I want to insert record into the object witout using an apex (only make it in visualforce page) . Can you please help.

Thanks In Advance,