スタイル付きの範囲データをコピー
Contents
[
Hide
]
範囲データのみをコピーセルの範囲から別の範囲にデータをコピーする方法を説明しました。 Aspose.Cells は、書式を設定した範囲をコピーすることもできます。この記事では、その方法について説明します。
スタイル付きの範囲データをコピー
Aspose.Cells は、範囲を操作するためのさまざまなクラスとメソッドを提供します。たとえば、createRange(), スタイルフラグ, [applyStyle()](https://reference.aspose.com/cells/java/com.aspose.cells/cells#applyStyle(com.aspose.cells.Style,%20com.aspose.cells.StyleFlag))など
この例:
- ワークブックを作成します。
- 最初のワークシートの多数のセルにデータを入力します。
- 範囲を作成します。
- 指定されたフォーマット属性を持つスタイル オブジェクトを作成します。
- スタイルをデータ範囲に適用します。
- セルの 2 番目の範囲を作成します。
- 最初の範囲から 2 番目の範囲に書式設定されたデータをコピーします。
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(CopyRangeDataWithStyle.class); | |
// Instantiate a new Workbook | |
Workbook workbook = new Workbook(); | |
// Get the first Worksheet Cells | |
Cells cells = workbook.getWorksheets().get(0).getCells(); | |
// Fill some sample data into the cells | |
for (int i = 0; i < 50; i++) { | |
for (int j = 0; j < 10; j++) { | |
cells.get(i, j).putValue(i + "," + j); | |
} | |
} | |
// Create a range (A1:D3). | |
Range range = cells.createRange("A1", "D3"); | |
// Create a style object. | |
Style style = workbook.createStyle(); | |
// Specify the font attribute. | |
style.getFont().setName("Calibri"); | |
// Specify the shading color. | |
style.setForegroundColor(Color.getYellow()); | |
style.setPattern(BackgroundType.SOLID); | |
// Specify the border attributes. | |
style.getBorders().getByBorderType(BorderType.TOP_BORDER).setLineStyle(CellBorderType.THIN); | |
style.getBorders().getByBorderType(BorderType.TOP_BORDER).setColor(Color.getBlue()); | |
style.getBorders().getByBorderType(BorderType.BOTTOM_BORDER).setLineStyle(CellBorderType.THIN); | |
style.getBorders().getByBorderType(BorderType.BOTTOM_BORDER).setColor(Color.getBlue()); | |
style.getBorders().getByBorderType(BorderType.LEFT_BORDER).setLineStyle(CellBorderType.THIN); | |
style.getBorders().getByBorderType(BorderType.LEFT_BORDER).setColor(Color.getBlue()); | |
style.getBorders().getByBorderType(BorderType.RIGHT_BORDER).setLineStyle(CellBorderType.THIN); | |
style.getBorders().getByBorderType(BorderType.RIGHT_BORDER).setColor(Color.getBlue()); | |
// Create the styleflag object. | |
StyleFlag flag = new StyleFlag(); | |
// Implement font attribute | |
flag.setFontName(true); | |
// Implement the shading / fill color. | |
flag.setCellShading(true); | |
// Implment border attributes. | |
flag.setBorders(true); | |
// Set the Range style. | |
range.applyStyle(style, flag); | |
// Create a second range (L9:O11) | |
Range range2 = cells.createRange("L9", "O11"); | |
// Copy the range data with formatting. | |
range2.copy(range); | |
// Save the Excel file. | |
workbook.save(dataDir + "CopyRangeDataWithFormatting.xlsx", SaveFormat.XLSX); |