Search code examples
coldfusionapache-poicoldfusion-10

Apache POI XSSF header not created


As I experiment with xlsx creation, I'm stuck on creating headers. I'm able to create a file with rows and merged cells, but headers never seem to work. Here's what I have:

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();

When I run this code, no errors are thrown. The file is created and can be opened without throwing any errors, but no headers appear. And like I said, if I create rows/cells instead of a header, those are created correctly. What am I missing?

EDIT: As Leigh points out below, headers/footers have a different meaning in Excel than how I was thinking of them (as in PDFs). I got thrown off by the way adding a header in Excel shows it above the first row, and thought that adding one through POI would do the same thing.


Solution

  • (Promoted from comments, in case the answer is helpful for the next guy)

    Silly question, but how are you verifying the headers are not present? In 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, the code works fine for me under CF10 and 11, after I populated at least one cell (so there was something to print).

    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()#">