[SalesForce] View office file in visualforce page

Is there a way to view a microsoft office document in a visualforce page?

The files are Attachments of a custom object and I need to open a viewer for them.

I tried to use the field Body of the Attachment and render it in a visualforce page rendered as PDF. But it didn't work.

Any suggestions?

Best Answer

Microsoft Word/Excel/PowerPoint .doc/.xls/.ppt is a pretty awkward binary format and the newer .docx/.xlsx/.pptx is zipped XML but still complicated enough. So platforms like Salesforce do not have viewing or conversion capability for all these formats built in.

A reliable way to view these documents is to get your clients to install the free viewers that Microsoft provides if they don't have an Office license. Then as long as the attachments have the correct content type set (which AFAIK they will if manually uploaded from a correct extension file) a link in your Visualforce like this:

<apex:outputLink value="{!URLFOR($Action.Attachment.Download, attachment.id)}">
    Download/View
</apex:outputLink>

will download the attachment and if their browser is configured to "Always Open Files of this Type" (that is an option in Chrome) the attachment will be automatically opened in the correct viewer.