行と列のコピー

序章

ワークシート全体をコピーせずに、ワークシートの行と列をコピーする必要がある場合があります。 Aspose.Cells を使用すると、ブック内またはブック間で行と列をコピーできます。 行 (または列) がコピーされると、そこに含まれるデータ (参照が更新された数式を含む) と、値、コメント、書式設定、非表示のセル、画像、およびその他の描画オブジェクトもコピーされます。

Microsoft Excel で行と列をコピーする

  1. コピーする行または列を選択します。
  2. 行または列をコピーするには、コピー上で標準ツールバー、またはCTRL+C.
  3. 選択範囲をコピーする場所の下または右にある行または列を選択します。
  4. 行または列をコピーする場合は、Cellsをコピーしました上で入れるメニュー。

Microsoft Excel で貼り付けオプションを使用して行と列を貼り付ける

  1. コピーするデータまたはその他の属性を含むセルを選択します。
  2. [ホーム] タブで、コピー.
  3. 目的の領域の最初のセルをクリックします。ペーストあなたがコピーしたもの。
  4. [ホーム] タブで、ペーストを選択し、ペースト特別な。
  5. を選択オプションあなたがしたい。

Aspose.Cells を使用

単一行のコピー

Aspose.Cells は行のコピーの方法Cellsクラス。このメソッドは、数式、値、コメント、セル形式、非表示のセル、画像、およびその他の描画オブジェクトを含むすべての種類のデータをソース行から宛先行にコピーします。

行のコピーメソッドは次のパラメータを取ります。

  • 起源Cells物体、
  • ソース行インデックス、および
  • 宛先行インデックス。

このメソッドを使用して、シート内の行をコピーするか、別のシートにコピーします。の行のコピーメソッドは、Microsoft Excel と同様に機能します。したがって、たとえば、宛先行の高さを明示的に設定する必要はありません。その値もコピーされます。

次の例は、ワークシートの行をコピーする方法を示しています。テンプレート Microsoft の Excel ファイルを使用し、2 行目を (データ、書式設定、コメント、画像などを含めて) コピーし、同じワークシートの 12 行目に貼り付けます。

を使用してソース行の高さを取得するステップをスキップできます。Cells.GetRowHeightメソッドを使用して、目的の行の高さを設定します。Cells.SetRowHeightメソッドとして行のコピーメソッドは行の高さを自動的に処理します。

// 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");

複数行のコピー

を使用しながら、複数の行を新しい宛先にコピーすることもできます。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クラスの場合、このメソッドは、数式 (更新された参照を含む) および値、コメント、セル形式、非表示のセル、画像、およびその他の描画オブジェクトを含むすべての種類のデータをソース列から宛先列にコピーします。

コピー列メソッドは次のパラメータを取ります。

  • 起源Cells物体、
  • ソース列インデックス、および
  • 宛先列のインデックス。

使用コピー列シート内または別のシートに列をコピーするメソッド。

次の使用例は、ワークシートから列をコピーし、別のブックのワークシートに貼り付けます。

// 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 が提供するようになりました貼り付けオプション関数の使用中行のコピーコピー列. 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);