Java Apache POI 克隆风格和获取风格的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24165124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Apache POI difference between cloning style and getting style
提问by Stupid.Fat.Cat
Are there any significant differences between doing:
执行以下操作是否有任何显着差异:
CellStyle newCellStyle = workbook.createCellStyle();
neweCellStyle.cloneStyleFrom(oldCell.getCellStyle());
newCell.setCellStyle(newCellStyle);
versus
相对
CellStyle newCellStyle = oldCell.getCellStyle();
newCell.setCellStyle(newCellStyle);
the reason why I ask this is because I'm not sure if by taking the first approach I risk creating too many CellStyles, I've encountered issues where if I create too many CellStyles in one particular workbook, all styling for the workbook disappear. So is there anything wrong with taking the second approach?
我之所以这么问是因为我不确定采用第一种方法是否会冒着创建过多 CellStyle 的风险,我遇到了一些问题,如果我在一个特定工作簿中创建了太多 CellStyle,工作簿的所有样式都会消失。那么采用第二种方法有什么问题吗?
采纳答案by Norbert Radyk
The first approach will create a new cell style for every new cell, which as you've rightly noticed might lead to the situation where you end up to the 'CellStyle explosion'.
第一种方法将为每个新单元格创建一个新的单元格样式,正如您正确注意到的那样,这可能会导致您最终出现“CellStyle 爆炸”的情况。
The potential benefit (or drawback - depending on your use case) of this solution is the situation where every cell references a different cell style object, so changes to the style of one cell won't impact any other cell (might be useful if you want to amend the style for only selected part of your spreadsheet in the future).
此解决方案的潜在好处(或缺点 - 取决于您的用例)是每个单元格引用不同单元格样式对象的情况,因此对一个单元格样式的更改不会影响任何其他单元格(如果您想在将来只修改电子表格选定部分的样式)。
In the second approach, all cells referencing the same style would be modified, though there is a neat way around this (when necessary) by using CellUtil
which you can find covered in more details in another Stack Overflow post here.
在第二种方法中,所有引用相同样式的单元格都将被修改,尽管有一种巧妙的方法可以解决这个问题(必要时)CellUtil
,您可以在此处的另一篇 Stack Overflow 帖子中找到更多详细信息。