function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ 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;

   }

   </script>


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">
<tr>
  <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}"/>
     </apex:outputText>     
   </td>     
  </tr>   
</table>

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.

NK@BITNK@BIT
You can try this blog..

https://nitinkhunalsalesforce.wordpress.com/2016/10/27/visualforce-export-excel-report-using-remote-action-and-alasql/

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