Monday, February 13, 2012

VisualForce Reports: Excel, Word, PDF, CSV

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 controller="ReportController" contentType="application/vnd.ms-excel#report.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 controller="ReportController" contentType="application/msword#report.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="Quote__c" 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#report.csv">
<apex:outputText value="{!csvStr}" />
</apex:page>


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/

No comments:

Post a Comment