2017-02-15 41 views
0

我試圖讀取一個電子表格,它使用cfspreadsheet在多個選項卡上有預定義的圖表,但是當涉及到處理數據時,我得到的變量是未定義的。cfspreadsheet導致變量未定義的錯誤

我使用的例子從Adobe - http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec17cba-7f87.html

我也看了其它的例子,但這些例子使用cfspreadsheet和POI或Java的組合,我寧願如果可能的話只使用cfspreadsheet。任何想法在下面的問題來自哪裏看我的代碼?謝謝。

<cfcontent type="application/vnd.ms-excel" reset="Yes"> 

<cfquery name="GetData" datasource="#request.dsn#"> 
SELECT * 
FROM v_Occurrences 
</cfquery> 

<cfset strFileName = "OccurrenceData" & "#now().getTime()#" & "#UserID#"> 
<!--- 
<cfdump var="#GetData#"> 
---> 

<cfset filepath = "file:///...OccurenceData.xls"> 
<!--- Write the two sheets to a single file ---> 
<cfspreadsheet 
    action ="read" 
    columnnames = "Occurence_Date,Full_Name_Client" 
    columns = "2" 
     excludeHeaderRow = "false" 
    headerrow = "1" 
    query="GetData" 
    rows = "2" 
    src ="#filepath#"> 

<cfscript> 
    OccurrenceData = SpreadsheetNew("Data"); 
    Month = SpreadsheetNew("Month"); 
    Person = SpreadsheetNew("Person"); 
    SpreadsheetAddRows(occurrencedata,getdata); 
</cfscript> 

<!--- Write the two sheets to a single file ---> 
<cfspreadsheet 
    action="write" 
    filename="#filepath#" 
    name="OccurrenceData" 
    overwrite="true" 
     sheetname="Data" > 


<cfspreadsheet 
    action="Update" 
    filename="#filepath#" 
    name="Month" 
     sheetname="Month"> 

<cfspreadsheet 
    action="Update" 
    filename="#filepath#" 
     name="Person" 
     sheetname="Person" > 

<cfspreadsheet 
    action="read" 
    src="#filepath#" 
    sheetname="occurrencedata" 
    query="GetData"> 

這裏就是錯誤發生:變量Occurrence_Data未定義

<cfscript> 
    SpreadsheetAddRow(OccurrenceData,"Date,Name",1,1); 
    SpreadsheetAddRow(OccurrenceData, 
    "#Dateformat(Occurrence_Date,'mm/dd/yyyy')#,#Full_Name_Client#", 
    2,1,true); 
</cfscript> 

<cfspreadsheet 
    action="write" 
    autosize="true" 
    filename="#strFileName#.xls" 
    overwrite="true" 
    password="password" 
    query="GetData" 
     sheetname="Data" > 

回答

0

問題數1.這是可能做弊大於利。

<cfcontent type="application/vnd.ms-excel" reset="Yes"> 

問題數2.如果你想寫兩片單個文件,你不這樣做:

OccurrenceData = SpreadsheetNew("Data"); 
Month = SpreadsheetNew("Month"); 

你做這樣的事情:

OccurrenceData = SpreadsheetNew("Data"); 
// do stuff with this sheet 
SpreadsheetCreateSheet(OccurrenceData, "Month"); 
SpreadSheetSetActiveSheetNumber(OccurrenceData, 2); 
// do stuff with this sheet 

SpreadSheetWrite(OccurrenceData, whateverYourFileNameIs); 

我也建議你一次做一件事。編寫一些代碼,運行它,然後查看結果。然後添加更多的代碼,運行它,然後查看結果。這將幫助您隔離導致錯誤的代碼。

+0

因此,我調整了我的代碼並將其簡化爲下面的內容,但沒有收到任何錯誤,但我也沒有收到Excel表單 - 只是一個空白屏幕。我知道我需要調整SpreadsheetAddRow行以循環我的數據,以便您可以看到以下更改: spObj = SpreadsheetNew(「Data」); SpreadsheetAddRows(spObj,#getdata#); SpreadsheetAddRow(spObj,「Date,Name」,1,1); SpreadsheetCreateSheet(spObj,「Month」); SpreadSheetSetActiveSheetNumber(spObj,2);

+0

spObj = SpreadsheetNew(「Data」); SpreadsheetAddRow(spObj,「#Dateformat(getdata.Occurrence_Date,'mm/dd/yyyy')#,#GetData.Full_Name_Client#」,2(i = 1; i <= GetData.recordcount; i ++){ ,1,TRUE); } SpreadSheetWrite(spObj, 「###的文件路徑#strFileName .xls的」, 「是」);