1

I'm trying to export a Visualforce page as an Excel document.

I've looked at similar questions and the exported file is saved as a webpage embedded in excel which is prompted as unsupported/corrupt in mobile devices.

It was suggested to use html tags instead of visualforce ones to enforce proper format but this also didn't work. Below is the sample table code in the VF page.

Any thoughts? Thanks.

<apex:page id="Stock_Flexible2"
       standardController="Account"
       extensions="StockPDFController"
       sidebar="false"
       showHeader="false"
       contentType="application/application/vnd.ms-excel#Flexlist.xls"
       applyBodyTag="false"
       applyHtmlTag="false"
       cache="true"
       standardStylesheets="false"    
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
</head>
<div>
            <table style="width:100%">

            <h1> Sample Table </h1>

            <tr>
                <td>a</td>
                <td>b No</td>
                <td>c</td>
            </tr>

                <apex:repeat value="{!flexibleStocks}" var="flex">

                    <tr>
                        <td width="%10"> {!flex.Order_Serial__c}  </td>
                    </tr>

                    <tr>
                        <td width="10%">  {!flex.Order_No__c} </td>
                    </tr>

                    <tr>
                        <td width="10%"> {!flex.Queue__c} </td>
                    </tr>
                </apex:repeat>
            </table>
</div>

4
  • 1
    Excel HTML format is kind of particular in how it is parsed. Also, I've never seen an Excel file rendered this way. You might need to do some research on the specific format. Also, if it says "unsupported format", are you sure you have an app that can handle the file? – sfdcfox Oct 15 '18 at 12:08
  • 1
    I have the office mobile app. Excel Desktop says the file format and the extension isn't matched and considers it unsafe. I found that this is a security feature but users need to open the file in Desktop and save it as an xls or a similar format to be able to use it in mobile. – Atlas Can Oct 15 '18 at 12:13
  • Shane Steinfeld explained an interesting approach here, using Visualforce to actually render an Excel XML sheet. I would not count on Excel to open an HTML table masquerading as .xls across all platforms/versions. – David Reed Oct 15 '18 at 22:06
  • Thank you both, I'll try to implement that solution. – Atlas Can Oct 16 '18 at 7:02
2

For anyone looking up this problem in the future, I've used an external JS library called SheetJS. You can put the script tag in your VF page and send the data as a blob to your apex controller.

You would need to hardcode the excel columns to your data table as well.

<script>
    function s2ab(s) {
        var buf = new ArrayBuffer(s.length);
        var view = new Uint8Array(buf);
        for (var i=0; i!=s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
        return buf;
    }
    function exportExcel(type) {
        var elt = document.getElementById('data-table');
        var wb = XLSX.utils.table_to_book(elt);
        var base64string = XLSX.write(wb, {bookType:type, bookSST:true, type: 'base64', ignoreEC: false});

        /*
        send this base64string to apex
        and download with proper content-type3
        */

        var blob = new Blob([s2ab(atob(base64string))], {
            type: 'application/octet-stream'
        });
        document.getElementById('StockExcel:form:fb').value= base64string;

    }

</script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.