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

问题描述

...我用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());

推荐答案

,HSSFOptimiser还将删除未使用的样式,除了删除重复的单元格样式。

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

所以,抓住自己最近的每晚构建/ svn签版本(或只是等待一个月左右的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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 00:49