2017-10-19 47 views
0

我想弄清楚如何將我的HTML表格添加到CFExreadsheet以顯示在Excel中。我找到的所有在線示例都不如我的瘋狂(只是一個簡單的基本查詢)。任何幫助,這將不勝感激。這是我已經能夠到目前爲止找出對我的電子表格使用CFspreadsheet編寫電子表格,以避免錯誤此文件可能損壞

<cfset objSpreadsheet = SpreadsheetNew()> 
<cfset filename = expandPath("./myexcel.xls")> 

<!--- Create and format the header row. ---> 
<cfset SpreadsheetAddRow(objSpreadsheet, "Associate Name,Location,Checklists Generated by Associate,Checklists Generated by Selected Location(s),Associate Percentage of Location Total")> 
<cfset SpreadsheetFormatRow(objSpreadsheet, {bold=true, alignment="center"}, 1)> 


<cfheader name="Content-Disposition" value="attachment; filename=#filename#"> 
<cfcontent type="application/vnd.ms-excel" variable="#SpreadsheetReadBinary(objSpreadsheet)#"> 

我的表試圖轉換:

<table class="table table-hover"> 
    <thead> 
     <th><strong>Associate Name</strong></th> 
     <th><strong>Location</strong></th> 
     <th><strong>Checklists Generated by Associate</strong></th> 
     <th><strong>Checklists Generated by Selected Location(s)</strong></th> 
     <th><strong>Associate Percentage of Location Total</strong></th> 
    </thead> 
    <tbody> 
     <cfoutput query="GetEmployeeInfo"> 
     <tr> 
      <td><cfif rnA EQ 1><strong>#assoc_name#</strong></cfif></td> 
      <td><cfif rnL EQ 1>#trans_location#</cfif></td> 
      <td>#checklistsByAssocLoc#</td> 
      <td>#assocChecklistsByLoc#</td> 
      <td>#DecimalFormat(totalChecklistsByAssocLocPct)# %</td> 
      <!---<td> rnA: #rnA# | rnL: #rnL# | rnTotAssoc: #rnTotAssoc# </td> ---> 
     </tr> 
     <cfif rnTotAssoc EQ 1> 
     <tr> 
      <td>Associate Total</td> 
      <td></td> 
      <td>#totalChecklistsByAssoc#</td> 
      <td>#totalAssocChecklistsByAllFilteredLoc#</td> 
      <td>#DecimalFormat(totalChecklistsByLocPct)# %</td> 
     </tr> 
     </cfif> 
    </cfoutput> 
    </tbody> 
</table> 

我瘋狂的查詢!:

<cfquery datasource="#application.dsn#" name="GetEmployeeInfo"> 
    SELECT s4.associate /* Associate's ID */ 
     , s4.assoc_name /* Associate's Name */ 
     , s4.trans_location /* Associate's Location */ 
     , s4.checklistsByAssocLoc /* Gives you a count of Checklists by Associate for a specific Location. */ 
     , s4.assocChecklistsByLoc /* Gives you a count of Total Checklists by All Associates in a Location. */ 
     , s4.totalChecklistsByAssoc /** Gives you a count of Total Checklists by Specific Associate in All Locations. */ 
     , s4.totalAssocChecklistsByAllFilteredLoc /* Gives you a count of Total Checklists by Specific Associates in All Locations. */ 
     , CASE WHEN (coalesce(s4.assocChecklistsByLoc,0) > 0) THEN (CAST(s4.checklistsByAssocLoc AS decimal(8,2))/s4.assocChecklistsByLoc) * 100 ELSE 0 END AS totalChecklistsByAssocLocPct /* This gives you a percent of associate location checklists over count of checklists by Associate in a Location. */ 
     , CASE WHEN (coalesce(s4.totalAssocChecklistsByAllFilteredLoc,0) > 0) THEN (CAST(s4.totalChecklistsByAssoc AS decimal(8,2))/s4.totalAssocChecklistsByAllFilteredLoc) * 100 ELSE 0 END AS totalChecklistsByLocPct /* This gives you a percent of Total Associate Checklists in All Locations over count of Checklists by All Associate in All Locations. */ 
     , s4.rnA /* Placeholder for a record to display the Associate Name. */ 
     , s4.rnL /* Placeholder for a record to display the Location. */ 
     , s4.rnTotAssoc /* Placeholder for the last Associate Location row. The next row should be an Associate Total. */ 
    FROM ( 
    SELECT s3.* 
     , SUM(s3.assocChecklistsByLoc) OVER (PARTITION BY s3.associate) AS totalAssocChecklistsByAllFilteredLoc /* Gives you a count of Total Checklists by Specific Associates in All Locations. */ 
    FROM ( 

    SELECT s2.* 
     FROM ( 
      SELECT a.assoc_name 
       , s1.associate 
       , s1.trans_location 
       , s1.checklistsByAssocLoc 
       , s1.assocChecklistsByLoc 
       , s1.totalChecklistsByAssoc 
       , ROW_NUMBER() OVER (PARTITION BY s1.associate ORDER BY s1.associate, s1.trans_location) AS rnA /* Placeholder for a record to display the Associate Name */ 
       , ROW_NUMBER() OVER (PARTITION BY s1.associate, s1.trans_location ORDER BY s1.associate, s1.trans_location) AS rnL /* Placeholder for a record to display the Location */ 
       , ROW_NUMBER() OVER (PARTITION BY s1.associate ORDER BY s1.trans_location DESC) AS rnTotAssoc /* Placeholder for the last Associate Location row. The next row should be an Associate Total. */ 
    FROM ( 
    SELECT c.associate 
     , c.trans_location 
     , COUNT(*) OVER (PARTITION BY c.associate, c.trans_location) AS checklistsByAssocLoc /* Gives you a count of Checklists by Associate for a specific Location. */ 
     , COUNT(*) OVER (PARTITION BY c.associate) AS totalChecklistsByAssoc /* Gives you a count of Total Checklists by Associate in All Locations. */ 
     , COUNT(*) OVER (PARTITION BY c.trans_location) AS assocChecklistsByLoc /* Gives you a count of Total Checklists by All Associates in a Location. */ 
    FROM cl_checklists c 
    LEFT OUTER JOIN tco_associates a ON c.associate = a.assoc_id 
     AND a.assoc_id IN (<cfqueryparam value="#FORM.EmployeeName#" cfsqltype="cf_sql_varchar" list="true" /> ) /* SELECTED ASSOCIATE IDs */ 
      WHERE c.[DATE] >= <cfqueryparam value="#date1#" cfsqltype="cf_sql_timestamp" /> /* SELECTED DATES */ 
       AND c.[DATE] <= <cfqueryparam value="#date2#" cfsqltype="cf_sql_timestamp" /> 
       AND c.trans_location IN (<cfqueryparam value="#locList#" cfsqltype="cf_sql_varchar" list="true" /> ) /* SELECTED LOCATIONS */ 
    ) s1 
    INNER JOIN tco_associates a ON s1.associate = a.assoc_id 
     AND a.assoc_id IN (<cfqueryparam value="#FORM.EmployeeName#" cfsqltype="cf_sql_varchar" list="true" /> ) /* SELECTED ASSOCIATE IDs */ 

    ) s2 
    WHERE s2.rnA = 1 OR s2.rnL = 1 /* There will be a final Location (rnL=1 and rnTotAssoc=1). This is the final row. */ 
    ) s3 
    ) s4 
    ORDER BY s4.assoc_name, s4.trans_location 
</cfquery> 

這是我在想的路,但我真的不明白調用行和列。我甚至有正確的想法還是我的方式?

<cfoutput query="GetEmployeeInfo"> 
    <cfif rnA EQ 1><cfset SpreadsheetSetCellValue(objSpreadsheet, #assoc_name#, 2, 1) ></cfif> 
    <cfif rnL EQ 1><cfset SpreadsheetSetCellValue(objSpreadsheet, #trans_location#, 2, 1) ></cfif> 
    <cfset SpreadsheetSetCellValue(objSpreadsheet, #checklistsByAssocLoc#, 2, 1) > 
    <cfset SpreadsheetSetCellValue(objSpreadsheet, #assocChecklistsByLoc#, 2, 1) > 
    <cfset SpreadsheetSetCellValue(objSpreadsheet, #DecimalFormat(totalChecklistsByAssocLocPct)# %, 2, 1) > 
    <cfif rnTotAssoc EQ 1> 
     <cfset SpreadsheetSetCellValue(objSpreadsheet, 'Associate Total', 2, 1) > 
     <cfset SpreadsheetSetCellValue(objSpreadsheet, '', 2, 1) > 
     <cfset SpreadsheetSetCellValue(objSpreadsheet, #totalChecklistsByAssoc#, 2, 1) > 
     <cfset SpreadsheetSetCellValue(objSpreadsheet, #totalAssocChecklistsByAllFilteredLoc#, 2, 1) > 
     <cfset SpreadsheetSetCellValue(objSpreadsheet, #DecimalFormat(totalChecklistsByLocPct)# %, 2, 1) > 
    </cfif> 
</cfoutput> 

也試過:

<cfoutput query="GetEmployeeInfo"> 
    <cfset SpreadsheetAddRow(objSpreadsheet, "<cfif rnA EQ 1>#assoc_name#</cfif>,<cfif rnL EQ 1>#trans_location#</cfif>,#checklistsByAssocLoc#,#assocChecklistsByLoc#,#DecimalFormat(totalChecklistsByAssocLocPct)# %")> 
    <cfif rnTotAssoc EQ 1> 
     <cfset SpreadsheetAddRow(objSpreadsheet, "Associate Total,'',#totalChecklistsByAssoc#,#totalAssocChecklistsByAllFilteredLoc#,#DecimalFormat(totalChecklistsByLocPct)# %")> 
    </cfif> 
</cfoutput> 
+1

當你運行該代碼會發生什麼完整的工作代碼?包括任何錯誤消息,堆棧跟蹤等。如果它不像您期望的那樣工作,請解釋它的作用和您希望做的事情之間的區別。 [問] –

回答

2

對於您的最終片段ColdFusion標記將不會以字符串文字進行評估。對於if語句,您可以使用三元運算符來切換某些輸出。此外,如果您的任何數據包含逗號/ s,它將在單元格之間拆分數據。爲了解決這個問題,可以嘗試用引號將每個單元格值包裝起來。這可能會保留您的文本在一個單元格中。 Others still have had issues with this fix

<cfset rowNumber = 0 /> 
<cfoutput query="GetEmployeeInfo"> 
    <cfset rowNumber++ /> 
    <cfset rowList = "'#(rnA eq 1)?assoc_name:''#', '#(rnl eq 1)?trans_location:''#', '#checklistsByAssocLoc#,#assocChecklistsByLoc#', '#DecimalFormat(totalChecklistsByAssocLocPct)# %'"> 
    <cfset SpreadsheetAddRow(objSpreadsheet, rowList)> 
    <cfset spreadsheetFormatCell(objSpreadsheet, {bold: true}, rowNumber, 1)> 
    <cfif rnTotAssoc EQ 1> 
     <cfset rowNumber++ /> 
     <cfset rowList = "'Associate Total','','#totalChecklistsByAssoc#','#totalAssocChecklistsByAllFilteredLoc#','#DecimalFormat(totalChecklistsByLocPct)# %'" > 
     <cfset SpreadsheetAddRow(objSpreadsheet, rowList)> 
    </cfif> 
</cfoutput> 

就個人而言,由於緩慢的使用許多(幾百+)spreadsheetAddRow s到創建一個電子表格,並用繩子數據列表的工作可以是一個痛苦的渲染時間,我幾乎總是把我的電子表格數據成查詢對象。一旦數據位於查詢對象中,需要調用spreadsheetAddRows來將數據導入到電子表格中。

qReportData = queryNew("Name, Age", 
    "varchar, integer", 
    [{name: "Tom", age: 25},{name: "Dick", age: 40},{name: "Harry", age: 55}] 
); 
sheet = SpreadsheetNew(false); 

//Add and format headers 
bold = {bold: true}; 
spreadsheetAddRow(sheet, "Name, Age"); 
spreadsheetFormatRow(sheet, bold, 1); 

spreadsheetAddRows(sheet, qReportData); 

鑑於一些報告的數據可以在多個行,在特定情況下,您將不能只是出口報表查詢,所以我們必須建立一個新的代碼。我們將遍歷報表並在電子表格查詢中生成行。在我的例子,我任何時候的人是增加一個額外的行的40

qSheetOutput = queryNew("Name, Age"); 
for(row in qReportData){ 
    queryAddRow(qSheetOutput, { 
     name: row.name, 
     age: row.age 
    }); 
    if(row.age > 40){ 
     queryAddRow(qSheetOutput, { 
      name: row.name & " is over 40" 
     }); 
    } 
} 
// now writing the generated query to the spreadsheet 
spreadsheetAddRows(sheet, qSheetOutput); 

歲的最後一步將是迭代和格式化表格的單元格結束。在迭代輸出時,我必須通過表單中的標題數來抵消正在處理的行,本示例中的女巫爲1.同樣,對於此示例,超過40歲的人的額外行將不要粗體顯示,並且會跨越2個單元格。

for(row in qSheetOutput){ 
    if(!len(row.age)){ 
     spreadsheetFormatCell(sheet, {dataformat="@", alignment="center"}, qSheetOutput.currentRow + 1, 1); 
     spreadsheetFormatCell(sheet, qSheetOutput.currentRow + 1, qSheetOutput.currentRow + 1, 1, 2); 
    } 
    else{ 
     spreadsheetFormatCell(sheet, bold, qSheetOutput.currentRow + 1, 1); 
    } 
} 

如果看輸出是太難以確定正確的格式/ s的所需行,你可以遍歷需要特定格式/ s的行號的數組/秒。再次注意到我再次使用標題計數作爲偏移量。

dataRows = []; 
messageRows = []; 
for(row in qReportData){ 
    queryAddRow(qSheetOutput, { 
     name: row.name, 
     age: row.age 
    }); 
    arrayAppend(dataRows, qSheetOutput.recordCount + 1); 
    if(row.age > 40){ 
     queryAddRow(qSheetOutput, { 
      name: row.name & " is over 40" 
     }); 
     arrayAppend(messageRows, qSheetOutput.recordCount + 1); 
    } 
} 
... 
for(rowNumber in dataRows){ 
    spreadsheetFormatCell(sheet, bold, rowNumber, 1); 
} 
for(rowNumber in messageRows){ 
    spreadsheetFormatCell(sheet, {dataformat="@", alignment="center"}, rowNumber, 1); 
    spreadsheetFormatCell(sheet, rowNumber, rowNumber, 1, 2); 
} 

這裏是TryCF.com

+0

電子表格知道單元格的寬度作爲其格式化的一部分,您可以使用'SpreadSheetSetColumnWidth'來更改字段的寬度。 Excel並不像HTML表格那樣假設列的寬度。 – Twillen

+0

讓我們[在聊天中繼續討論](http://chat.stackoverflow.com/rooms/157515/discussion-between-twillen-and-david-brierton)。 – Twillen

1

貴在持之以恆的榮譽,這裏是一個我做了前兩天。 visitData,headers,columnstitle變量是在程序的前面設置的,因爲它們也適用於html輸出。

<cfscript> 
filePath = "d:\dw\dwweb\work\"; 
fileName = title & " " & getTickCount() & ".xlsx"; 

sheet = spreadSheetNew("data", true); 
HeaderFormat = {}; 
HeaderFormat.bold = true; 

spreadSheetAddRow(sheet, headers); 
SpreadSheetFormatRow(sheet, HeaderFormat, 1); 
SpreadSheetAddFreezePane(sheet, 0,1); 

for (queryRow = 1; queryRow <= visitData.recordcount; queryRow ++) { 
rowNumber = queryRow + 1; 
for (columnNumber = 1; columnNumber <= listLen(columns); columnNumber ++) { 
thisColumn = listGetAt(columns, columnNumber); 
thisValue = visitData[thisColumn][queryrow]; 
SpreadSheetSetCellValue(sheet, thisValue, rowNumber, columnNumber); 
} // columns 
} // rows 

SpreadSheetWrite(sheet,filePath & fileName, true); 
</cfscript> 
<cfheader name="content-disposition" value="Attachment;filename=#fileName#"> 
<cfcontent file="#filePath & fileName#" type="application/vnd.ms-excel"> 

請注意我在最後兩個標記中使用的變量。 <cfheader>標籤只有文件的名稱,但不包含路徑。我之前犯的一個錯誤是隻使用了一個變量。結果是發送給用戶的文件名不受歡迎。

+0

文件路徑這是否允許您從.htm獲取表格,然後遍歷字段並填充電子表格?對不起,我仍然對所有這些都感到困惑 –