Copier des plages d Excel
Introduction
Dans Excel, vous pouvez sélectionner une plage, copier la plage, puis la coller avec des options spécifiques dans la même feuille de calcul, d’autres feuilles de calcul ou d’autres fichiers.
Copier des plages en utilisant Aspose.Cells
Aspose.Cells propose quelques méthodes de surcharge Range.Copy pour copier la plage.
Copier la plage
Création de deux plages : la plage source, la plage cible, puis copie de la plage source vers la plage cible avec la méthode Range.Copy.
Voir le code suivant :
// 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); |
Coller la plage avec des options
Aspose.Cells prend en charge le collage de la plage avec un type spécifique.
// 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); |
Copier uniquement les données de la plage.
Vous pouvez également copier les données avec la méthode Range.CopyData comme dans les codes suivants :
// 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); |