在导出的Excel文件中包含链接

时间:2014-07-16 19:49:26

标签: excel coldfusion export-to-excel coldfusion-10 cfspreadsheet

我正在创建一个查询,然后将其下载到Excel文档中。我想在excel文档中包含一些链接,但无法弄清楚如何做到这一点。我尝试在querySetCell()值中放置一个锚标记,但只是在excel文件中打印html,html不会被解析。我使用的是CF10。以下是我的代码,任何建议表示赞赏。

<cfset q = queryNew("#columnNames#")>

<cfset queryAddRow(q)>
<cfset querySetCell(q, "Attributes", "Part Number")>
<cfset querySetCell(q, "PrimaryPart", "#local.primaryPart.getProductNumber()#")>
<cfset i = 0>
<cfloop array="#local.comparableParts#" index="part">
    <cfset i++>
    <cfset querySetCell(q, "alternatePart"& i, "#part.getPartNumber()#")>
</cfloop>

<cfspreadsheet action="write" query="q" filename="partCompare.xls" overwrite="true" />

<!--- Make a spreadsheet object --->
<cfset s = spreadsheetNew()>
<!--- Add header row --->
<cfset spreadsheetAddRow(s, "#columnNames#")>
<!--- format header --->    
<cfset spreadsheetFormatRow(s, {bold=true, fgcolor="lemon_chiffon"}, 1)>

<cfset spreadsheetAddRows(s, q)>

<cfheader name="content-disposition" value="attachment; filename=partCompare.xls">
<cfcontent type="application/msexcel" variable="#spreadsheetReadBinary(s)#" reset="true">


更新: 我根据评论对代码进行了一些更改。这些链接现在显示在excel文档中,但是友好名称&#39;显示为0,直到我点击“启用编辑”为止。 excel中的按钮。如果友好名称从头开始显示会很好,但如果不可能,或者取决于用户设置,这应该没问题。

<cfset partCompareQueryObj = queryNew("#columnNames#")>

<cfset queryAddRow(partCompareQueryObj)>
<cfset querySetCell(partCompareQueryObj, "Attributes", "Part Number")>
<cfset querySetCell(partCompareQueryObj, "PrimaryPart", "#local.primaryPart.getProductNumber()#")>
<cfset i = 0>
<cfloop array="#local.comparableParts#" index="part">
    <cfset i++>
    <cfset querySetCell(partCompareQueryObj, "alternatePart"& i, "#part.getPartNumber()#")>
</cfloop> 

<cfspreadsheet action="write" query="partCompareQueryObj" filename="partCompare.xls" overwrite="true" />

<!--- Make a spreadsheet object --->
<cfset partCompareSpreadSheet = spreadsheetNew()>

<!--- Add header row --->
<cfset spreadsheetAddRow(partCompareSpreadSheet, "#columnNames#")>
<!--- format header --->    
<cfset spreadsheetFormatRow(partCompareSpreadSheet, {bold=true, fgcolor="lemon_chiffon"}, 1)>

<cfset spreadsheetAddRows(partCompareSpreadSheet, partCompareQueryObj)>

<cfset SpreadsheetSetCellFormula(partCompareSpreadSheet, 'HYPERLINK("http://www.google.com","#local.primaryPart.getProductNumber()#")', 2, 2)>
<cfset i = 0>
<cfloop array="#local.comparableParts#" index="part">
    <cfset i++>
    <cfset SpreadsheetSetCellFormula(partCompareSpreadSheet, 'HYPERLINK("http://www.google.com","#local.primaryPart.getProductNumber()#")', 2, i+2)>
</cfloop>

<cfheader name="content-disposition" value="attachment; filename=partCompare.xls">
<cfcontent type="application/msexcel" variable="#spreadsheetReadBinary(partCompareSpreadSheet)#" reset="true">

1 个答案:

答案 0 :(得分:4)

确保通过调用ColdFusion的SpreadsheetSetCellFormula()函数来调用Excel的Hyperlink()函数。

<cfset spreadsheetSetCellFormula(sheetName
                  , 'HYPERLINK(url, linkText)'
                  , rowNumber
                  , columnNumber)>