使用ICustomFunction返回一系列值
从 Aspose.Cells for Java 20.8 发布开始,ICustomFunction 不再推荐使用,您应使用 AbstractCalculationEngine 类。AbstractCalculationEngine 类的使用方式在以下文章中描述。
Aspose.Cells提供了ICustomFunction接口,用于实现Microsoft Excel不支持的用户定义或自定义函数。
当您实现ICustomFunction接口方法时,通常需要返回单个单元格值。但有时,您需要返回一系列的值。本文将解释如何从ICustomFunction返回值的一系列值。
以下代码实现ICustomFunction,并通过其方法返回一系列值。
创建一个带有函数 CalculateCustomFunction 的类。该类实现了 ICustomFunction。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
public class CustomFunctionStaticValue : ICustomFunction | |
{ | |
public object CalculateCustomFunction(string functionName, ArrayList paramsList, ArrayList contextObjects) | |
{ | |
return new object[][] { | |
new object[]{new DateTime(2015, 6, 12, 10, 6, 30), 2}, | |
new object[]{3.0, "Test"} | |
}; | |
} | |
} |
现在将以上函数用于您的程序
// 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); | |
// Create workbook | |
Workbook wb = new Workbook(); | |
Cells cells = wb.Worksheets[0].Cells; | |
// Set formula | |
Cell cell = cells[0, 0]; | |
cell.SetArrayFormula("=MYFUNC()", 2, 2); | |
Style style = cell.GetStyle(); | |
style.Number = 14; | |
cell.SetStyle(style); | |
// Set calculation options for formula | |
CalculationOptions copt = new CalculationOptions(); | |
copt.CustomEngine = new CustomFunctionStaticValue(); | |
wb.CalculateFormula(copt); | |
// Save to xlsx by setting the calc mode to manual | |
wb.Settings.FormulaSettings.CalculationMode = CalcModeType.Manual; | |
wb.Save(dataDir + "output_out.xlsx"); | |
// Save to pdf | |
wb.Save(dataDir + "output_out.pdf"); |