Skip to content

Instantly share code, notes, and snippets.

@tyoshikawa1106
Last active December 16, 2015 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyoshikawa1106/5511623 to your computer and use it in GitHub Desktop.
Save tyoshikawa1106/5511623 to your computer and use it in GitHub Desktop.
Visualforce CSV Download
<apex:page controller="CsvListSearchController" cache="true" contentType="text/csv#filename.csv" language="en-US">
"Name","AccountNumber","Site","NumberOfEmployees"
<apex:repeat value="{!accounts}" var="item">
"{!item.Name}","{!item.AccountNumber}","{!item.Site}","{!item.NumberOfEmployees}"
</apex:repeat>
</apex:page>
public with sharing class CsvListSearchController {
public List<Account> accounts {get; set;}
public CsvListSearchController() {
this.accounts = new List<Account>();
}
public void doSearch() {
this.accounts = [select Id,Name,AccountNumber,Site,NumberOfEmployees from Account limit 200];
}
public PageReference doClick() {
return Page.CsvListDownloadPage.setRedirect(false);
}
}
<apex:page controller="CsvListSearchController" title="CsvListSearchPage" showHeader="true" sidebar="false" id="page">
<apex:form id="form">
<apex:pageBlock id="block">
<apex:pageBlockButtons >
<apex:commandButton value=" Search!! " title=" Search!! " action="{!doSearch}" reRender="form" />
<apex:commandButton value=" Download!! " title=" Download!! " action="{!doClick}" rendered="{!accounts.size > 0}" />
</apex:pageBlockButtons>
<apex:pageBlockTable value="{!accounts}" var="item">
<apex:column headerValue="{!$ObjectType.Account.Fields.Name.Label}">
<apex:outputText value="{!item.Name}" />
</apex:column>
<apex:column headerValue="{!$ObjectType.Account.Fields.AccountNumber.Label}">
<apex:outputText value="{!item.AccountNumber}" />
</apex:column>
<apex:column headerValue="{!$ObjectType.Account.Fields.Site.Label}">
<apex:outputText value="{!item.Site}" />
</apex:column>
<apex:column headerValue="{!$ObjectType.Account.Fields.NumberOfEmployees.Label}">
<apex:outputText value="{!item.NumberOfEmployees}" />
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment