Copia degli intervalli di Excel
Introduzione
In Excel, è possibile 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.
Copiare intervalli utilizzando Aspose.Cells
Aspose.Cells fornisce alcuni metodi di sovraccarico Range.Copy per copiare l’intervallo.
Copia Intervallo
Creazione di due intervalli: l’intervallo di origine, l’intervallo di destinazione, quindi copiare l’intervallo di origine nell’intervallo di destinazione con il metodo Range.Copy.
Vedere il codice seguente:
// Instantiate a new Workbook. | |
Workbook workbook = new Workbook(); | |
// Get all the worksheets in the book. | |
WorksheetCollection worksheets = workbook.getWorksheets(); | |
// Get the first worksheet in the worksheets collection. | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Create a range of cells. | |
Range sourceRange = worksheet.getCells().createRange("A1", "A2"); | |
// Input some data with some formats into a few cells in the range. | |
sourceRange.get(0, 0).putValue("Test"); | |
sourceRange.get(1, 0).putValue("123"); | |
// Create target range of cells. | |
Range targetRange = worksheet.getCells().createRange("B1", "B2"); | |
// Copy source range to target range in the same workhseet | |
targetRange.copy(sourceRange); | |
// Create target range of cells. | |
workbook.getWorksheets().add(); | |
worksheet = workbook.getWorksheets().get(1); | |
targetRange = worksheet.getCells().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.getWorksheets().get(0); | |
targetRange = worksheet.getCells().createRange("A1", "A2"); | |
// Copy source range to target range in another workbook | |
targetRange.copy(sourceRange); |
Incolla l’intervallo con opzioni
Aspose.Cells supporta l’incollaggio del intervallo con un tipo specifico.
// Instantiate a new Workbook. | |
Workbook workbook = new Workbook(); | |
// Get all the worksheets in the book. | |
WorksheetCollection worksheets = workbook.getWorksheets(); | |
// Get the first worksheet in the worksheets collection. | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Create a range of cells. | |
Range sourceRange = worksheet.getCells().createRange("A1", "A2"); | |
// Input some data with some formats into a few cells in the range. | |
sourceRange.get(0, 0).putValue("Test"); | |
sourceRange.get(1, 0).putValue("123"); | |
// Create target range of cells. | |
Range targetRange = worksheet.getCells().createRange("B1", "B2"); | |
// Init paste options. | |
PasteOptions options = new PasteOptions(); | |
// Set paste type. | |
options.setPasteType(PasteType.VALUES_AND_FORMATS); | |
options.setSkipBlanks(true); | |
// Copy source range to target range | |
targetRange.copy(sourceRange, options); |
Copia solo i dati dell’intervallo.
Puoi anche copiare i dati con il metodo Range.CopyData come nei seguenti codici:
// Instantiate a new Workbook. | |
Workbook workbook = new Workbook(); | |
// Get all the worksheets in the book. | |
WorksheetCollection worksheets = workbook.getWorksheets(); | |
// Get the first worksheet in the worksheets collection. | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Create a range of cells. | |
Range sourceRange = worksheet.getCells().createRange("A1", "A2"); | |
// Set a few cells in the range. | |
sourceRange.get(0, 0).putValue("Test"); | |
sourceRange.get(1, 0).putValue("123"); | |
// Create target range of cells. | |
Range targetRange = worksheet.getCells().createRange("B1", "B2"); | |
//Only copy data the range. | |
targetRange.CopyData(sourceRange); |