Difficulties with generating an Excel sheet to Mac in Salesforce

Salesforce supports developers greatly with including an export functionality for Visualforce pages. In order to generate the Excel sheet, developers need to have a Visualforce beginning, such as the following:

XHTML

1

2

3

<apex:page controller="ExcelSheetController"

contentType="application/vnd.ms-excel#{!sheetName}.xls"

showheader="false">

Then you need to simply define the content of the Visualforce page and when it is opened, the file is downloaded. Sounds easy, right? When you are working with Microsoft Excel for Windows, then it is indeed. However Excel for Mac does not display the sheet correctly.

For our Management by Objectives (MbO) application, we wanted to give users the possibility to export their views. We thought we were finished, until we tested this on a Mac:

This seemed inexplicable at first, but when we take a deeper look into the first line of the Manager column, we find

JavaScript

1

if(!window.sfdcPage){window.sfdcPage=newApexPage();

This seemed familiar. So we were asking ourselves, what Javascript code does in an Excel sheet. When opening the Excel sheet with a text editor, we found out that Salesforce is actually generating a normal HTML file.

After some Google search, we found out that the use of tags, such as apex:pageBlock or apex:pageBlockTable are not compatible with the Mac version of Excel. This is because it generates a Javascript block, when one of these tags is used. We replaced these tags, as they have a formatting purpose, and used the apex:dataTable tag for displaying our data. Still the same problem. apex:dataTable produces the same bug.

Therefore we drastically removed all apex:dataTable tags with a simple HTML table and an apex:repeat instruction:

XHTML

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<table>

<tr>

<th>Column 1</th>

<th>Column 2</th>

</tr>

<apex:repeat value="{!someCollection}"var="sC">

<tr>

<td>

<span>{!sC.FieldValue1}</span>

</td>

<td>

<span>{!sC.FieldValue2}</span>

</td>

</tr>

</apex:repeat>

</table>

Here is a list of working <apex:Tag> tags, which do not create a Javascript block. We use these for the MbO Excel sheet: