创建、操作或移除工作表中的场景

Contents
[ ]

Aspose.Cells提供了一些有用的类,例如 ScenarioCollectionScenarioScenarioInputCellCollectionScenarioInputCell。它还提供了 Worksheet.Scenarios 属性。下面的示例代码打开一个包含一些场景的XLSX Excel文件并从工作表中移除一个现有的场景。然后在保存Excel文件之前添加一个新的场景。它使用了一个非常简单的包含场景的模板文件。

执行代码后,现有场景将被移除,并向工作表添加新的场景。

输出文件

todo:image_alt_text

// 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");