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
DeepikareddyDeepikareddy 

How to read a imported file in the Visualforce page

This file is sample  tab Elimated Text file .. how to read this file and add to the List and render  in visualforce page in Salesforce ..!
User-added image Apex Class:
public class Textfileimport{

 public blob Selectedfile{get;set;}
 public list<Studentdetails> Lststudentdetails{get;set;}
 
public Textfileimport(){
Lststudentdetails= new list<Studentdetails>();
}

public void add(){

}
 //wrapperclass
 public class Studentdetails{
 public string studentname{get;set;}
 public string studentId{get;set;}
 public integer studentyear{get;set;}
  }
 
}

visualforce page:
<apex:page Controller="Textfileimport"  >
    <apex:messages />
    <apex:form id="theForm">
  
     <apex:inputFile value="{!Selectedfile}"  id="testhide" />
     
      <apex:commandButton value="ImportValues" action="{!add}"/>
      
      <apex:pageBlock >
      
      <apex:pageblockTable value="{!Lststudentdetails}" var="a" >
       <apex:column headerValue="Name">{!a.studentname} </apex:column>
         <apex:column headerValue="Id" >{!a.studentId}</apex:column>
         <apex:column headerValue="Year">{!a.studentyear}</apex:column>
       
       </apex:pageblockTable>
      </apex:pageBlock>
       
       
      
     </apex:form>
</apex:page>


how to add to list and display in the visualforce page ....!

​​​​​​​Please share any related links it might be help full thank you...!
 
sfdcBahusfdcBahu
try this and please mark if the solution works.

public class Textfileimport{

 public blob Selectedfile{get;set;}
 public list<Studentdetails> Lststudentdetails{get;set;}
 
public Textfileimport(){
Lststudentdetails= new list<Studentdetails>();
}

public void add(){

  string fileContent = Selectedfile.toString();
  
  List<string> Rows = fileContent.split('\n');
  
  for(String Row: Rows)
  {
    
    List<string> rowcolumns = Row.split('\t');
    
    Studentdetails sd = new Studentdetails();
    sd.studentname = rowcolumns[0];
    sd.studentId = rowcolumns[1];
    sd.studentyear = rowcolumns[2];
    
    Lststudentdetails.add(sd);
    
  }


}
 //wrapperclass
 public class Studentdetails{
 public string studentname{get;set;}
 public string studentId{get;set;}
 public integer studentyear{get;set;}
  }
 
}