管理Excel文件的公式
介绍
Microsoft Excel的一个引人注目的功能之一是其处理具有公式和函数的数据的能力。Microsoft Excel提供了一组内置函数和公式,帮助用户快速执行复杂的计算。Aspose.Cells还提供了大量内置函数和公式,帮助开发人员轻松计算值。Aspose.Cells还支持插件函数。此外,Aspose.Cells支持在Aspose.Cells中的数组和R1C1公式。
如何使用公式和函数
Aspose.Cells提供了一个表示Microsoft Excel文件的类Workbook。Workbook类包含一个Worksheets集合,允许访问Excel文件中的每个工作表。工作表由Worksheet类表示。Worksheet类提供了一个Cells集合。Cells集合中的每个项都代表了Cell类的对象。
可以使用Cell类提供的属性和方法将公式应用到单元格中,下面将更详细地讨论。
- 使用内置函数。
- 使用插件函数。
- 使用数组公式。
- 创建R1C1公式。
如何使用内置函数
内置函数或公式作为现成的函数提供,以减轻开发人员的工作和时间。请参阅Aspose.Cells支持的内置函数列表。函数按字母顺序列出。将来将支持更多函数。
Aspose.Cells支持大多数Microsoft Excel提供的公式或函数。开发人员可以通过API或设计人员已设计的电子表格使用这些公式。Aspose.Cells支持一大堆数学、字符串、布尔、日期/时间、统计、数据库、查找和参考公式。
使用Cell类的Formula属性向单元格添加公式。复杂的公式,例如
= H7*(1+IF(P7 = $L$3,$M$3, (IF(P7=$L$4,$M$4,0))))
Aspose.Cells也支持定义的函数。在将公式应用于单元格时,始终要以等号(=)开头,就像在Microsoft Excel中创建公式时一样,并使用逗号(,)来分隔函数参数。
在下面的示例中,复杂的公式应用于工作表的第一个单元格的Cells集合。该公式使用Aspose.Cells提供的内置IF函数。
// 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 directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Adding a new worksheet to the Excel object | |
int sheetIndex = workbook.Worksheets.Add(); | |
// Obtaining the reference of the newly added worksheet by passing its sheet index | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Adding a value to "A1" cell | |
worksheet.Cells["A1"].PutValue(1); | |
// Adding a value to "A2" cell | |
worksheet.Cells["A2"].PutValue(2); | |
// Adding a value to "A3" cell | |
worksheet.Cells["A3"].PutValue(3); | |
// Adding a SUM formula to "A4" cell | |
worksheet.Cells["A4"].Formula = "=SUM(A1:A3)"; | |
// Calculating the results of formulas | |
workbook.CalculateFormula(); | |
// Get the calculated value of the cell | |
string value = worksheet.Cells["A4"].Value.ToString(); | |
// Saving the Excel file | |
workbook.Save(dataDir + "output.xls"); |
如何使用Add-in函数
我们可以有一些自定义的公式,我们希望将其包含为Excel加载项。当设置cell.Formula函数内置函数正常工作时,但需要使用加载项函数设置自定义函数或公式。
Aspose.Cells提供功能来使用Worksheets.RegisterAddInFunction()注册加载项函数。之后,当我们设置cell.Formula = anyFunctionFromAddIn时,输出的Excel文件包含从AddIn函数计算出的值。
可以下载以下XLAM文件以注册以下示例代码中的加载项函数。类似地,可以下载输出文件"test_udf.xlsx"以检查输出。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Create empty workbook | |
Workbook workbook = new Workbook(); | |
// Register macro enabled add-in along with the function name | |
int id = workbook.Worksheets.RegisterAddInFunction(sourceDir + @"TESTUDF.xlam", "TEST_UDF", false); | |
// Register more functions in the file (if any) | |
workbook.Worksheets.RegisterAddInFunction(id, "TEST_UDF1"); //in this way you can add more functions that are in the same file | |
// Access first worksheet | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Access first cell | |
var cell = worksheet.Cells["A1"]; | |
// Set formula name present in the add-in | |
cell.Formula = "=TEST_UDF()"; | |
// Save workbook to output XLSX format. | |
workbook.Save(outputDir + @"test_udf.xlsx", Aspose.Cells.SaveFormat.Xlsx); |
如何使用数组公式
数组公式是以数组作为参数的函数所组成的公式。在显示数组公式时,会用大括号({})括起来。
某些Microsoft Excel函数返回值数组。要使用数组公式计算多个结果,请将数组输入到与数组参数具有相同行数和列数的单元格范围中。
可以通过调用Cell类的SetArrayFormula方法将数组公式应用于单元格。SetArrayFormula方法接受以下参数:
- 数组公式,数组公式。
- 行数,要填充数组公式结果的行数。
- 列数,要填充数组公式结果的列数。
// 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 directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Adding a new worksheet to the Excel object | |
int sheetIndex = workbook.Worksheets.Add(); | |
// Obtaining the reference of the newly added worksheet by passing its sheet index | |
Worksheet worksheet = workbook.Worksheets[sheetIndex]; | |
// Adding a value to "A1" cell | |
worksheet.Cells["A1"].PutValue(1); | |
// Adding a value to "A2" cell | |
worksheet.Cells["A2"].PutValue(2); | |
// Adding a value to "A3" cell | |
worksheet.Cells["A3"].PutValue(3); | |
// Adding a value to B1 | |
worksheet.Cells["B1"].PutValue(4); | |
// Adding a value to "B2" cell | |
worksheet.Cells["B2"].PutValue(5); | |
// Adding a value to "B3" cell | |
worksheet.Cells["B3"].PutValue(6); | |
// Adding a value to C1 | |
worksheet.Cells["C1"].PutValue(7); | |
// Adding a value to "C2" cell | |
worksheet.Cells["C2"].PutValue(8); | |
// Adding a value to "C3" cell | |
worksheet.Cells["C3"].PutValue(9); | |
// Adding a SUM formula to "A4" cell | |
worksheet.Cells["A6"].SetArrayFormula("=LINEST(A1:A3,B1:C3,TRUE,TRUE)", 5, 3); | |
// Calculating the results of formulas | |
workbook.CalculateFormula(); | |
// Get the calculated value of the cell | |
string value = worksheet.Cells["A6"].Value.ToString(); | |
// Saving the Excel file | |
workbook.Save(dataDir + "output.xls"); |
如何使用R1C1公式
使用Cell类的R1C1Formula属性向单元格添加R1C1引用样式的公式。
// 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); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "Book1.xls"); | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Setting an R1C1 formula on the "A11" cell, | |
// Row and Column indeces are relative to destination index | |
worksheet.Cells["A11"].R1C1Formula = "=SUM(R[-10]C[0]:R[-7]C[0])"; | |
// Saving the Excel file | |
workbook.Save(dataDir + "output.xls"); |