重用样式对象
Contents
[
Hide
]
重用样式对象可以节省内存并使程序执行更快。
若要对工作表中的大范围单元格应用一些格式设置:
- 创建一个样式对象。
- 指定属性。
- 将样式应用于范围中的单元格。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(ReuseStyleObjects.class); | |
// Create an instance of Workbook & load an existing spreadsheet | |
Workbook workbook = new Workbook(dataDir + "Book1.xls"); | |
// Retrieve the Cell Collection of the first Worksheet | |
Cells cells = workbook.getWorksheets().get(0).getCells(); | |
// Create an instance of Style and add it to the pool of styles | |
Style styleObject = workbook.createStyle(); | |
// Retrieve the Font object of newly created style | |
Font font = styleObject.getFont(); | |
// Set the font color to Red | |
font.setColor(Color.getRed()); | |
// Set the newly created style on two different cells | |
cells.get("A1").setStyle(styleObject); | |
cells.get("A2").setStyle(styleObject); |
上述讨论的相同流程也可以按照以下方式完成。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cells.get("A1").getStyle().getFont().setColor(Color.getRed()); | |
cells.get("A2").getStyle().getFont().setColor(Color.getRed()); |
因为 Cell.getStyle 和 Cell.setStyle 方法使用的内存较少且高效,旧的 Cell.getStyle() 属性消耗了大量不必要的内存,在 Aspose.Cells 7.1.0 发布时被移除。