gpt4 book ai didi

coldfusion - Apache POI XSSF header 未创建

转载 作者:行者123 更新时间:2023-12-04 01:27:18 26 4
gpt4 key购买 nike

当我尝试创建 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com