+ Start a Discussion
URVASHIURVASHI 

Export to CSV file

hi i need help

Below is my code:

VF page:

<apex:page sidebar="false" showHeader="false" controller="communityCon1234" cache="true" contentType="text/csv#Error.csv" language="en-US">


<apex:repeat value="{!HeaderXLS}" var="a">

<apex:outputText value="{!a}">
</apex:outputText>

</apex:repeat>
<br/>
<apex:repeat value="{!fileLine}" var="b">
<apex:outputText value="{!b}"></apex:outputText>

</apex:repeat>
<br/>
</apex:page>

 

Controller:

 

 public List<String> getHeaderXLS()
    {
      List<String> listString1 = new List<String>();
     listString1.add('phnnumber');
      listString1.add('name');
   
      return  listString1;
    }
    public List<String> getFileLine()
    { List<String> listString = new List<String>();
     listString.add('123456');
      listString.add('urvashi');
      return listString;
      
    }

 MY problem here is i wana display the values of listString and listString1 in different columns in excel file.

like

phnnumber    name

123456           urvashi

 

The the size of listString is not fixed.It varies depending upon the code.

How do i do this?
Please help.

And how do i insert<br/> tag in btwn vf page so that it doesnot come in the excel file.

Sonali BhardwajSonali Bhardwaj

I am really not sure if there is any better way to create CSV file in SFDC, but below code is working as you are expecting:

 

VF:

<apex:page sidebar="false" showHeader="false" controller="myClass" cache="true" contentType="text/csv#Error.csv" language="en-US">

<apex:outputText value="{!Header}"/>

<apex:repeat value="{!Data}" var="b">
    <apex:outputText value="{!b}"></apex:outputText>

</apex:repeat>


</apex:page>

Apex: 

public String getHeader() {
    return 'phnnumber, name \n';        
}

public List<String> getData() { 
      List<String> listString = new List<String>();
      listString.add('123456, urvashi \n');
      listString.add('5664564, test \n');
      return listString;
}

 

sambasamba

VF:

 

<apex:page sidebar="false" showHeader="false" controller="myClass" cache="true" contentType="text/csv#Error.csv" language="en-US">

<apex:outputText value="{!Header}"/>

<apex:repeat value="{!DataProxy}" var="b">
    <apex:outputText value="{!b.Phone}"></apex:outputText>
   <apex:outputText value="{!b.Name}">
</apex:repeat>
</apex:page>

 

Apex:

 

 

public String getHeader() {
    return 'phnnumber, name \n';        
}

public List<DataInfo> getDataProxy() { 
      List<DataInfo> dataInfoProxy = new List<DataInfo>();
// Adds some thing to here. return listString; }

class DateInfo
{
public String pnnumber {get; set;}
public String name {get; set;}
}

 

 

 

asish1989asish1989

This is your code

<apex:page controller="exportCSVController" cache="true" contentType="text/csv#Export.csv" language="en-US">
    "Col A","Col B"
    <apex:repeat value="{!myList}" var="a">
        <apex:repeat value="{!a}" var="asub">
        "{!asub.PhoneNumber}","{!asub.Name}"
        </apex:repeat>
    </apex:repeat>
</apex:page>

 

public class exportCSVController {

    public List<List<myClass>> myList {get;set;}
    
    public exportCSVController() {
        myList = new List<List<myClass>>();
        List<myClass> temp = new List<myClass>();
        
        for(Integer i = 0; i < 2500; i++){
            if(temp.size() < 1000){
                myClass m = new myClass();
                m.PhoneNumber = 'PhoneNumber ' + i;
                m.Name = 'Name ' + i;
                temp.add(m);
            }
            else{
                myList.add(temp);
                temp = new List<myClass>();
                myClass m = new myClass();
                m.PhoneNumber = 'PhoneNumber ' + i;
                m.Name = 'Name ' + i;
                temp.add(m);
            }
        }
        myList.add(temp);
    }
    

    public class myClass{
        public string PhoneNumber {get;set;}
        public string Name {get;set;}
    }
}

 GO through these post


http://boards.developerforce.com/t5/Apex-Code-Development/How-to-export-data-to-CSV-file-using-Apex/td-p/166567

 

http://boards.developerforce.com/t5/Apex-Code-Development/export-specific-data-from-Salesforce-to-a-csv-file/td-p/301481

URVASHIURVASHI

Hey sonali thanks.

It helped me.

Can u futher help me,

I am using a list<list<String>> for a 2d array for writing to the file.

But it writes some extra chararcters to the file

Above is my code:

controller:

 
public List<List<String>> getFileLine()
    { 
   
    list1.add('Message');
    for(Integer i=0;i<HeaderXLS.size();i++)
    {
    list1.add(HeaderXLS[i]);
    }
    for(Integer j=0;j<filelines.size();j++)
    {
    list2.add('Error');
    }
    for(Integer j=1;j<filelines.size();j++)
    {
    
    list2.add(filelines[j]);
    }
  

    HeaderDataC.add(list1);
    HeaderDataC.add(list2);
     
    
   
  
    
      return HeaderDataC;
      
    }
   

 Vf page

<apex:repeat value="{!FileLine}" var="b">
<apex:outputText value="{!b}"></apex:outputText>

</apex:repeat>

 

Here when i try to write message and error

it writes with braces

[message] and [error]

can u tell me how to remove this braces.