从工作表中创建、操作或删除场景
Contents
[
Hide
]
有时,您需要在电子表格中创建、操作或删除方案。场景是一个命名的假设模型,其中包括通过一个或多个公式链接在一起的变量输入单元格。在创建方案之前,设计一个工作表,使其至少包含一个公式,该公式取决于可以插入不同值的单元格。以下示例显示如何使用 Aspose.Cells API 在工作表中创建和删除方案。
Aspose.Cells提供了一些有用的类,例如场景集, 设想, ScenarioInputCellCollection和场景输入单元格.它还提供了工作表.场景财产。下面的示例代码打开一个 XLSX Excel 文件(包含一些场景)并从工作表中删除现有场景。它还在保存 Excel 文件之前添加了一个新场景。它使用一个包含场景的非常简单的模板文件。
执行代码后,现有方案将被删除,新方案将添加到工作表中。
输出文件
This file contains 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
// 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"); |