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