0 Replies Latest reply on Jun 12, 2007 9:02 PM by FyiGuru

    Export to Excel or PDF Custom tag...

      After scouring the web I've put together a custom tag that will allow HTML data to be streamed to an Excel file or a PDF.

      First I created the custom tag called "contentWrapper":


      <cfparam name="url.exportType" default=""><!--- I am the output type for this page; pdf, flashpaper, excel or html --->

      <!--- check to see what whether the "ExportFormat" string exists in the URL --->
      <cfswitch expression="#url.exportType#">

      <!--- Begin logic "ExportFormat" string = pdf or flashpagerexists in the URL --->
      <cfcase value="pdf,flashpaper">
      <cfif thisTag.executionMode eq "end">
      <cfdocument format="#url.exportType#">

      <title>Export to PDF or Flashpaper</title>

      <cfdocumentitem type="header"><span style="font-face: Verdana; font-size: 0.7em">Footer Content</span></cfdocumentitem>
      <cfdocumentitem type="footer"><span style="font-face: Verdana; font-size: 0.7em">#dateFormat(now(),"short")# #timeFormat(now(),"short")#</span></cfdocumentitem>
      <!--- End logic "ExportFormat" string = pdf or flashpagerexists in the URL --->

      <!--- Begin logic "ExportFormat" string = excel in the URL --->
      <cfcase value="excel">

      <cfif thisTag.executionMode eq "end">

      <cfheader name="Content-Disposition" value="filename=export.xls">
      <cfcontent type="application/msexcel" variable="#ToBinary( ToBase64( thisTag.GeneratedContent.Trim() ) )#">

      <!--- End logic "ExportFormat" string = excel in the URL --->



      Next I place my custom tag around a HTML table:

      <!--- Begin Content Wrapper Custom --->

      <!-- Begin content table -->
      <table width="90%" border="0" cellpadding="2" cellspacing="1" bgcolor="#000000">
      <td align="center" bgcolor="#CCCCCC"><strong>Custom tag</strong></td>
      <td bgcolor="#FFFFFF">Export to Excel or PDF</td>
      <td bgcolor="#FFFFFF">Example</td>
      <td bgcolor="#FFFFFF"> </td>
      <!-- End content table -->

      <!--- End Content Wrapper Custom --->

      Finally I used two form buttons inconjunction with the onClick event to open my PDF or Excel file:

      <table cellpadding="" cellspacing="">
      <!-- Begin buttons used to pass the exportType -->
      <input type="button" onClick="window.open(' http://#HTTP_HOST#/#SCRIPT_NAME#?#QUERY_STRING#&exporttype=pdf');" value="Export To PDF">
      <input type="button" onClick="window.open(' http://#HTTP_HOST#/#SCRIPT_NAME#?#QUERY_STRING#&exporttype=excel');" value="Export To Excel">
      <!-- End buttons used to pass the exportType -->