// Create a new workbook Workbook workbook = new Workbook(); InputStream resourceStream = this.getResourceStream("xlsx/What-If-Analysis-Scenarios.xlsx"); workbook.open(resourceStream); IWorksheet worksheet = workbook.getActiveSheet(); // Add different scenarios which represent the different discount rates. // Create a scenario with less discount rates. // The changing cells are D2:D6. The values of the changing cells are [0.05, 0.02, 0.03, 0.02, 0.05]. // The comment of the scenario is "Created by Document Solutions for Excel". List lessDiscountRatesValues = Arrays.asList(0.05, 0.02, 0.03, 0.02, 0.05); IScenario lessDiscountRates = worksheet.getScenarios().add("Less Discount Rates", worksheet.getRange("D2:D6"), lessDiscountRatesValues, "Created by Document Solutions for Excel"); // Create a scenario with normal discount rates. // The changing cells are D2:D6. The values of the changing cells are [0.1, 0.05, 0.05, 0.05, 0.1]. List normalDiscountRatesValues = Arrays.asList(0.1, 0.05, 0.05, 0.05, 0.1); IScenario normalDiscountRates = worksheet.getScenarios().add("Normal Discount Rates", worksheet.getRange("D2:D6"), normalDiscountRatesValues); // Create a scenario with selling without discount. // The changing cells are D2:D6. The values of the changing cells are [0, 0, 0, 0, 0]. List sellingWithoutDiscountValues = Arrays.asList(0, 0, 0, 0, 0); IScenario sellingWithoutDiscount = worksheet.getScenarios().add("Selling Without Discount", worksheet.getRange("D2:D6"), sellingWithoutDiscountValues); // Create a scenario with bulk quantity sold. // The changing cells are E2:E6. The values of the changing cells are [1000, 1000, 1000, 1000, 1000]. List bulkQuantitySoldValues = Arrays.asList(1000, 1000, 1000, 1000, 1000); IScenario bulkQuantitySold = worksheet.getScenarios().add("Bulk Quantity Sold", worksheet.getRange("E2:E6"), bulkQuantitySoldValues); // Modify the values of the changing cells of the "Less Discount Rates" scenario. lessDiscountRatesValues.set(1, 0.04); lessDiscountRates.changeScenario(worksheet.getRange("D2:D6"), lessDiscountRatesValues); // After showing the "Less Discount Rates" scenario, the D4:D6 will be assigned the values [0.05, 0.04, 0.03, 0.02, 0.05]. // The formulas(F2:K6, N6) associated with D2:D6 is recalculated and the charts are updated. worksheet.getScenarios().get("Less Discount Rates").show(); // Save to an excel file workbook.save("CreateScenarios.xlsx");