You need to sign in to do that
Don't have an account?
Phantom
How to export data to CSV file using Apex?
Hi all,
Im newbie in force.com, and i have a question, my question is that: i want to export list of object into CSV file using Apex. So please help me how to do this?
Thanks,
contentType="text/plain"
All Answers
Any reason you need to use Apex?
You can export data as CSV very easily, either using a report, or the data loader.
download Apex Data Loader.
there you get option to export and import the csv file.
I currently have the same requirements too. Try <apex:page ... contentType="text/csv#filename.csv" language="en-US">
(With the #filename.csv, it prompts the user to save/open the file. Otherwise, it loads into the browser as html.)
You'll also have to format the VF page as a clean csv text file, that means no pageblocktables or anything that might insert html formatting. For example:
<apex:page controller="csvController" cache="true" contentType="text/csv#filename.csv" language="en-US"> "Col A","Col B","Col C","Col D" <apex:repeat value="{!myList}" var="a"> "{!a.ColA}","{!a.ColB}","{!a.ColC}","{!a.ColD}" </apex:repeat> </apex:page>
Thank Wongt,
And if customer want to export data object to text file, so Force.com can do that?
contentType="text/plain"
I have tried to do follow this code:
It work well when the data i selected < 1000, but when the data select > 1000, the system raise error:
collection exceeds maximum size: 1001
because the limitation of List<SObject> that contains data. So please help me the solution to solve if in case the data i select > 1000? or have another way to export to CSV with the large data using Apex code?
With Spring'10 release there's no longer any limitation on List size. Give a try saving your apex with version 18.0
With Spring'10 release there's no longer any limitation on List size. Give a try saving your apex with version 18.0
--->this method is not useful.when the data select > 1000, the system raise error.How can i do?
Regards,
Hi,
Can you please tell me the workaround for exporting the csv. I am just unable to make it work
Thanks.
You can use a list of lists and nested repeaters to get around the 1000 records in a list limit. Here's an example:
Controller
VisualForce Page:
This should allow you to have up to 10,00,000 records.
Is this generated CSV RFC4180-compliant ? And how can i insert \n or \r in the csv?
Why does the first column always have quotes in the field? How do you remove them? It does export to excel nicely..
Here's an enhancement to Nishad Kallingal's solution at
https://success.salesforce.com/answers?id=9063A0000019RBmQAM
This example has a custom object on the account. I only wanted accounts that had custom objects so I filtered them in the controller.
[note the limit is in while testing the output to the screen. Put back the csv line when page is working]
Controller:
public with sharing class vwQuery2ExcelController {
public List<Account> cs{get; set;}
public vwQuery2ExcelController()
{
cs = new List<Account>();
for (Account c : [SELECT Account.id,Account.Name, (SELECT licence__c.Id, licence__c.Name FROM Account.Licences__r) FROM Account LIMIT 10])
{
if (c.licences__r.isEmpty()) {}
else
{cs.add(c);}
//endif
}
}
}//vwQuery2ExcelController
vf Page:
<apex:page Controller="vwQuery2ExcelController" tabStyle="Account" >
<!--
The following parameters have been removed from the apex:page line while testing.
Put back in to get the result exported...
readOnly="true" contentType="application/vnd.ms-excel#UserReport.xls" cache="true"
Report on a query and send to Excel -->
<!--from Nishad Kallingal -->
<!--https://success.salesforce.com/answers?id=9063A0000019RBmQAM -->
<!-- -->
<apex:pageBlock title="Export Results" >
<apex:pageBlockSection title="Accounts with custom object Licence__c" >
<apex:pageBlockTable value="{!cs}" var="result">
<apex:column value="{!result.ID}"/>
<apex:column value="{!result.Name}"/>
<apex:repeat var="lc" value="{!result.licences__r}">
<apex:column value="{!lc.Name}"/>
</apex:repeat>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:page>
Samuel Reyes in https://success.salesforce.com/answers?id=9063A0000019RBm
says use the code snippet at https://gist.github.com/DouglasCAyers/c733b60db61c290b95ff0047ab3d2432.
and https://douglascayers.com/2016/03/20/salesforce-easy-ways-to-export-data-as-csv/
Thanks to Samuel and Douglas!
Also, my query can be refined to only list accounts with contacts by using this format:
SELECT Account.Name, (SELECT Contact.Name FROM contacts) FROM Account WHERE Account.Id IN (SELECT Contact.accountId FROM Contact)
see: https://developer.salesforce.com/forums/?id=906F0000000Aiy4IAC and other answers.
Please verify the steps to send Apex Result to Excel.
https://deepikamatam.blogspot.com/2019/07/export-apex-result-to-csv-file.html
https://www.salesforcetutorial.com/how-to-export-data-in-csv-by-using-apex-code/