如何循环使用cfspreadsheet标题?

时间:2016-12-06 06:50:47

标签: coldfusion coldfusion-11 cfspreadsheet

我有一个我试图读取的excel文件,然后在下拉列表中显示标题的值。我的excel文件中的第一行包含所有值(标题名称)。

我使用下面的代码,但会发生的是所有标题名称都以逗号显示在一行中。我希望标题分开,以便它会显示在包含许多<option>的下拉列表中,而不是单个<option>。我该怎么做?

    <!-- Read the header values from excel -->
    <cfset spreadsheet = "uploads/spreadsheet.xlsx">

    <cfspreadsheet action="read" headerrow="1" src="uploads/spreadsheet.xlsx" query="excelHeader" rows="1" />

    <cfset excelHeaders = excelHeader.columnList>

    <!-- Display the header names as a dropdown -->
    <select name="id_headers">
        <option>
            #excelHeaders#
        </option>
    </select>

1 个答案:

答案 0 :(得分:4)

您可以尝试使用此代码;

<!--- create new spreadsheet and populate with sample data --->
<cfset theSheet = SpreadsheetNew("Expenses")>
<cfset SpreadsheetSetCellValue(theSheet,"column1",1,1)> 
<cfset SpreadsheetSetCellValue(theSheet,"column2",1,2)>
<cfset SpreadsheetSetCellValue(theSheet,"column3",1,3)>
<cfset SpreadsheetSetCellValue(theSheet,"column4",1,4)>

<!--- Write the spreadsheet to a file, replacing any existing file. ---> 
<cfset pathToFile = GetDirectoryFromPath(GetCurrentTemplatePath()) & "newSpreadsheet.xls">
<cfspreadsheet action="write" filename="#pathToFile#" name="theSheet" overwrite=true>

<!--- Read spreadsheet into query object ---> 
<cfspreadsheet action="read" headerrow="1" src="#pathToFile#" query="excelHeader" rows="1">

<!--- Display the header names as a dropdown --->
<cfoutput>
<select name="id_headers">
<cfloop list="#excelHeader.columnList#" index="option">
    <option>#option#</option>
</cfloop>
</select>
</cfoutput>

您可以在trycf

中运行此代码段