如何複製儲存格
「複製儲存格」功能允許您複製儲存格的內容,並將它們貼上到一個或多個其他儲存格中。 這是一種方便的方法來複製工作表中的數據、公式、格式或其他屬性。
此外,Copy
方法還保留了樣式,能夠使用 IronXL 在單個或多個工作表中進行高效且精確的數據複製。
如何複製儲存格內容
- Download the C# library for copying cells
- Load the existing Excel spreadsheet
- 選擇您要複製的範圍、行或列
- 在選定範圍上調用
Copy
方法 - 將目的地工作表及位置傳遞給
Copy
方法
開始使用IronXL
立即在您的專案中使用IronXL,並享受免費試用。
複製單一儲存格範例
要複製選定儲存格的內容,您可以使用Copy
方法。 將工作表對象作為第一個參數傳遞,起始位置作為第二個參數傳遞。 Copy 方法也保留了儲存格包含的任何樣式。
:path=/static-assets/excel/content-code-examples/how-to/copy-cells-copy-single-cell.cs
using IronXL;
WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.GetWorkSheet("Sheet1");
// Copy cell content
workSheet["A1"].Copy(workBook.GetWorkSheet("Sheet1"), "B3");
workBook.SaveAs("copySingleCell.xlsx");
Imports IronXL
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
Private workSheet As WorkSheet = workBook.GetWorkSheet("Sheet1")
' Copy cell content
workSheet("A1").Copy(workBook.GetWorkSheet("Sheet1"), "B3")
workBook.SaveAs("copySingleCell.xlsx")
輸出試算表

複製單元格範圍範例
與 Clear 方法類似,此方法也可用於 Range 類別,讓您可以在任何範圍執行,不論其大小。以下是一些例子:
複製單個儲存格 (C10):
-
workSheet["C10"].Copy(workBook.GetWorkSheet("Sheet1"), "B13")
複製一列(A):
-
workSheet.GetColumn(0).Copy(workBook.GetWorkSheet("Sheet1"), "H1")
複製一行(4):
-
workSheet.GetRow(3).Copy(workBook.GetWorkSheet("Sheet1"), "A15")
複製二維範圍 (D6:F8):
-
workSheet ["D6:F8"].Copy(workBook.GetWorkSheet("Sheet1"), "H17")
第二個參數接受一個地址位置,標記數據輸入的起始點。 從該地址開始,複製的數據將向右和向下展開。
:path=/static-assets/excel/content-code-examples/how-to/copy-cells-copy-cell-range.cs
using IronXL;
WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.GetWorkSheet("Sheet1");
// Copy a single cell(C10)
workSheet["C10"].Copy(workBook.GetWorkSheet("Sheet1"), "B13");
// Copy a column(A)
workSheet.GetColumn(0).Copy(workBook.GetWorkSheet("Sheet1"), "H1");
// Copy a row(4)
workSheet.GetRow(3).Copy(workBook.GetWorkSheet("Sheet1"), "A15");
// Copy a two-dimensional range(D6:F8)
workSheet["D6:F8"].Copy(workBook.GetWorkSheet("Sheet1"), "H17");
workBook.SaveAs("copyCellRange.xlsx");
Imports IronXL
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
Private workSheet As WorkSheet = workBook.GetWorkSheet("Sheet1")
' Copy a single cell(C10)
workSheet("C10").Copy(workBook.GetWorkSheet("Sheet1"), "B13")
' Copy a column(A)
workSheet.GetColumn(0).Copy(workBook.GetWorkSheet("Sheet1"), "H1")
' Copy a row(4)
workSheet.GetRow(3).Copy(workBook.GetWorkSheet("Sheet1"), "A15")
' Copy a two-dimensional range(D6:F8)
workSheet("D6:F8").Copy(workBook.GetWorkSheet("Sheet1"), "H17")
workBook.SaveAs("copyCellRange.xlsx")
輸出試算表

跨工作表複製儲存格範例
由於第一個參數接受工作表對象,因此可以在不同的工作表之間複製和貼上單元格範圍。 只需將不同的工作表物件傳遞作為第一個參數即可。
請注意
:path=/static-assets/excel/content-code-examples/how-to/copy-cells-copy-to-other-worksheet.cs
using IronXL;
WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.GetWorkSheet("Sheet1");
// Copy cell content
workSheet["A1"].Copy(workBook.GetWorkSheet("Sheet2"), "B3");
workBook.SaveAs("copyAcrossWorksheet.xlsx");
Imports IronXL
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
Private workSheet As WorkSheet = workBook.GetWorkSheet("Sheet1")
' Copy cell content
workSheet("A1").Copy(workBook.GetWorkSheet("Sheet2"), "B3")
workBook.SaveAs("copyAcrossWorksheet.xlsx")