Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2011
733
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3.  
  4. <cfset showForm = true>
  5. <cfif structKeyExists(form, "xlsfile") and len(form.xlsfile)>
  6.  
  7.     <!--- Destination outside of web root --->
  8.     <cfset dest = getTempDirectory()>
  9.  
  10.     <cffile action="upload" destination="#dest#" filefield="xlsfile" result="upload" nameconflict="makeunique">
  11.  
  12.     <cfif upload.fileWasSaved>
  13.         <cfset theFile = upload.serverDirectory & "/" & upload.serverFile>
  14.         <cfif isSpreadsheetFile(theFile)>
  15.             <cfspreadsheet action="read" src="#theFile#" query="data" headerrow="1">
  16.             <cffile action="delete" file="#theFile#">
  17.             <cfset showForm = false>
  18.         <cfelse>
  19.             <cfset errors = "The file was not an Excel file.">
  20.             <cffile action="delete" file="#theFile#">
  21.         </cfif>
  22.     <cfelse>
  23.         <cfset errors = "The file was not properly uploaded."> 
  24.     </cfif>
  25.        
  26. </cfif>
  27.  
  28. <cfif showForm>
  29.     <cfif structKeyExists(variables, "errors")>
  30.         <cfoutput>
  31.         <p>
  32.         <b>Error: #variables.errors#</b>
  33.         </p>
  34.         </cfoutput>
  35.     </cfif>
  36.    
  37.     <form action="test2.cfm" enctype="multipart/form-data" method="post">
  38.          
  39.           <input type="file" name="xlsfile" required>
  40.           <input type="submit" value="Upload XLS File">
  41.          
  42.     </form>
  43. <cfelse>
  44.  
  45.     <style>
  46.     .ssTable { width: 100%;
  47.                border-style:solid;
  48.                border-width:thin;
  49.     }
  50.     .ssHeader { background-color: #ffff00; }
  51.     .ssTable td, .ssTable th {
  52.         padding: 10px;
  53.         border-style:solid;
  54.         border-width:thin;
  55.     }
  56.     </style>
  57.    
  58.     <p>
  59.     Here is the data in your Excel sheet (assuming first row as headers):
  60.     </p>
  61.    
  62.     <cfset metadata = getMetadata(data)>
  63.     <cfset colList = "">
  64.     <cfloop index="col" array="#metadata#">
  65.         <cfset colList = listAppend(colList, col.name)>
  66.     </cfloop>
  67.    
  68.     <cfif data.recordCount is 1>
  69.         <p>
  70.         This spreadsheet appeared to have no data.
  71.         </p>
  72.     <cfelse>
  73.         <table class="ssTable">
  74.             <tr class="ssHeader">
  75.                 <cfloop index="c" list="#colList#">
  76.                     <cfoutput><th>#c#</th></cfoutput>
  77.                 </cfloop>
  78.             </tr>
  79.             <cfoutput query="data" startRow="2">
  80.                 <cfset hadStuff = false>
  81.                 <cfsavecontent variable="row">
  82.                 <tr>
  83.                 <cfloop index="c" list="#colList#">
  84.                     <cfif not hadStuff and len(trim(data[c][currentRow]))>
  85.                         <cfset hadStuff = true>
  86.                     </cfif>
  87.                     <td>#data[c][currentRow]#</td>
  88.                 </cfloop>
  89.                 </tr>
  90.                 </cfsavecontent>           
  91.                 <cfif hadStuff>#row#</cfif>    
  92.             </cfoutput>
  93.         </table>
  94.     </cfif>
  95.    
  96. </cfif>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement