2016-12-06 33 views
1

我有一个excel文件,我试图读取,然后在下拉菜单中显示标题的值。我的Excel文件中的第一行具有所有的值(标题名称)。如何循环浏览cfspreadsheet标题?

我使用下面的代码,但是会发生什么是所有标题名称都以逗号出现在一行中。我想要将标题分开,以便它将显示在许多<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> 
+0

运行该代码片段我没有做了很多与电子表格和ColdFusion,但如果excelHeader.columnList是回来为列表,把它当作一个列表 - 通过循环列表,当你做你的选择框 – luke

+0

我试图循环使用列表,“#excelHeader.columnList#',它仍然不能按我想要的方式工作。 – gosi123

+0

就像我说的,我对excel和colfusion没有太多了解,但是要循环播放列表,你需要像#headerItem# luke

回答

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

+0

@Leigh嘿它很好。现在看起来更专业。谢谢。 – Justin