普段の Excel 操作では、「Ctrl+C」でコピーし、「Ctrl+V」でペーストするという手順が当たり前のように感じられます。しかし、Infragistics の Excel ライブラリを使ってプログラム上でセルのコピー&ペーストを行おうとすると、「あれ、ペーストってどうやるの?」という疑問にぶつかります。
❓ 「コピー」と「ペースト」はどこにある?
実はこのライブラリには、コピーやペーストといった専用のメソッドは存在しません。
その代わりに、「値」や「フォーマット」を明示的に代入することで、「コピー&ペースト」が実現されます。
✅ コピーとペーストの区別がない
Infragistics の Excel ライブラリでは、コピーとペーストの区別がありません。
値をコピーして別のセルに代入する処理そのものが、コピー&ペーストを意味します。
// セルA1の値をA2にコピー(=ペースト)している worksheet.Rows[1].Cells[0].Value = worksheet.Rows[0].Cells[0].Value;
このように、値をそのまま代入するだけで、Excel 上でのコピー&ペーストと同じ効果が得られます。
ペースト専用の命令があるわけではなく、「代入」こそがコピー&ペーストという考え方になります。
📌 範囲コピーも手動処理が基本
Excel では範囲選択して一括でコピー&ペーストすることができますが、Infragistics の Excel ライブラリには範囲コピーや範囲ペーストのための API は存在しません。
そのため、コピー元の範囲と貼り付け先の範囲をループなどで走査しながら、1セルずつ個別に処理する必要があります。
📌 書式もコピーしたいときは?
値だけでなく、フォント・色・罫線・背景色などのフォーマット(書式)もコピーしたい場合は、以下のように CellFormat の各プロパティを個別に代入していきます。(設定できるフォーマットの一部です)
private void CopyCellFormat(WorksheetCell sourceCell, WorksheetCell targetCell) { // フォントの設定 targetCell.CellFormat.Font.Bold = sourceCell.CellFormat.Font.Bold; targetCell.CellFormat.Font.Italic = sourceCell.CellFormat.Font.Italic; targetCell.CellFormat.Font.Name = sourceCell.CellFormat.Font.Name; targetCell.CellFormat.Font.Height = sourceCell.CellFormat.Font.Height; targetCell.CellFormat.Font.ColorInfo = sourceCell.CellFormat.Font.ColorInfo; // 塗りつぶし targetCell.CellFormat.Fill = sourceCell.CellFormat.Fill; // アライメント targetCell.CellFormat.Alignment = sourceCell.CellFormat.Alignment; targetCell.CellFormat.WrapText = sourceCell.CellFormat.WrapText; // 罫線 targetCell.CellFormat.BottomBorderStyle = sourceCell.CellFormat.BottomBorderStyle; targetCell.CellFormat.TopBorderStyle = sourceCell.CellFormat.TopBorderStyle; targetCell.CellFormat.LeftBorderStyle = sourceCell.CellFormat.LeftBorderStyle; targetCell.CellFormat.RightBorderStyle = sourceCell.CellFormat.RightBorderStyle; }
この処理により、見た目を含めた「コピー&ペースト」が実現されます。
CellFormat で指定できるプロパティについては以下をご参照ください。
🧪 実際のサンプルコード
以下のサンプルでは、Excel ファイルを開き、セル A1・B1 の値とフォーマットをそれぞれ A2・B2 に「コピー&ペースト」しています。
private void CopyCellButton_Click(object sender, RoutedEventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog { Filter = "Excel Files (*.xls;*.xlsx)|*.xls;*.xlsx" }; if (openFileDialog.ShowDialog() == true) { string filePath = openFileDialog.FileName; // Excelファイル読み込み Workbook workbook = Workbook.Load(filePath); Worksheet worksheet = workbook.Worksheets[0]; // A1 → A2 var cellA1 = worksheet.Rows[0].Cells[0]; var cellA2 = worksheet.Rows[1].Cells[0]; cellA2.Value = cellA1.Value; // B1 → B2(値 + フォーマット) var cellB1 = worksheet.Rows[0].Cells[1]; var cellB2 = worksheet.Rows[1].Cells[1]; cellB2.Value = cellB1.Value; CopyCellFormat(cellB1, cellB2); string savePath = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "CopiedCellsSample.xlsx" ); workbook.Save(savePath); MessageBox.Show($"コピー完了!\n保存場所: {savePath}"); } }
📘 まとめ
- Infragistics の Excel ライブラリには「ペースト処理」はありません。
- コピーした値や書式を「代入」することがコピー&ペーストそのものです。
- 範囲コピーや一括ペーストの API もないため、1セルずつ処理する必要があります。
以下のサンプルでは、Excel ファイルを開き、セル A1・B1 の値とフォーマットをそれぞれ A2・B2 に「コピー&ペースト」してデスクトップにファイルを出力します。同梱された Excel ファイルもご活用ください。