我正在尝试通过 Apache POI 读取
mduration
函数,但它似乎不支持。
在探索从 Excel 中提取
mduration
公式结果的选项时,我遇到了这个 Aspose 库。
我想知道这是否可以免费使用以及如何在 Java 或 python 程序中使用它在我们的程序中提取
mduration
结果。
如果有详细阐述相同内容的代码片段,我们将不胜感激。
需要有关 Aspose cells API 以及如何在 Java 和 Python 程序中使用它的详细信息。
一般来说,Aspose.Cells是一个商业组件,虽然Aspose提供免费试用版,但有一定的限制,允许用户在购买前探索其功能,但它不是免费提供的。您可以下载并使用适用于 Java 和 Python 库的 Aspose.Cells。
Aspose.Cells 是一个功能丰富且强大的库,专为以编程方式操作所有 Excel 文件格式和渲染功能而设计。此外,它可以执行复杂的计算,包括高级 Excel 公式,如 MDURATION(修改持续时间)函数,所有这些都不需要 Microsoft Excel 将安装在您的系统上。
我将编写两个示例(一个使用 Aspose.Cells for Java,另一个使用 Aspose.Cells for Python SDK)来演示根据您的需要在 Excel 电子表格中设置和计算 MDURATION 公式/函数。
[Java]
//Instantiating a new Workbook
Workbook workbook = new Workbook();
//Get the first worksheet (default worksheet) in the workbook
Worksheet worksheet = workbook.getWorksheets().get(0);
//Insert values into cells for MDURATION formula calculation
worksheet.getCells().get("A1").putValue("Setting date");
worksheet.getCells().get("A2").putValue("01-Mar-2018");
worksheet.getCells().get("B1").putValue("Maturity Date");
worksheet.getCells().get("B2").putValue("01-Mar-2028");
worksheet.getCells().get("C1").putValue("C Rate");
worksheet.getCells().get("C2").putValue(0.04);
worksheet.getCells().get("D1").putValue("Yield");
worksheet.getCells().get("D2").putValue(0.05);
worksheet.getCells().get("E1").putValue("freq.");
worksheet.getCells().get("E2").putValue(2);
//Set MDURATION formula in the cell
worksheet.getCells().get("F1").putValue("MDURATION function result");
worksheet.getCells().get("F2").setFormula("=MDURATION(A2, B2, C2, D2, E2)");
//Calculate the results for the formula(s)
workbook.calculateFormula();
// Get the calculated result from the cell
double mdurationResult = worksheet.getCells().get("F2").getDoubleValue();
//Printing the result
System.out.println("MDuration result: " + mdurationResult);
//Save the output Excel file to disk
workbook.save("d:\\files\\output_mduration_results1.xlsx")
[Python]
import jpype
import asposecells
# Start JVM
jpype.startJVM()
# Import Aspose.Cells for Python
from asposecells.api import Workbook
# Instantiating a new Workbook
workbook = Workbook()
# Get the first worksheet (default worksheet) in the workbook
worksheet = workbook.getWorksheets().get(0)
# Insert values into cells for MDURATION formula calculation
worksheet.getCells().get("A1").putValue("Setting date")
worksheet.getCells().get("A2").putValue("01-Mar-2018")
worksheet.getCells().get("B1").putValue("Maturity Date")
worksheet.getCells().get("B2").putValue("01-Mar-2028")
worksheet.getCells().get("C1").putValue("C Rate")
worksheet.getCells().get("C2").putValue(0.04)
worksheet.getCells().get("D1").putValue("Yield")
worksheet.getCells().get("D2").putValue(0.05)
worksheet.getCells().get("E1").putValue("freq.")
worksheet.getCells().get("E2").putValue(2)
# Set MDURATION formula in the cell
worksheet.getCells().get("F1").putValue("MDURATION function result")
worksheet.getCells().get("F2").setFormula("=MDURATION(A2, B2, C2, D2, E2)")
# Calculate the results for the formula(s)
workbook.calculateFormula()
# Get the calculated result from the cell
mdurationResult = worksheet.getCells().get("F2").getDoubleValue()
# Printing the result
print(f"MDuration result: {mdurationResult}")
# Save the output Excel file to disk
workbook.save("d:\\files\\output_mduration_results1.xlsx")
# Shutdown JVM
jpype.shutdownJVM()
希望这能有所帮助。另外,您可以在专门的论坛中发布您的疑问。
PS。我在 Aspose 担任支持开发人员/布道者。