使用ColdFusion在电子表格中创建验证下拉列表时出错

时间:2014-02-15 17:37:29

标签: coldfusion apache-poi cfspreadsheet

我只是使用ColdFusion从数据库查询中创建电子表格中的验证下拉列表。但我收到以下错误

  

“公式中的字符串文字不能大于255个字符ASCII”

任何人都可以帮忙解决这个问题,因为我迫切需要这个功能。

<cfset pop_array = ArrayNew(1)>
<cfset provider_name_array = ArrayNew(1)>
<h1>Hello</h1>
<cfquery name="qryGetPOP" datasource="webalc">
    select distinct center_code from alc_pop<!--- where rownum<=10 --->
</cfquery>
<!--- Convert the qryGetPOP to an array. --->
<cfloop query="qryGetPOP">
    <cfset arrayAppend(pop_array, center_code)>
</cfloop>

<cfquery name="qryGetProviderName" datasource="webalc">
    select distinct telcoofficialname from cmt_access_provider where rownum<=10
</cfquery>

<h1>Hello1</h1>
<!--- Convert the qryGetProviderName to an array. --->
<cfloop query="qryGetProviderName">
    <cfset arrayAppend(provider_name_array, telcoofficialname)>
</cfloop>

<cfdump var="‪#‎qryGetPOP‬#">
<cfdump var="‪#‎pop_array‬#">
<cfdump var="‪#‎provider_name_array‬#">

<cfscript>
    //Create New Spreadsheet
    SpreadsheetObj = spreadsheetNew("testsheet");
        SpreadSheetAddRow(spreadsheetObj,'Request_ID,Requested_Services,Target_Cost,MRC_cost,NRC_Cost,MRC_Margin,NRC_Margin,Carrier_On_Net,Currency_information,Access_Technology,Speed,CSAID,Provider_Name,Centre_Code,Need_for_optimization,Contract_Duration,Budgetary_OR_Firm,Carrier_Quote_Reference,Quote_Received_Date,Optimization_Cost_From,Quote_Validity_Days,Expiry_Term_Date,Comments_to_Sales');
    //Get Workbook object
    workbook = SpreadsheetObj.getWorkBook();

    //Get sheet by name where you liek to add list validation
    sheet = workbook.getSheet("testsheet");


    //Create object of required class
    dvconstraint = createObject("java","org.apache.poi.hssf.usermodel.DVConstraint");
    cellRangeList = createObject("java","org.apache.poi.ss.util.CellRangeAddressList");
    dataValidation = createObject("java","org.apache.poi.hssf.usermodel.HSSFDataValidation");


    //Define cell list rowstart, rowend, column start, column end for pop list
    addressList = cellRangeList.init(1, 50, 0, 0);//First 10 rows in first column
    dvConstraint = dvconstraint.createExplicitListConstraint(pop_array); //set contraint value
    dataValidation = dataValidation.init(addressList, dvConstraint); //apply validation on address list
    dataValidation.setSuppressDropDownArrow(false);//Enable/disable dropdown arrow.
    sheet.addValidationData(dataValidation);//Add validation to sheet.

    //Create object of required class for provider
    dvconstraint_provider = createObject("java","org.apache.poi.hssf.usermodel.DVConstraint");
    cellRangeList_provider = createObject("java","org.apache.poi.ss.util.CellRangeAddressList");
    dataValidation_provider = createObject("java","org.apache.poi.hssf.usermodel.HSSFDataValidation");
    //Define cell list rowstart, rowend, column start, column end for provider list
    addressList_provider = cellRangeList.init(1, 50, 1, 1);//First 10 rows in first column
    dvConstraint_provider = dvconstraint_provider.createExplicitListConstraint(provider_name_array); //set contraint value
    dataValidation_provider = dataValidation_provider.init(addressList_provider, dvConstraint_provider); //apply validation on address list
    dataValidation_provider.setSuppressDropDownArrow(false);//Enable/disable dropdown arrow.
    sheet.addValidationData(dataValidation_provider);//Add validation to sheet.

    //write spreadsheet object
    spreadsheetwrite(SpreadsheetObj,"‪#‎expandpath‬('./downloadsheet.xls')#",true);
</cfscript>
<cfoutput>Done</cfoutput>

2 个答案:

答案 0 :(得分:4)

错误信息非常清楚。您尝试添加到验证列表的某些值比Excel允许的最大长度长255个字符。要么缩短值,要么尝试this suggestion使用隐藏范围来解决限制。

答案 1 :(得分:0)

不确定这是否有任何帮助。这将在单元格中为您提供非常基本的是或否下拉列表

请注意......这适用于xssf NOT hssf(.xlsx文件格式)

<!--- Create an instance of a new spreadsheet --->
<cfset local.spreadsheet = spreadsheetNew("My Spreadsheet", true)>

<!--- we need all this so we can have dropdowns --->
<cfset local.workbook = local.spreadsheet.getWorkBook()>
<cfset local.worksheet = local.workbook.getSheet("My Spreadsheet")>
<cfset local.dataValidationConstraint = createObject("java","org.apache.poi.xssf.usermodel.XSSFDataValidationConstraint")>
<cfset local.cellRangeList = createObject("java","org.apache.poi.ss.util.CellRangeAddressList")>
<cfset local.dataValidationHelper = createObject("java","org.apache.poi.xssf.usermodel.XSSFDataValidationHelper").init(local.worksheet)>

<!--- Define cell list rowstart, rowend, column start, column end --->
<cfset local.list = local.cellRangeList.init(0, 1, 1, 1)>
<cfset local.dvconstraint = local.dataValidationHelper.createExplicitListConstraint(["YES","NO"])>
<cfset local.dataValidation = local.dataValidationHelper.createValidation(local.dvconstraint, local.list)>
<cfset local.dataValidation.setSuppressDropDownArrow(true)>

<!--- add it to the spreadsheet --->
<cfset local.worksheet.addValidationData(dataValidation)>