• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
//sr.setName(styleName);
}

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
 
reply
    Bookmark Topic Watch Topic
  • New Topic