创建、操作或移除工作表中的场景
Contents
[
Hide
]
有时,您需要在电子表格中创建、操作或删除方案。方案是一个命名的“假设”模型,其中包含由一个或多个公式链接的可变输入单元格。在创建方案之前,设计工作表,使其至少包含一个依赖于可以插入不同值的单元格的公式。以下示例演示了如何通过Aspose.Cells API在工作簿中的工作表中创建和删除方案。
Aspose.Cells提供一些有用的类,例如ScenarioCollection,Scenario,ScenarioInputCellCollection和ScenarioInputCell类。它还提供Worksheet.Scenarios属性。下面的示例代码打开一个包含一些方案的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-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Instantiate the Workbook | |
// Load an Excel file | |
Workbook workbook = new Workbook(dataDir+ "aspose-sample.xlsx"); | |
// Access first worksheet | |
Worksheet worksheet = workbook.Worksheets[0]; | |
if (worksheet.Scenarios.Count > 0) | |
{ | |
// Remove the existing first scenario from the sheet | |
worksheet.Scenarios.RemoveAt(0); | |
// Create a scenario | |
int i = worksheet.Scenarios.Add("MyScenario"); | |
// Get the scenario | |
Scenario scenario = worksheet.Scenarios[i]; | |
// Add comment to it | |
scenario.Comment = "Test sceanrio is created."; | |
// Get the input cells for the scenario | |
ScenarioInputCellCollection sic = scenario.InputCells; | |
// Add the scenario on B4 (as changing cell) with default value | |
sic.Add(3, 1, "1100000"); | |
dataDir = dataDir + "outBk_scenarios1.out.xlsx"; | |
// Save the Excel file. | |
workbook.Save(dataDir); | |
Console.WriteLine("\nProcess completed successfully.\nFile saved at " + dataDir); | |
} |