如何通过ApachePOI删除XLS的用户定义样式?

时间:2015-11-02 15:41:52

标签: java apache-poi poi-hssf hssf

我想为XLS删除一些预定义的样式 - 例如" Good"。对于XLSX,没有问题:创建新的CTCellStyle(不幸的是通过反射),setName(" Good"),setBuiltinId(26)和setHidden(true) - 现在Excel(2016)没有显示&#34 ;良好的"样式。我可以为XLS做这样的事吗?

修改

示例代码:

XLSX的隐藏风格 - 没有问题:

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) {}

XLS的隐藏风格:

如果工作簿中存在样式,我可以得到它,但如何将其设置为&#34;隐藏&#34;?

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) {}

即使我可以将样式标记为&#34;隐藏&#34;,还有其他问题:如果我从0迭代到internalWorkbook.getNumRecords(),我只会现有样式。因此,如果我自己创建工作簿,可能我应该创建新的StyleRecord和/或HSSFCellStyle并标记为&#34; hidden&#34;。我试过这个:

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

这是设置我自己&#34; Good&#34;的方法。样式。如果我不这样做,Excel(2016)将显示默认&#34; Good&#34;风格。

1 个答案:

答案 0 :(得分:1)

您应该可以使用HSSFWorkbook.getCellStyleAt(int index)来访问给定位置的样式。