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
URVASHIURVASHI 

Write to CSV file

Hello,

 I Wanna write data to the CSV file, and i have the values of those variables in my controller.

 

How do i di it.

 

<apex:page controller="comm" cache="true" contentType="text/csv#File.csv" language="en-US">
</apex:page>

 I know the above method to create a file as File.csv.

But how do i write data to CSV.

 

Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
Neeraj_ChauriyaNeeraj_Chauriya

Then its very simple..just access it with the name you provide in the var attribute of <apex:repeat> tag.

 

<apex:reapeat value="{!inputvalues}" var="inVal">

<!-- Write the comma separated values in the page -->

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

</apex:repeat>

 

All Answers

AsitM9AsitM9
public class comm{
  public list<Account> acc{get;set;}
  public void getCSV(){
    acc=[select id,name from Account];
    
  }
  
  
}

 use list acc in the VF page. 

Neeraj_ChauriyaNeeraj_Chauriya

Just include comma separated values in the viualforce page and try to render it.

 

Avoid including divs,spans or any other HTML/VF components.

 

<apex:page controller="comm" cache="true" contentType="text/csv#File.csv" language="en-US">
<apex:outputText value="Account Name"/>,<apex:outputText value="Id"/>
<br/>
<apex:repeat value="{!accList}" var="acc">
<apex:outputText value="{!acc.Name}"/>,<apex:outputText value="{!acc.Id}"/>
<br/>
</apex:repeat>
</apex:page>

 

Please hit Kudos and mark it as Solution if it helps you.

URVASHIURVASHI
for(Integer j=0;j<length;j++)
               {
              try
               {
                 sObj.put(l[j],inputvalues[j]);
                 
                 
                }catch(Exception e)
                {
                
                 ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'record fail');
                ApexPages.addMessage(errormsg);
             
             
               }
             

               } 

 This is my code for controller.

I wana write inputvalues array in csv file.

so how do i do this?

and i want my code for writing to CSV file in catch block?

 

 

Thanks for the reply Neeraj and Asit9.

Neeraj_ChauriyaNeeraj_Chauriya

You can simply use inputvalues array with <apex:repeat> and access its elements.

If its a array of SObject then you can write values like:

 

<apex:reapeat value="{!inputvalues}" var="inVal">

<!-- Write the comma separated values in the page -->

<apex:outputText value="{!inVal.Value1}"/> , <apex:outputText value="{!inVal.Value2}"/> , <apex:outputText value="{!inVal.Value3}"/>

<br/>

</apex:repeat>

 

 

Please hit Kudos and mark it as solution if it helps you.

URVASHIURVASHI

what is value1 ,value2,value3

where do i define them?

 

Thanks for the help neeraj.

Neeraj_ChauriyaNeeraj_Chauriya

Hi URVASHI,

 

That was just an example for writing field values of an sObject.

 

I am assuming that the inputvalues is an array of sObject and value1, value2, value3...valueN are the fields of the sObject.

 

 

URVASHIURVASHI

no my inputvalues is a List of type String.

in this case how do i refer its values in 'outputtext'.

 

Thanks.

Neeraj_ChauriyaNeeraj_Chauriya

Then its very simple..just access it with the name you provide in the var attribute of <apex:repeat> tag.

 

<apex:reapeat value="{!inputvalues}" var="inVal">

<!-- Write the comma separated values in the page -->

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

</apex:repeat>

 

This was selected as the best answer
URVASHIURVASHI

Hey thanks neeraj.

I need one more help.

Now all the data is displayed on one single column in excel file.

How do i seperate the data in different columns.

Could you please help me on this.

And if i apply <br/> tag its displayed on excel file.how to avoid doing this?

 

Thanks.