Créer, manipuler ou supprimer des scénarios des feuilles de calcul
Aspose.Cells fournit quelques classes utiles, par exemple ScenarioCollection, Scenario, ScenarioInputCellCollection et ScenarioInputCell. Elle fournit également la propriété Worksheet.Scenarios. Le code d’exemple ci-dessous ouvre un fichier Excel XLSX (qui contient quelques scénarios) et supprime un scénario existant de la feuille de calcul. Il ajoute également un nouveau scénario avant d’enregistrer le fichier Excel. Il utilise un fichier de modèle très simple contenant un scénario.
Après l’exécution du code, un scénario existant est supprimé et un nouveau scénario est ajouté à la feuille de calcul.
Le fichier de sortie
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(CreateScenariosfromWorksheets.class); | |
// Instantiate the Workbook | |
// Load an Excel file | |
Workbook workbook = new Workbook(dataDir + "Bk_scenarios.xlsx"); | |
// Access first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Remove the existing first scenario from the sheet | |
worksheet.getScenarios().removeAt(0); | |
// Create a scenario | |
int i = worksheet.getScenarios().add("MyScenario"); | |
// Get the scenario | |
Scenario scenario = worksheet.getScenarios().get(i); | |
// Add comment to it | |
scenario.setComment("Test sceanrio is created."); | |
// Get the input cells for the scenario | |
ScenarioInputCellCollection sic = scenario.getInputCells(); | |
// Add the scenario on B4 (as changing cell) with default value | |
sic.add(3, 1, "1100000"); | |
// Save the Excel file. | |
workbook.save(dataDir + "outBk_scenarios1.xlsx"); |