Win a copy of Machine Learning with R: Expert techniques for predictive modeling this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

ClassCastException when calling org.apache.poi.hssf.usermodel.HSSFOptimiser.optimiseCellStyles

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am just curious about one thing on the org.apache.poi.hssf.usermodel.HSSFCellStyle

HSSFCellStyle.setUserStyleName will create a style record, which is a form of an XF but is incompatible to the
HSSFWorkbook.getCellStyleAt( short index ) as, though I'm not fully sure, workbook.getExFormatAt( short index )
expects an ExtendedFormatRecord but setUserStyleName creates a Style Record. You can't convert an ExtendedFormatRecord to
a StyleRecord as the classes are siblings of each other and will get a ClassCastException after using something like
HSSFOptimiser.optimiseCellStyle( HSSFWorkbook wb ). My suggestion is to use something like InternalWorkbook.createCellStyleXf in the setUserStyleName function. So this is a suggested way of the function:

public void setUserStyleName(String styleName) {
ExtendedFormatRecord efr = _workbook.getExFormatRecord(_index);
if(efr == null) {
efr = _workbook.createCellXF();
// All Style records start as "builtin", but generally
// only 20 and below really need to be
if(sr.isBuiltin() && _index <= 20) {
throw new IllegalArgumentException("Unable to set user specified style names for built in styles!");
// The name needs to be defined as a member in ExtendedFormatRecord

public String getUserStyleName() {
ExtendedFormatRecord efr = _workbook.getExFormatRecord(_index);
if(efr == null) {
return null;
if(efr.isBuiltin()) {
return null;
return efr.getName();

But the ExtendedFormatRecord needs to have the isBuiltin defined using perhaps the example from the StyleRecord class
Yup, yup, yup. Tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!