SpreadsheetGear 2023
WPF
SpreadsheetGear 2023 > Tutorials > WPF

Follow these steps to use SpreadsheetGear 2023 in a WPF Project

Create a new WPF Application Project

  1. Launch Visual Studio 2010, Visual Studio 2012, Visual Studio 2013, Visual Studio 2015 or Visual Studio 2017.
  2. On the File menu, point to New and click Project.  The New Project dialog box appears.
  3. Under Installed Templates, choose Visual C#.
  4. Set the .NET Framework version to .NET Framework 4.0. or higher.
  5. In the list of templates, choose WPF Application (WPF App in Visual Studio 2017).
  6. Change the Name and Location as desired.
  7. Click OK to create the project.

Create a Window with a Workbook and a Button

  1. Open the SpreadsheetGear 2023 category in the Visual Studio Toolbox.
  2. Click and drag WorkbookView onto MainWindow.
  3. In Visual Studio 2012 and later you will need to give the new WorkbookView instance the name 'workbookView1' by selecting the WorkbookView, pressing F4 for Properties, and changing the Name property from <No Name> to 'workbookView1'.
  4. Open the Common WPF Controls category in the Toolbox.
  5. Click and drag a Button onto MainWindow.
  6. Adjust the size of MainWindow and the controls as desired.

Add Code to Calculate a Value and Display it in a Cell

  1. Double click the Button to display the Button's Click event handler.
  2. Copy the following code to the Button's Click event handler.

    Simple WPF Sample

    Copy Code
    // Interrupt background calculation if necessary and acquire a lock on the workbook set.
    workbookView1.GetLock();
    try
    {
        // Get a reference to the active cell.
        SpreadsheetGear.IRange activeCell = workbookView1.ActiveCell;
        // Set a formula
        activeCell.Formula = "=24901.55 / PI()";
        // Place the result of the formula in cell B2.
        SpreadsheetGear.IRange b2 = workbookView1.ActiveWorksheet.Cells["B2"];
        b2.Value = "The diameter of the earth is " + activeCell.Text + " miles.";
                    
        // Set the font of B2 to italic.
        b2.Font.Italic = true;
    }
    finally
    {
        // Release the lock on the workbook set.
        workbookView1.ReleaseLock();
    }
    

Build and Run the Application

  1. On the Build menu, click Build Solution.  The solution builds with no errors.
  2. On the Debug menu, click Start Without Debugging.  The application runs in a window and when you click the Button, cell B2 will display "The diameter of the earth is 7926.409546 miles."

Explore SpreadsheetGear 2023

  1. Close the application if you have not already done so.
  2. Select the MainWindow.xaml (Design) tab in Visual Studio.
  3. Right click on the WorkbookView control on MainWindow and click Workbook Designer.
  4. Enter values and formulas in cells in the Workbook Designer.
  5. On the Format menu, click Cells... to bring up the Range Explorer which is a modeless dialog used to format cells. Notice that changes made in the Range Explorer are displayed immediately.
  6. Right click on the workbook in the Workbook Designer and click Workbook Explorer. Use the Workbook Explorer to add, remove and save workbooks as well as add, delete, rename, copy and move worksheets. Right click on each element in the tree on the left to see a menu of available actions.
  7. Close the Workbook Designer and notice that any changes you made in the Workbook Designer are reflected in the WorkbookView control in Visual Studio.
  8. To enable the Workbook Designer to work with the WPF WorkbookView control at runtime, you must add references to the "SpreadsheetGear 2023 for Windows Forms" and "SpreadsheetGear 2023 Windows Forms and WPF Integration" assemblies to your WPF application.
  9. From Visual Studio, Press F5 to launch the application. Notice that the changes made in the Workbook Designer at design time are saved with the form and loaded at run time.
  10. Right click on the WorkbookView and click Workbook Designer. The Workbook Designer, Workbook Explorer and Range Explorer are available at runtime to applications which are powered by SpreadsheetGear.