I already tried to generate a csv of my list by create a separate vf page with the page attribute


Is that possible to generate and download a csv on the fly without using "contentType="text/csv#Export.csv"

1 Answer 1


You do need the contentType, but they can be rolled into one page. You can use a merge field to control which output to use. For example:

<apex:page controller="listpage" contentType="{!contentType}">
    <!-- your content here, rendered based on content type... -->
    <apex:form rendered="{!contentType='text/html'}">
        <apex:commandButton value="Export" action="{!export}" />

And the controller:

public class listpage {
    public string contentType { get; set; }
    public listpage() {
        contentType = 'text/html';
    public void export() {
        contentType = 'text/csv#export.csv';

NOTE You'll need to do more than just this to make sure the headers are not emitted, etc based on the content type. It may be easier to just use a separate page, but it's certainly possible to make it a single page.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .