The 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).
To set the calculation mode, follow these steps:
1. Add a reference to C1.Silverlight.Excel.dlland create a C1XLBook.
2. Add a simple formula to perform a calculation.
3. Save the workbook. The code looks like the following. In this example it is placed within a button1_Click event so the Save As dialog box will open when the user clicks the button.
•C#
private void button1_Click_1(object sender, RoutedEventArgs e)
{
// create a new workbook to be saved
SaveBook(book =>
{
XLSheet sheet = book.Sheets[0];
// 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;
});
}
// save the file
private void SaveBook(Action<C1XLBook> action)
{
var dlg = new SaveFileDialog();
dlg.Filter = "Excel Files (*.xlsx)|*.xlsx";
if (dlg.ShowDialog() == true)
{
try
{
var book = new C1XLBook();
if (action != null)
{
action(book);
}
using (var stream = dlg.OpenFile())
{
book.Save(stream);
}
}
catch (Exception x)
{
MessageBox.Show(x.Message);
}
}
}
4. 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.