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
Radhika pawar 5Radhika pawar 5 

How to Export csv file from visual force page

I m to export one particular Export Excel File from VF page
Virendra ChouhanVirendra Chouhan
Hi Radhika,

just use a simple attribute in <apex:page> Component . 
that is contentType="application/vnd.ms-excel#contacts.xls"
i.e 
<apex:page standardController="Opportunity"   contentType="application/vnd.ms-excel#contacts.xls">
it'll export a Excel file from VF page.


Regards
Virendra 

Virendra ChouhanVirendra Chouhan
Hi Radhika,

Sorry i didn't get last time you want to export CSV file from visualforce.
so, try this contentType="application/octet-stream#MYCSV.csv"

<apex:page standardController="Opportunity" contentType="application/octet-stream#MyFile.csv"  >
it'll be export a csv file with the name of MyFile.

Regards
Virendra


AshlekhAshlekh
Hi 

Below info help you for differnt type files to save.
=============================================
Excel:
=============================================
For this report, creating an HTML table will work to render the data in its appropriate columns and rows. You may also use apex:pageBlockTable, apex:repeat, apex:dataTable, etc.

The "#report.xls" tagged at the end of the contentType string will be the name of your file. Also, you have to set cache="true" for IE support.
*********************************************************************************************************
<apex:page  contentType="application/vnd.ms-excel#YOUR_FILE_NAME.xls" cache="true">
*********************************************************************************************************

=============================================
Word:
=============================================
Same as the Excel file, the "#report.doc" tagged at the end of the contentType string will be the name of your file. Again, you have to set cache="true" for IE support.

*********************************************************************************************************
<apex:page  contentType="application/msword#YOUR_FILE_NAME.doc" cache="true">
*********************************************************************************************************

This tag helps you control your page breaks:
<br clear="all" style="page-break-before:always" />

=============================================
PDF:
=============================================
If you're using apex:pageBlock, etc, the Salesforce theme will not display in the PDF, including the nice rounded page borders, colors, or headers. It'll just appear as different-sized fonts with different weights, etc.
*********************************************************************************************************
<apex:page standardController="Account" showHeader="false" renderAs="pdf">
*********************************************************************************************************

=============================================
CSV:
=============================================
Note that this page has to be completely clean and display only text that would be accepted in a .csv file. Even using something as basic as "apex:repeat" tend to insert breaks, spaces, and formatting that would be disruptive if you are using the csv file for an upload process that may be fussy. I've found that the best way to do this is to format your entire csv text within your controller class into a string variable, and display just the string in the VisualForce page using {!string} or apex:outputText.

The "#report.xls" tagged at the end of the contentType string will be the name of your file.
*********************************************************************************************************
<apex:page controller="ReportController" contentType="text/csv#YOUR_FILE_NAME.csv">

*********************************************************************************************************

Source:
http://www.salesforce.com/us/developer/docs/pages/Content/pages_styling_content_type.htm
http://www.salesforce.com/us/developer/docs/pages/Content/pages_quick_start_renderas_pdf.htm
http://theviewfromouthere.wordpress.com/2009/10/08/turning-a-visualforce-page-into-a-word-document/

Please mark as solution if it helps you.
Radhika pawar 5Radhika pawar 5
Why this come when  I m  exporting that page?????
Today                 
[{"displayName": "exportRIschoolReportcard","name": "exportRIschoolReportcard","viewstateSize": 34.97,"percentOfParent": 100.0,"percentOfTotalViewstate": 100.0,"leaf": false,"isGroupable": false,"identityCode": "ae20030f9da550b8d0fa24bb78cb32ff","children": [{"displayName": "Component Tree","name": "Component Tree","viewstateSize": 3.66,"percentOfParent": 10.0,"percentOfTotalViewstate": 10.0,"leaf": true,"iconCls": "task","isGroupable": false,"identityCode": "bf2da6a8e97f5497c0a6c8097f3b1eec"},{"displayName": "State","name": "State","viewstateSize": 31.3,"percentOfParent": 90.0,"percentOfTotalViewstate": 90.0,"leaf": false,"isGroupable": false,"identityCode": "413e533e3401d96aa9b3e80302097af5","children": [{"displayName": "Expressions","name": "Expressions","viewstateSize": 13.53,"percentOfParent": 43.0,"percentOfTotalViewstate": 39.0,"leaf": true,"iconCls": "task","isGroupable": false,"identityCode": "48b4d113b8dbc1704910266b31bd56f4"},{"displayName": "Controllers","name": "Controllers","viewstateSize": 0.81,"percentOfParent": 3.0,"percentOfTotalViewstate": 2.0,"leaf": false,"isGroupable": false,"identityCode": "bf386e2756d2aeee69fb02a125af99e","children": [{"displayName": "ctrlRISchoolCardInteraction","name": "ctrlRISchoolCardInteraction","type": "Page exportrischoolreportcard Controller Extension","viewstateSize": 0.81,"percentOfParent": 100.0,"percentOfTotalViewstate": 2.0,"leaf": false,"iconCls": "task","isGroupable": false,"identityCode": "125c64de0397f7aa6c5514ba04526264","children": [{"displayName": "last_submission","name": "last_submission","type": "Integer","viewstateSize": 0.08,"percentOfParent": 10.0,"percentOfTotalViewstate": 0.2,"value": "0","leaf": true,"iconCls": "task","isGroupable": false,"identityCode": "e6156ce6e495eee9b5a7e119813f6765"},{"displayName": "rsr","name": "rsr","type": "RI_School_Report_Cards__c","viewstateSize": 0.11,"percentOfParent": 14.0,"percentOfTotalViewstate": 0.3,"leaf": true,"isGroupable": false,"identityCode": "476205c9fbcbadfb035530155e1f9371"},{"displayName": "user","name": "user","type": "User","viewstateSize": 0.17,"percentOfParent": 21.0,"percentOfTotalViewstate": 0.5,"leaf": false,"isGroupable": false,"identityCode": "2b3e08ed434ed950901f3c18c357439c","children": [{"displayName": "RI_Login_State__c","name": "RI_Login_State__c","type": "String","viewstateSize": 0.11,"percentOfParent": 65.0,"percentOfTotalViewstate": 0.3,"value": "Madhya Pradesh","leaf": true,"iconCls": "task","isGroupable": false,"identityCode": "e3a98528fcd5ea5227ea7b9a78d46172"},{"displayName": "RI_Login_Block__c","name": "RI_Login_Block__c","type": "String","viewstateSize": 0.11,"percentOfParent": 65.0,"percentOfTotalViewstate": 0.3,"value": "Vidisha","leaf": true,"iconCls": "task","isGroupable": false,"identityCode": "3b85a84fee30b16691bd12302f1e479"},{"displayName": "Id","name": "Id","type": "String","viewstateSize": 0.09,"percentOfParent": 53.0,"percentOfTotalViewstate": 0.3,"value": "00590000001dzJjAAI","leaf": true,"iconCls": "task","isGroupable": false,"identityCode": "f90548e8b2c837662d8f561abd5c39ef"},{"displayName": "RI_Login_District__c","name": "RI_Login_District__c","type": "String","viewstateSize": 0.11,"percentOfParent": 65.0,"percentOfTotalViewstate": 0.3,"value": "Vidisha","leaf": true,"iconCls": "task","isGroupable": false,"identityCode": "31a9848a87fbb979f4e9a0f32c9a127d"}]},{"displayName": "volthr [1]","name": "volthr","type": "LIST<RI_VOL_THR_INFO__c>","viewstateSize": 0.28,"percentOfParent": 35.0,"percentOfTotalViewstate": 0.8,"leaf": false,"isGroupable": true,"identityCode": "ce55180042dc258786cdb2c14acc5c9c","children": [{"displayName": "0","name": "0","type": "RI_VOL_THR_INFO__c","viewstateSize": 0.14,"percentOfParent": 50.0,"percentOfTotalViewstate": 0.4,"leaf": false,"iconCls": "task","isGroupable": false,"identityCode": "b8e5dd1e21e0e7168555981ef35f710","children": [{"displayName": "RI_School_Card__c","name": "RI_School_Card__c","type": "String","viewstateSize": 0.11,"percentOfParent": 79.0,"percentOfTotalViewstate": 0.3,"value": "null","leaf": true,"iconCls": "task","isGroupable": false,"identityCode": "31eb0b6e0923fe7f04afec6b7efbf544"}]}],"count": 1}]}]},{"displayName": "Internal","name": "Internal","viewstateSize": 16.96,"percentOfParent": 54.0,"percentOfTotalViewstate": 49.0,"leaf": true,"iconCls": "task","isGroupable": false,"identityCode": "6ced6842ce6d7d41667f92dd06b6688"}]}],"viewstateLimit": 135.0,"percentOfViewstateLimit": 25.9}]
Himanshu Tiwari 8Himanshu Tiwari 8

@Radhika pawar 5, you must be using <apex:form> tag, i faced the same problem. try removing it.

Thank You.

Hari Haran 82Hari Haran 82

hi@Radhika pawar5 did you find any solution for  that

while export the  CSV i getting this error 

<script type="text/javascript"> SfdcApp.Visualforce.ViewState.ViewstateSender.sendViewstate </script>

Anyone have a solution for that