行と列のコピー
紹介
時には、ワークシート全体をコピーせずに行や列をコピーする必要があります。Aspose.Cellsを使用すると、ワークブック内またはワークブック間で行や列をコピーすることができます。 行(または列)をコピーすると、それに含まれるデータ(更新された参照を含む数式、値、コメント、書式設定、非表示セル、画像、その他の図形オブジェクトなど)がコピーされます。
Microsoft Excelで行や列をコピーする方法
- コピーしたい行または列を選択します。
- 行または列をコピーする場合は、標準ツールバーのコピーをクリックするか、CTRL+Cを押します。
- コピーする選択範囲の下または右側に行または列を選択します。
- 行または列をコピーする際に、挿入メニューでコピーしたセルをクリックします。
Microsoft Excelを使用した貼り付けオプションを使用した行や列の貼り付け方法
- コピーしたいデータやその他の属性を含むセルを選択します。
- コピーをクリックしてHomeタブを選択します。
- 貼り付けしたいエリア内で最初のセルをクリックします。
- Homeタブで、貼り付けの横にある矢印をクリックし、貼り付けを選択します。
- 希望するオプションを選択します。
Aspose.Cells for .NETを使用して行と列をコピーする方法
単一の行をコピーする方法
Aspose.Cellsは、Cells クラスのCopyRow メソッドを提供します。このメソッドは、数式、値、コメント、セルの書式、非表示セル、画像など、すべての種類のデータをコピーします。
CopyRowメソッドは以下のパラメーターを取ります:
- ソースのCellsオブジェクト
- ソースの行インデックス、および
- 宛先の行インデックス。
このメソッドを使用すると、シート内または他のシートへの行のコピーが可能です。このCopyRow メソッドはMicrosoft Excelと同様に動作します。つまり、例えば、宛先行の高さを明示的に設定する必要はありません。その値もコピーされます。
以下の例は、ワークシート内の行をコピーする方法を示しています。テンプレートのMicrosoft Excelファイルを使用し、2番目の行(データ、書式設定、コメント、画像などを含む)を12番目の行に貼り付けます。
Cells.GetRowHeight メソッドを使用してソース行の高さを取得し、Cells.SetRowHeight メソッドを使用して宛先行の高さを設定する手順は省略できます。なぜなら、CopyRow メソッドが自動的に行の高さを処理するからです。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Open the existing excel file. | |
Workbook excelWorkbook1 = new Workbook(dataDir + "book1.xls"); | |
// Get the first worksheet in the workbook. | |
Worksheet wsTemplate = excelWorkbook1.Worksheets[0]; | |
// Copy the second row with data, formattings, images and drawing objects | |
// To the 16th row in the worksheet. | |
wsTemplate.Cells.CopyRow(wsTemplate.Cells, 1, 15); | |
// Save the excel file. | |
excelWorkbook1.Save(dataDir + "output.xls"); |
行をコピーする際は、関連する画像、グラフ、またはその他の描画オブジェクトに注目することが重要です。これはMicrosoft Excelと同じです。
- もしソース行インデックスが5であれば、画像、グラフなどはその3行に含まれている場合にコピーされます(開始行インデックスが4で終了行インデックスが6の場合)。
- 宛先行にある既存の画像やグラフなどは削除されません。
複数の行をコピーする方法
追加の整数型パラメーターを取るメソッドCells.CopyRowsを使用して、新しい宛先に複数の行をコピーすることもできます。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create an instance of Workbook class by loading the existing spreadsheet | |
Workbook workbook = new Workbook(dataDir + "aspose-sample.xlsx"); | |
// Get the cells collection of worksheet by name Rows | |
Cells cells = workbook.Worksheets["Rows"].Cells; | |
// Copy the first 3 rows to 7th row | |
cells.CopyRows(cells, 0, 6, 3); | |
// Save the result on disc | |
workbook.Save(dataDir + "output_out.xlsx"); |
列をコピーする方法
Aspose.CellsはCellsクラスのCopyColumnメソッドを提供しており、このメソッドは、ソース列から宛先列へ数式(更新された参照を含む)、値、コメント、セル形式、非表示セル、画像、および他の描画オブジェクトなどを含むあらゆるタイプのデータをコピーします。
CopyColumnメソッドは以下のパラメーターを取ります:
- ソースのCellsオブジェクト
- ソースの列インデックス、および
- 宛先の列インデックス。
シート内または他のシートへの列のコピーにはCopyColumnメソッドを使用します。
この例では、ワークシートから列をコピーして別のブック内のワークシートに貼り付けます。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create another Workbook. | |
Workbook excelWorkbook1 = new Workbook(dataDir + "book1.xls"); | |
// Get the first worksheet in the book. | |
Worksheet ws1 = excelWorkbook1.Worksheets[0]; | |
// Copy the first column from the first worksheet of the first workbook into | |
// The first worksheet of the second workbook. | |
ws1.Cells.CopyColumn(ws1.Cells, ws1.Cells.Columns[0].Index, ws1.Cells.Columns[2].Index); | |
// Autofit the column. | |
ws1.AutoFitColumn(2); | |
// Save the excel file. | |
excelWorkbook1.Save(dataDir + "output.xls"); |
複数の列をコピーする方法
Cells.CopyRowsメソッドと同様に、Aspose.CellsのAPIは新しい位置に複数のソース列をコピーするためのCells.CopyColumnsメソッドも提供しています。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create an instance of Workbook class by loading the existing spreadsheet | |
Workbook workbook = new Workbook(dataDir + "aspose-sample.xlsx"); | |
// Get the cells collection of worksheet by name Columns | |
Cells cells = workbook.Worksheets["Columns"].Cells; | |
// Copy the first 3 columns 7th column | |
cells.CopyColumns(cells, 0, 6, 3); | |
// Save the result on disc | |
workbook.Save(dataDir + "output_out.xlsx"); |
貼り付けオプションを使用して行と列を貼り付ける方法
Aspose.Cellsは現在、関数CopyRowsおよびCopyColumnsを使用してPasteOptionsを提供しています。これにより、Excelと同様の適切な貼り付けオプションを設定することができます。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
//Source directory | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
//Output directory | |
string outputDir = RunExamples.Get_OutputDirectory(); | |
// Load sample excel file | |
Workbook wb = new Workbook(sourceDir + "sampleChangeChartDataSource.xlsx"); | |
// Access the first sheet which contains chart | |
Worksheet source = wb.Worksheets[0]; | |
// Add another sheet named DestSheet | |
Worksheet destination = wb.Worksheets.Add("DestSheet"); | |
// Set CopyOptions.ReferToDestinationSheet to true | |
CopyOptions options = new CopyOptions(); | |
options.ReferToDestinationSheet = true; | |
// Set PasteOptions | |
PasteOptions pasteOptions = new PasteOptions(); | |
pasteOptions.PasteType = PasteType.Values; | |
pasteOptions.OnlyVisibleCells = true; | |
// Copy all the rows of source worksheet to destination worksheet which includes chart as well | |
// The chart data source will now refer to DestSheet | |
destination.Cells.CopyRows(source.Cells, 0, 0, source.Cells.MaxDisplayRange.RowCount, options, pasteOptions); | |
// Save workbook in xlsx format | |
wb.Save(outputDir + "outputChangeChartDataSource.xlsx", SaveFormat.Xlsx); |