You need to sign in to do that
Don't have an account?

Upload Csv File and Conveted into pageblock table
Hi All,
Please find below code ,which is working for only one object like Account , but i want this process for all object ,
please help me how to do
<apex:page controller="csvFileReaderController">
<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:SelectList value="{!val}" size="1">
<apex:selectOptions value="{!Name}"></apex:selectOptions>
</apex:SelectList>
</apex:form>
</apex:page>
apex:
Public with sharing class csvFileReaderController {
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 csvFileReaderController(){
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);
}
}
public String val {get;set;}
public List<SelectOption> getName()
{
List<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values();
System.debug('@@@@@@@@@'+gd);
List<SelectOption> options = new List<SelectOption>();
for(Schema.SObjectType f : gd)
{
options.add(new SelectOption(f.getDescribe().getLabel(),f.getDescribe().getLabel()));
}
System.debug('$$$$$$$$$'+options);
return options;
}
}
Thanks
Satheesh
Please find below code ,which is working for only one object like Account , but i want this process for all object ,
please help me how to do
<apex:page controller="csvFileReaderController">
<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:SelectList value="{!val}" size="1">
<apex:selectOptions value="{!Name}"></apex:selectOptions>
</apex:SelectList>
</apex:form>
</apex:page>
apex:
Public with sharing class csvFileReaderController {
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 csvFileReaderController(){
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);
}
}
public String val {get;set;}
public List<SelectOption> getName()
{
List<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values();
System.debug('@@@@@@@@@'+gd);
List<SelectOption> options = new List<SelectOption>();
for(Schema.SObjectType f : gd)
{
options.add(new SelectOption(f.getDescribe().getLabel(),f.getDescribe().getLabel()));
}
System.debug('$$$$$$$$$'+options);
return options;
}
}
Thanks
Satheesh