2016-06-14 184 views
3

我见过this后,但看起来确实是一个解决方案。无论如何,我正在使用ColdFusion 10来生成Excel电子表格。但是,当我使用SpreadsheetFormatRow()并传入要格式化的行时,它只会执行大约3次,然后突然停止。下面是一个例子...SpreadsheetFormatRow突然停止工作

ColdFusion代码

<cfscript> 

    rowCount = 1; 
    headingRows = 4; 

    // Create instance of new Spreadsheet 
    excelSheet = SpreadsheetNew("ReportName",false); 

    // HEADING (IMAGE) ROW FORMAT 
    formatHeadingRow = StructNew(); 
    formatHeadingRow.fgcolor="blue";   

    // Add rows to fill the header area (must add as many as we are spanning with the above image) 
    for (x=0;x<headingRows;x++) { 
     SpreadsheetAddRow(excelSheet,"TEST,TEST,TEST,TEST,TEST,TEST,TEST,TEST,TEST,TEST,TEST,TEST"); 
     SpreadsheetFormatRow(excelSheet,formatHeadingRow,rowCount); 
     rowCount++; 
    } 

</cfscript> 

<!--- stream it to the browser ---> 
<cfheader name="Content-Disposition" value="inline; filename=reportName.xls"> 
<cfcontent type="application/vnd.ms-excel" variable="#SpreadSheetReadBinary(excelSheet)#"> 

,这里是生成的Excel工作表

enter image description here

的为什么格式化十大之后停止截图行数和单元格数? 如果我切换到使用XML格式

excelSheet = SpreadsheetNew("ReportName",true); 

它工作正常。不过,我为我的颜色使用自定义调色板,所以我不认为切换到XLSX格式对我来说很有用。当我尝试然后致电

palette = excelSheet.getWorkbook().getCustomPalette(); 

我收到一个错误,指出getCustomPalette()方法未定义。

coldfusion.runtime.java.MethodSelectionException: The getcustompalette method was not found 

任何人都可以帮我解决这个问题吗?谢谢!!!

甚至更​​好,因为它的工作原理与XML格式,任何人都可以显示例如如何使用自定义调色板与XLSX(XML格式)

+0

通过trycf只是想你的代码,它工作正常的CF10 http://trycf.com/gist/ce1e66bed5fe5d1bc40f8abd79f8fc72/acf?theme=monokai –

+0

@JohnWhish我只是用你的trycf.com链接尝试,它给了我同样的结果。几行后格式破碎,最后几个单元格保持白色而不是蓝色。什么版本的Excel?我的Excel 2016与Office 365 – Phil

回答

3

这是XLS文件打交道时,我所看到的经常是一个问题来自CF;他们似乎停止在特定数量的单元格后应用样式。我已经能够通过输出到xlsx来解决它。 (我是能够复制并通过这样做“修理”你的问题。)

excelSheet = SpreadsheetNew("ReportName",true); 

...

<cfheader name="Content-Disposition" value="inline; filename=reportName.xlsx"> 
<cfcontent type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" 
     variable="#SpreadSheetReadBinary(excelSheet)#"> 
+0

虽然这个工作,我需要使用我的电子表格自定义调色板,所以我不限于HSSF预定义的颜色。当我尝试使用XLSL选项时,getCustomPalette()方法在工作簿对象中不再可用。 – Phil

3

既然你申请完全一样的格式到所有的行,只有做到这一点一次,不在每一行上。循环使用后应SpreadsheetFormatCellRange解决问题:

SpreadsheetFormatCellRange(excelSheet 
          , formatHeadingRow 
           , startRow 
           , startCol 
           , endRow 
           , endCol); 

嫌疑这个问题在某种程度上涉及回Excel's maximum style limits。由于CF是一个黑匣子,很难知道它实际上创建了多少样式,或者确切地说它们是如何应用的。但是,根据我的经验,即使不知道它,也很容易超出样式限制。特别是在使用较旧的.xls文件格式时,其限制要低得多。这就是为什么我建议using the newer .xlsx format instead

getCustomPalette()方法未定义。

正确。它在XSSF中不存在。如您在其他线程中提到的,是否有某些原因需要自定义调色板,而不仅仅是defining your own colors

+0

实现您在我的其他帖子上提供的示例以获取我的自定义颜色,并切换到XSLX格式。 – Phil

+0

公平地说,如果你打算切换到xlsx,@TimJasko建议首先:)虽然上述确实解决了.xls的问题,但实际上它只是延长了样式所不可避免的问题。更好的选择是切换到.xlsx,它具有更多的功能和更高的样式限制。 – Leigh