Bereiche von Excel kopieren

Einführung

In Excel können Sie einen Bereich auswählen, den Bereich kopieren und ihn mit spezifischen Optionen in dasselbe Arbeitsblatt, in andere Arbeitsblätter oder in andere Dateien einfügen.

Bereiche mit Aspose.Cells kopieren

Aspose.Cells bietet einige Überladungsmethoden für Range.Copy an, um den Bereich zu kopieren. Und Range.CopyStyle kopiert nur den Stil des Bereichs; Range.CopyData kopiert nur den Wert des Bereichs.

Bereich kopieren

Erstellen von zwei Bereichen: Der Quellbereich, der Zielbereich, dann Kopieren des Quellbereichs in den Zielbereich mit der Range.Copy-Methode.

Siehe den folgenden Code:

// 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);
view raw Copy-Range.cs hosted with ❤ by GitHub

Bereich mit Optionen einfügen

Aspose.Cells unterstützt das Einfügen des Bereichs mit einem spezifischen Typ.

// 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);
view raw Paste-Range.cs hosted with ❤ by GitHub

Nur Daten des Bereichs kopieren

Sie können auch die Daten mit der Range.CopyData-Methode wie folgt kopieren:

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

Erweiterte Themen