Copier les plages d'Excel
Contents
[
Hide
]
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 les plages à l’aide de Aspose.Cells
Aspose.Cells fournit une certaine surchargeRange.Copy méthodes pour copier la plage. EtRange.CopyStyle uniquement le style de copie de la plage ;Range.CopyData uniquement la valeur de copie de la plage
Plage de copie
Création de deux plages : la plage source, la plage cible, puis copie de la plage source dans la plage cible avec la méthode Range.Copy.
Voir le code suivant :
This file contains hidden or 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); |
Coller la plage avec des options
Aspose.Cells prend en charge le collage de la plage avec un type spécifique.
This file contains hidden or 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); |
Copiez uniquement les données de la plage.
Vous pouvez également copier les données avec la méthode Range.CopyData avec les codes suivants :
This file contains hidden or 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); |