Skip to content
Julian Halliwell edited this page Mar 8, 2023 · 5 revisions

Returns a new POI CellStyle object based on the formatting properties passed in.

createCellStyle( workbook, format )

Required arguments

Chainable? Yes, but ends the chain.

Notes

Each time you use formatCell() or any of the formatting methods, POI "registers" a new CellStyle inside the spreadsheet workbook, even if the format is the same (although see update below). There is a limit on the number of CellStyles that can be added: 4,000 styles for ".xls" spreadsheets, 64,000 for ".xlsx" workbooks.

If you are applying the same format with more than one call of any of the formatting functions within the same workbook, you should consider passing the format as a CellStyle object instead. This will mean only one CellStyle is created within the workbook.

Use this method to convert your common format properties to a POI CellStyle object before passing it into the formatting methods.

Update: from version 3.8.1

The library will now try to cache and re-use the cellStyle objects created from the format struct you pass in. For full control and best performance though, creating and passing your own cellStyle objects is still recommended.

Example

Create a single CellStyleobject from a common format and apply it to rows in different sheets. Only one "style" is registered in the workbook.

spreadsheet = New spreadsheet();
workbook = spreadsheet.new();
// check the number of registered styles for this workbook
WriteDump( spreadsheet.getWorkbookCellStylesTotal( workbook ) ); 
commonFormat = { font: "Georgia", fontsize: 9 }; 
commonCellStyle = spreadsheet.createCellStyle( workbook, commonFormat );
spreadsheet.addRows( workbook, [ "Frumpo", "McNugget" ] );
spreadsheet.formatRow( workbook=workbook, format=commonCellStyle, row=1 );
spreadsheet.createSheet( workbook );
spreadsheet.setActiveSheetNumber( workbook, 2 );
spreadsheet.addRows( workbook, [ "Emilia", "Tenterhook" ] );
spreadsheet.formatRow( workbook=workbook, format=commonCellStyle, row=1 );
// Now check the number of styles
WriteDump( spreadsheet.getWorkbookCellStylesTotal( workbook ) ); // only 1 additional style is registered
Clone this wiki locally