Java – Apache POI removes cellstyle from the workbook
Using Apache poi... I used workbook Createcellstyle(), if I need to delete the created cellstyle after a period of time... How do I delete it from the workbook? Even if it is not used, I can still see that it still exists
What I need is like a workbook deleteCellStyle(cellStyle.getIndex());
Solution
As of r1391891, in addition to deleting duplicate cell styles, HSSF optimizer will also delete unused styles
So grab your latest nightly build / SVN checkout Version (or just wait a month or so for the 3.9-beta 1 release!), Then do the following:
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()
After that, optimsed Xls will not contain duplicate cell styles or unused cell styles (if the file does not already exist, you can easily perform optimization steps at the end of file creation)
Note – the HSSF optimizer method is only applicable to Xls file, not xssf Xlsx file It should be possible to summarize the method through less work, but now it is just HSSF