スタイル オブジェクトの再利用
Contents
[
Hide
]
スタイル オブジェクトを再利用すると、メモリが節約され、プログラムの実行速度が向上します。
ワークシート内の広範囲のセルに書式を適用するには:
- スタイル オブジェクトを作成します。
- 属性を指定します。
- 範囲内のセルにスタイルを適用します。
This file contains hidden or 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 hidden or 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](https://reference.aspose.com/cells/java/com.aspose.cells/cell#getStyle() ) と[Cell.setStyle](https://reference.aspose.com/cells/java/com.aspose.cells/cell#setStyle(com.aspose.cells.Style) メソッドはメモリ使用量が大幅に少なく効率的であり、古いほどCell.getStyle()不要なメモリを大量に消費するプロパティは、のリリースで削除されましたAspose.Cells 7.1.0.