Beräkna formler

Lägga till formler och beräkna resultat

Aspose.Cells har en inbäddad formelberäkningsmotor. Det kan inte bara räkna om formler som importerats från designermallar utan det stöder också att beräkna resultaten av formler som läggs till vid körning.

Aspose.Cells stöder de flesta formler eller funktioner som ingår i Microsoft Excel(Läsen lista över de funktioner som stöds av beräkningsmotorn). Dessa funktioner kan användas via API:er eller designerkalkylblad. Aspose.Cells stöder en stor uppsättning matematiska formler, sträng-, booleska-, datum/tid-, statistiska, databas-, uppslags- och referensformler.

AnvändFormel egendom ellerSetFormula(…) metoder förCellklass för att lägga till en formel i en cell. När du använder en formel, börja alltid strängen med ett likhetstecken (=) som du gör när du skapar en formel i Microsoft Excel och använd ett kommatecken (,) för att avgränsa funktionsparametrar.

För att beräkna resultaten av formler kan användaren anropaBeräkna Formel metod förArbetsbokklass som bearbetar alla formler inbäddade i en Excel-fil. Eller så kan användaren ringa tillBeräkna Formel metod förWorsheet klass som bearbetar alla formler inbäddade i ett ark. Eller så kan användaren också ringaBeräkna metod förCellklass som behandlar formeln för en Cell:

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

Viktigt att veta

Direkt beräkning av formel

Aspose.Cells har en inbäddad formelberäkningsmotor. Förutom att beräkna formler som importeras från en designerfil kan Aspose.Cells beräkna formelresultat direkt.

Ibland måste du beräkna formelresultat direkt utan att lägga till dem i ett kalkylblad. Värdena för cellerna som används i formeln finns redan i ett kalkylblad och allt du behöver är att hitta resultatet av dessa värden baserat på någon Microsoft Excel-formel utan att lägga till formeln i ett kalkylblad.

Du kan använda Aspose.Cells' API:er för formelberäkningsmotorer förArbetsblad tillBeräkna resultaten av sådana formler utan att lägga till dem i kalkylbladet:

// 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);
// Create a workbook
Workbook workbook = new Workbook();
// Access first worksheet
Worksheet worksheet = workbook.Worksheets[0];
// Put 20 in cell A1
Cell cellA1 = worksheet.Cells["A1"];
cellA1.PutValue(20);
// Put 30 in cell A2
Cell cellA2 = worksheet.Cells["A2"];
cellA2.PutValue(30);
// Calculate the Sum of A1 and A2
var results = worksheet.CalculateFormula("=Sum(A1:A2)");
// Print the output
System.Console.WriteLine("Value of A1: " + cellA1.StringValue);
System.Console.WriteLine("Value of A2: " + cellA2.StringValue);
System.Console.WriteLine("Result of Sum(A1:A2): " + results.ToString());

Ovanstående kod ger följande utdata:

Value of A1: 20
Value of A2: 30
Result of Sum(A1:A2): 50.0

Beräknar formler upprepade gånger

När det finns många formler i arbetsboken och användaren behöver beräkna dem upprepade gånger med endast en liten del av dem, kan det vara till hjälp för prestanda att aktivera formelberäkningskedjan:FormulaSettings.EnableCalculationChain.

// 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);
// Load the template workbook
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Print the time before formula calculation
Console.WriteLine(DateTime.Now);
// Set the CreateCalcChain as tue
workbook.Settings.FormulaSettings.EnableCalculationChain = true;
// Calculate the workbook formulas
workbook.CalculateFormula();
// Print the time after formula calculation
Console.WriteLine(DateTime.Now);
//change the value of one cell
workbook.Worksheets[0].Cells["A1"].PutValue("newvalue");
//re-calculate those formulas which depend on cell A1
workbook.CalculateFormula();

Viktigt att veta

Förhandsämnen