coldfusion - Apache POI XSSF header 未创建

标签 coldfusion apache-poi coldfusion-10

当我尝试创建 xlsx 时,我一直在创建 header 。我能够创建一个包含行和合并单元格的文件,但标题似乎永远不起作用。这是我所拥有的:

var WorkBook = CreateObject( 
    "java",
    "org.apache.poi.xssf.usermodel.XSSFWorkbook"
).Init();

var Sheet = WorkBook.CreateSheet(
    JavaCast( "string", 'my sheetname' )
);

// create the default header if it doesn't exist
var header = sheet.getHeader(); // have also tried getEvenHeader() and getOddHeader()

header.setText('&LLeft Section');
// have also tried the following:
//header.setLeft('left header');
//header.setCenter('CENTER');
//header.setRight('right header');

// open the file stream
var FileOutputStream = CreateObject(
    "java",
    "java.io.FileOutputStream"
).Init(
    JavaCast( "string", filename )
);

// Write the workbook data to the file stream.
WorkBook.Write( 
    FileOutputStream 
);

// Close the file output stream.
FileOutputStream.Close();

当我运行此代码时,没有抛出任何错误。文件已创建并且可以打开而不会引发任何错误,但不会出现标题。就像我说的,如果我创建行/单元格而不是标题,那么它们就会正确创建。我错过了什么?

编辑: 正如 Leigh 在下面指出的那样,页眉/页脚在 Excel 中的含义与我对它们的理解(如在 PDF 中)不同。我对在 Excel 中添加标题将其显示在第一行上方的方式感到困惑,并认为通过 POI 添加标题会做同样的事情。

最佳答案

(从评论中推广,以防答案对下一个人有帮助)

愚蠢的问题,但是你如何验证 header 不存在?在 Excel 中,headers and footers should only be visible when printing (or in print preview mode)

...Headers and footers are not displayed on the worksheet in Normal view — they are displayed only in Page Layout view and on the printed pages.

FWIW,在我填充至少一个单元格后(因此有一些东西要打印),该代码在 CF10 和 11 下对我来说工作得很好。

Runnable Example on trycf.com

<cfscript>
    workBook = CreateObject( "java", "org.apache.poi.xssf.usermodel.XSSFWorkbook").Init();
    sheet = WorkBook.CreateSheet( JavaCast( "string", 'my sheetname' ) );
    header = sheet.getHeader(); // have also tried getEvenHeader() and getOddHeader()
    header.setText('&LLeft Section');
    // add some data so there is something to print
    sheet.createRow(0).createCell(0).setCellValue("sample value");
    // Using binary stream because trycf.com does not support files for security reasons
    baos = createObject("java", "java.io.ByteArrayOutputStream").init();
    // Write the workbook data to the binary stream
    workBook.write( baos );
    baos.close();
</cfscript>

<!--- CF10 lacks support for script version of cfcontent --->
<cfcontent type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
    variable="#baos.toByteArray()#">

关于coldfusion - Apache POI XSSF header 未创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38596796/

相关文章:

coldfusion - SpreadsheetFormatRow 突然停止工作

tomcat - 如何声明/var/www为Tomcat和Railo ROOT?

java - HSSFSheet 类型的 getCell(int) 方法未定义

web-services - 无法添加最基本的 RESTful Web 服务

java - Excel 中显示的 Apache POI : getting a number as a string,

Java Apache 兴趣点

coldfusion - ColdFusion 8 和 ColdFusion 10 之间的 serializeJSON

ColdFusion 10 cffile 操作 ="upload"接受属性通配符不起作用

coldfusion - 如何在 CFscript 中指定参数属性? (CF9)

orm - ColdFusion ORM 在使用虚拟目录时找不到 CFC