Apache POI 从工作簿中删除 CellStyle [英] Apache POI delete CellStyle from workbook

查看:47
本文介绍了Apache POI 从工作簿中删除 CellStyle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 apache POI ...我使用了 workbook.CreateCellStyle(),如果一段时间后我需要删除创建的 CellStyle ...如何将其从工作簿中删除?即使未使用,我也可以看到它仍然存在.

Using apache POI ... I used workbook.CreateCellStyle(), if after a while I needed to delete the CellStyle created ... How do I remove it from the workbook? I can see it still remains even if it is unused.

我需要的是类似 workbook.deleteCellStyle(cellStyle.getIndex());

What I need is something like workbook.deleteCellStyle(cellStyle.getIndex());

推荐答案

As of r1391891,HSSFOptimiser 除了删除重复的单元格样式外,还将删除未使用的样式.

As of r1391891, HSSFOptimiser will also remove un-used styles, in addition to removing duplicate cell styles.

因此,为自己准备一个最近的每晚构建/svn checkout 构建(或者等待一个月左右的 3.9-beta1 发布!),然后执行以下操作:

So, grab yourself a recent nightly build / svn checkout build (or just wait for the 3.9-beta1 release in a month or so!), and then do something like:

NPOIFSFileSystem poifs = new NPOIFSFileSystem(new File("/path/to/excel/file.xls"));
HSSFWorkbook wb = new HSSFWorkbook(poifs.getRoot());
HSSFOptimiser.optimiseCellStyles(wb);

FileOutputStream fout = new FileoutputStream("optimised.xls");
wb.write(fout);
fout.close()

之后,optimsed.xls 将不包含重复的单元格样式,也不会包含未使用的单元格样式.(如果文件不存在,您可以轻松地将优化步骤放在创建文件的末尾)

After that, optimsed.xls will contain no duplicated cell styles, and no un-used cell styles. (You could easily put the optimise step at the end of creating the file, if it's not already existing)

注意 - HSSFOptimiser 方法仅适用于 .xls 文件,不适用于 XSSF .xlsx 文件.应该可以在不做太多工作的情况下推广该方法,但目前仅适用于 HSSF....

Note - the HSSFOptimiser approach will only work for .xls files, not for XSSF .xlsx ones. It should be possible to generalise the approach with not too much work, but for now it's HSSF only....

这篇关于Apache POI 从工作簿中删除 CellStyle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆