I want remove some predefined styles for XLS - for example "Good". For XLSX there is no problem: create new CTCellStyle (unfortunatelly by reflection), setName("Good"), setBuiltinId(26) and setHidden(true) - now Excel (2016) doesn’t show "Good" style. Can I do sth like this for XLS?
EDIT
Sample code:
Hidding style for XLSX - there is no problem:
StylesTable styleSource = xssfWorkbook.getStylesSource(); // xssfWorkbook is instance of XSSFWorkbook
try {
// get ctCellStyles (by reflection)
Field field = StylesTable.class.getDeclaredField("doc");
field.setAccessible(true);
Object obj = field.get(styleSource);
StyleSheetDocument ssd = (StyleSheetDocument) obj;
CTStylesheet ctStyleSheet = ssd.getStyleSheet();
CTCellStyles ctCellStyles = ctStyleSheet.getCellStyles();
// find style "Good"
for (int i = 0; i < ctCellStyles.sizeOfCellStyleArray(); i++) {
CTCellStyle ctCellStyle = ctCellStyles.getCellStyleArray(i);
if (ctCellStyle.getName().equals("Good")) {
XmlBoolean hiddenXml = XmlBoolean.Factory.newInstance();
hiddenXml.setStringValue("1");
ctCellStyle.xsetHidden(hiddenXml);
}
}
} catch (Exception e) {}
Hidding style for XLS:
If style exists in workbook I can get it, but how to set it as "hidden"?
try {
// get InternalWorkbook (by reflection)
Field field = HSSFWorkbook.class.getDeclaredField("workbook");
field.setAccessible(true);
Object iwb = field.get(hssfWorkbook); // hssfWorkbook is instance of HSSFWorkbook
InternalWorkbook internalWorkbook = (InternalWorkbook) iwb;
// find style "Good"
for (int xfIndex = 0; xfIndex < internalWorkbook.getNumRecords(); xfIndex++) {
// try to get every record as StyleRecord from internalWorkbook
StyleRecord styleRecord = internalWorkbook.getStyleRecord(xfIndex);
if (styleRecord != null && styleRecord.getName() != null) {
if (styleRecord.getName().equals("Good")) {
new DebugUtil(styleRecord.getName());
// TODO set here sth like "hidden" for styleRecord or maybe:
// get style with current id from workbook
HSSFCellStyle hssfCellStyle = hssfWorkbook.getCellStyleAt((short) xfIndex); // workbook is instance of org.apache.poi.ss.usermodel.Workbook
// TODO set here sth like "hidden" for hssfCellStyle
}
}
}
} catch (Exception e) {}
Even If I could mark style as "hidden", there is other problem: If I iterate from 0
to internalWorkbook.getNumRecords()
I get only existing styles. So if I'm creating workbook self, probably I should create new StyleRecord and/or HSSFCellStyle and mark as "hidden". I tried this:
int size = internalWorkbook.getSize();
StyleRecord newStyleRecord = internalWorkbook.createStyleRecord(size);
HSSFCellStyle newHssfCellStyle = hssfWorkbook.createCellStyle();
newHssfCellStyle.setAlignment((short) 3); // align right, for tests, to see difference between original and created "Good" style
newStyleRecord.setName("Good");
// TODO set here sth like "hidden" for newStyleRecord and/or for newHssfCellStyle
This is the way to set my own "Good" style. If I don't do it, Excel (2016) will show default "Good" style.