当我尝试创建xlsx时,我被困在创建头文件的过程中。我可以创建包含行和合并单元格的文件,但标题似乎永远无法使用。这是我所拥有的:
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添加标头会做同样的事情。
最佳答案
(从评论中提拔,以防答案对下一个人有用)
愚蠢的问题,但是您如何验证标题不存在?在Excel中,headers and footers should only be visible when printing (or in print preview mode)。
...页眉和页脚未显示在``普通''中的工作表上
视图-它们仅在页面布局视图和已打印页面中显示
页面。
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()#">