Excel の範囲をコピーする
Contents
[
Hide
]
紹介
Excel では、範囲を選択し、範囲をコピーし、その後、同じワークシート、他のワークシート、または他のファイルに特定のオプションで貼り付けることができます。
Aspose.Cells を使用して範囲をコピーする
Aspose.Cellsは、範囲をコピーするいくつかのオーバーロードRange.Copyメソッドを提供しています。
範囲をコピー
ソース範囲、ターゲット範囲を作成し、その後、Range.Copy メソッドを使用してソース範囲をターゲット範囲にコピーします。
以下のコードを参照してください:
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
// Instantiate a new Workbook. | |
Workbook workbook = new Workbook(); | |
// Get all the worksheets in the book. | |
WorksheetCollection worksheets = workbook.getWorksheets(); | |
// Get the first worksheet in the worksheets collection. | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Create a range of cells. | |
Range sourceRange = worksheet.getCells().createRange("A1", "A2"); | |
// Input some data with some formats into a few cells in the range. | |
sourceRange.get(0, 0).putValue("Test"); | |
sourceRange.get(1, 0).putValue("123"); | |
// Create target range of cells. | |
Range targetRange = worksheet.getCells().createRange("B1", "B2"); | |
// Copy source range to target range in the same workhseet | |
targetRange.copy(sourceRange); | |
// Create target range of cells. | |
workbook.getWorksheets().add(); | |
worksheet = workbook.getWorksheets().get(1); | |
targetRange = worksheet.getCells().createRange("A1", "A2"); | |
// Copy source range to target range in another workhseet | |
targetRange.copy(sourceRange); | |
//Copy to another workbook | |
Workbook anotherWorkbook = new Workbook(); | |
worksheet = workbook.getWorksheets().get(0); | |
targetRange = worksheet.getCells().createRange("A1", "A2"); | |
// Copy source range to target range in another workbook | |
targetRange.copy(sourceRange); |
オプションで範囲を貼り付ける
Aspose.Cells は特定のタイプで範囲を貼り付ける機能をサポートしています。
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
// Instantiate a new Workbook. | |
Workbook workbook = new Workbook(); | |
// Get all the worksheets in the book. | |
WorksheetCollection worksheets = workbook.getWorksheets(); | |
// Get the first worksheet in the worksheets collection. | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Create a range of cells. | |
Range sourceRange = worksheet.getCells().createRange("A1", "A2"); | |
// Input some data with some formats into a few cells in the range. | |
sourceRange.get(0, 0).putValue("Test"); | |
sourceRange.get(1, 0).putValue("123"); | |
// Create target range of cells. | |
Range targetRange = worksheet.getCells().createRange("B1", "B2"); | |
// Init paste options. | |
PasteOptions options = new PasteOptions(); | |
// Set paste type. | |
options.setPasteType(PasteType.VALUES_AND_FORMATS); | |
options.setSkipBlanks(true); | |
// Copy source range to target range | |
targetRange.copy(sourceRange, options); |
範囲のデータのみをコピーします。
次のコードのようにRange.CopyDataメソッドを使用してデータをコピーすることもできます:
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
// Instantiate a new Workbook. | |
Workbook workbook = new Workbook(); | |
// Get all the worksheets in the book. | |
WorksheetCollection worksheets = workbook.getWorksheets(); | |
// Get the first worksheet in the worksheets collection. | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Create a range of cells. | |
Range sourceRange = worksheet.getCells().createRange("A1", "A2"); | |
// Set a few cells in the range. | |
sourceRange.get(0, 0).putValue("Test"); | |
sourceRange.get(1, 0).putValue("123"); | |
// Create target range of cells. | |
Range targetRange = worksheet.getCells().createRange("B1", "B2"); | |
//Only copy data the range. | |
targetRange.CopyData(sourceRange); |