The C1XLBook.CalculationMode property specifies the calculation mode for all formulas in the workbook. The CalculationMode enumeration provides three options: Manual (you manually perform the calculation), Auto (the calculation is automatically performed), or AutoNoTable (the calculation is performed except on tables).
In XAML View
Markup |
Copy Code
|
---|---|
<Button x:Name="HelloButton" Content="Click Hello" /> |
Markup |
Copy Code
|
---|---|
<Button x:Name="SaveButton" Content="Save" /> |
Markup |
Copy Code
|
---|---|
<TextBox Name="_tbContent" Text="Empty" IsReadOnly="True" AcceptsReturn="True" FontFamily="Courier New" Background="White" Margin="465,10,242,722" /> |
In Code View
To set the calculation mode, follow these steps:
C# |
Copy Code
|
---|---|
using C1.Xaml.Excel;
|
C# |
Copy Code
|
---|---|
public sealed partial class MainPage : Page { C1XLBook _book; } |
C# |
Copy Code
|
---|---|
_book = new C1XLBook();
|
C# |
Copy Code
|
---|---|
void RefreshView()
{
}
|
C# |
Copy Code
|
---|---|
private void HelloButton_Click(object sender, RoutedEventArgs e) { // step 1: create a new workbook _book = new C1XLBook(); // step 2: get the default sheet and give it a name XLSheet sheet = _book.Sheets[0]; // step 3: add a simple formula sheet[7, 0].Value = "Formula: 5!"; sheet[7, 1].Value = 122; sheet[7, 1].Formula = "1*2*3*4*5"; _book.CalculationMode = CalculationMode.Auto; // step 4: allow user to save the file _tbContent.Text = "'Hello World' workbook has been created, you can save it now."; RefreshView(); }[ES5] |
C# |
Copy Code
|
---|---|
async void SaveButton_Click(object sender, RoutedEventArgs e) { Debug.Assert(_book != null); var picker = new Windows.Storage.Pickers.FileSavePicker(); picker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary; picker.FileTypeChoices.Add("Open XML Excel file", new List<string>() { ".xlsx" }); picker.FileTypeChoices.Add("BIFF Excel file", new List<string>() { ".xls" }); picker.SuggestedFileName = "New Book"; var file = await picker.PickSaveFileAsync(); if (file != null) { try { // step 1: save file var fileFormat = Path.GetExtension(file.Path).Equals(".xls") ? FileFormat.Biff8 : FileFormat.OpenXml; await _book.SaveAsync(file, fileFormat); // step 2: user feedback _tbContent.Text = string.Format("File has been saved to: {0}.", file.Path); RefreshView(); } catch (Exception x) { _tbContent.Text = string.Format("EXCEPTION: {0}", x.Message); } } } |
Run the project and save and open the Excel file. Notice that the value for the cell in (7,1) is 120, or the total of 1*2*3*4*5, not 122, since we set the CalculationMode to Auto.