+ Start a Discussion
S SaiS Sai 

how to create Import and Export csv file using visualforce

HI all,

My requirment is Import and Export same file in in viusualforce page. Here i am importing csv data in same data i want to export csv for example 

User-added imageUser-added image


<apex:page controller="importDataFromCSVController">
    <apex:form >
        <apex:pagemessages />
        <apex:pageBlock >
            <apex:pageBlockSection columns="4">
                  <apex:inputFile value="{!csvFileBody}"  filename="{!csvAsString}"/>
                  <apex:commandButton value="Import Account" action="{!importCSVFile}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
        <apex:pageBlock >
           <apex:pageblocktable value="{!accList}" var="acc">
              <apex:column value="{!acc.name}" />
              <apex:column value="{!acc.AccountNumber}" />
              <apex:column value="{!acc.Type}" />
              <apex:column value="{!acc.Accountsource}" />
              <apex:column value="{!acc.Industry }" />
        </apex:pageblocktable>
     </apex:pageBlock>
   </apex:form>
</apex:page>



public class importDataFromCSVController {
public Blob csvFileBody{get;set;}
public string csvAsString{get;set;}
public String[] csvFileLines{get;set;}
public List<account> acclist{get;set;}
  public importDataFromCSVController(){
    csvFileLines = new String[]{};
    acclist = New List<Account>();
  }
  
  public void importCSVFile(){
       try{
           csvAsString = csvFileBody.toString();
           csvFileLines = csvAsString.split('\n');
            
           for(Integer i=1;i<csvFileLines.size();i++){
               Account accObj = new Account() ;
               string[] csvRecordData = csvFileLines[i].split(',');
               accObj.name = csvRecordData[0] ;            
               accObj.accountnumber = csvRecordData[1];
               accObj.Type = csvRecordData[2];
               accObj.AccountSource = csvRecordData[3];  
               accObj.Industry = csvRecordData[4];                                                                            
               acclist.add(accObj);  
           }
        //insert acclist;
        }
        catch (Exception e)
        {
            ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured while importin data Please make sure input csv file is correct');
            ApexPages.addMessage(errorMessage);
        } 
  }
}


here i want to export same data in csv..

Thanks 
SS
 
Rupal KumarRupal Kumar
Hi  S S,
 
# AccountDataExcel.page

<apex:page controller="AccountDataController" contentType="application/vnd.ms-excel#AccountTable.xls" showHeader="false" standardStylesheets="false">
  <apex:pageBlock >
    <apex:dataTable value="{!accounts}" var="account">
      <apex:column value="{!account.id}"/>
      <apex:column value="{!account.name}"/>
      <apex:column value="{!account.description}"/>
      <apex:column value="{!account.SLA__c}"/>
      <apex:column value="{!account.Active__c}"/>
   </apex:dataTable>
 </apex:pageBlock>
</apex:page>
 
public with sharing class AccountDataController {
  //constructor
  public AccountDataController() {
  }

  //list of accounts
  public List<Account> accounts {
    get {
      if(accounts != null) {
        return accounts;
      } else {
        accounts = [Select Id, name, Active__c, SLA__c, Description from Account limit 10];
        return accounts;
      }
    }
    set;
  }
}

We are now ready to add the “Export to Excel” button to the AccountData.page as follows:
public PageReference exportToExcel() {
   return Page.AccountDataExcel;
 }

Thanks
Rupal Kumar
http://Mirketa.com
S SaiS Sai
HI Rupal Kumar,
Thanks for your reply. Could you plz Explain i am confused..

Thanks 
SS

 
S SaiS Sai

Hi,
No need to create another page in the same page and same controller we need to export the data in csv only using apex and visualfroce 

Thanks
SS

 

Rupal KumarRupal Kumar
HI SS,

you need to create 2 vf page with  same controller .On first you create Account data table with expot button like this-
1.AccountDataTable page-
<apex:page controller="AccountDataController" tabStyle="Account">
<apex:form >
  <apex:pageBlock title="Account Data Table">
  <apex:commandButton value="Export"  action="{!exportAll}"/>
    <apex:pageBlockTable value="{!accounts}" var="account">
      <apex:column value="{!account.id}"/>
      <apex:column value="{!account.name}"/>
      <apex:column value="{!account.description}"/>
      <apex:column value="{!account.SLA__c}"/>
      <apex:column value="{!account.Active__c}"/>
    </apex:pageBlockTable>
  </apex:pageBlock>
  </apex:form>
</apex:page>
2.Excel file page-
<apex:page controller="AccountDataController" contentType="application/vnd.ms-excel#AccountTable.xls" showHeader="false" standardStylesheets="false">
  <apex:pageBlock >
    <apex:dataTable value="{!accounts}" var="account">
      <apex:column value="{!account.id}"/>
      <apex:column value="{!account.name}"/>
      <apex:column value="{!account.description}"/>
      <apex:column value="{!account.SLA__c}"/>
      <apex:column value="{!account.Active__c}"/>
   </apex:dataTable>
 </apex:pageBlock>
</apex:page>
Controller-
public with sharing class AccountDataController {
  //constructor
  public AccountDataController() {
  }

  //list of accounts
  public List<Account> accounts {
    get {
      if(accounts != null) {
        return accounts;
      } else {
        accounts = [Select Id, name, Active__c, SLA__c, Description from Account limit 10];
        return accounts;
      }
    }
    set;
  }
  public Pagereference exportAll(){
    return new Pagereference('/apex/Excel file page');
 }
}

Thanks
Rupal Kumar
http://mirketa.com

 
S SaiS Sai
Sorry Rupal Kumar ,

My Reqirment is when i am import the usernames in csv like 
User-added image        

and when i am clicking the upload button then download the user id's like 
User-added image
when i am importing the username and click the command button then i want to download the based on  usernames id's in  excel file  using apex visualforce 

Thanks 
SS