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

    Export to Excel or PDF Custom tag...

    FyiGuru
      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":

      <cfsilent>

      <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#">

      <cfoutput>
      <html>
      <head>
      <title>Export to PDF or Flashpaper</title>
      </head>

      <body>
      #thisTag.generatedContent#
      </body>
      </html>
      <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>
      </cfoutput>
      </cfdocument>
      </cfif>
      </cfcase>
      <!--- 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() ) )#">

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

      </cfswitch>

      </cfsilent>


      Next I place my custom tag around a HTML table:

      <!--- Begin Content Wrapper Custom --->
      <tags:contentWrapper>

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

      </tags:contentWrapper>
      <!--- End Content Wrapper Custom --->

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

      <cfoutput>
      <table cellpadding="" cellspacing="">
      <tr>
      <td>
      <!-- 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 -->


      </td>
      </tr>
      </table>
      </cfoutput>

      Voila.