+ Start a Discussion
Apex Code DevelopmentApex Code Development 

Export a table of data to Excel using Javascript in Visualforce

Hi Folks.....

I used the following Javascript to export my table of data to excel.


   <script type="text/javascript">

   function exportToExcel(){

       var oExcel = new ActiveXObject("Excel.Application");

       var oBook = oExcel.Workbooks.Add;

       var oSheet = oBook.Worksheets(1);

       for (var y=0;y<ExportTable.rows.length;y++){

           for (var x=0;x<ExportTable.rows(y).cells.length;x++){

               oSheet.Cells(y+1,x+1) =ExportTable.rows(y).cells(x).innerText;



       oExcel.Visible = true;

       oExcel.UserControl = true;



Through this code my requirement is almost reached except one issue.I developed a table using Visualforce in Salesforce.I've added the above javascript in the visualforce code.I was successful in exporting the data to Excel having the tags <apex:inputText> but not <apex:outputText>.

My sample code for
table is provided below for more information:

<table id="ExportTable">
  <td width="10%" BGCOLOR="#99CCFF"><center><b>April</b></center> </td>

  <td> <apex:inputText value="{!April1}" disabled="{!Apr1a}"/> </td>
  <td> <apex:inputText value="{!April2}" disabled="{!Apr2a}"/></td>
  <td> <apex:inputText value="{!April3}" disabled="{!Apr3a}"/> </td>
  <td> <apex:outputText value="{0,number,0.00}" id="AprilTotal">  
      <apex:param value="{!AprilTotal}"/>

I was able to export the "AprilTotal" value in to Excel with the above javascript code but not the values in "April1","April2","April3".

Kindly suggest a solution on how to export the values in <apex:outputText> tags.

Thanks & Regards,
Jagadeesh K.

You can try this blog..


You can export excel using javascript remoting, For this you don't need to create additional visualforce page to renderAs excel.