Copia gli intervalli di Excel
Contents
[
Hide
]
introduzione
In Excel, puoi selezionare un intervallo, copiare l’intervallo, quindi incollarlo con opzioni specifiche nello stesso foglio di lavoro, in altri fogli di lavoro o in altri file.
Copia gli intervalli utilizzando Aspose.Cells
Aspose.Cells fornisce un certo sovraccaricoIntervallo.Copia metodi per copiare l’intervallo. ERange.CopyStyle solo lo stile di copia della gamma;Intervallo.CopiaDati solo il valore di copia dell’intervallo
Intervallo di copia
Creazione di due intervalli: l’intervallo di origine, l’intervallo di destinazione, quindi la copia dell’intervallo di origine nell’intervallo di destinazione con il metodo Range.Copy.
Vedere il seguente codice:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
Incolla intervallo con opzioni
Aspose.Cells supporta l’incollaggio dell’intervallo con un tipo specifico.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
Copia solo i dati dell’intervallo.
Inoltre puoi copiare i dati con il metodo Range.CopyData come i seguenti codici:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |