Version

Creating a Workbook (Infragistics Excel Engine)

Before You Begin

The Infragistics Excel Engine enables you to save data to and load data from Microsoft® Excel®. You can create workbooks and worksheets, input data, and export the data to Excel using the library’s various classes. The Infragistics Excel Engine makes it easy to export the data in your application as an Excel spreadsheet as well as import data from Excel into your application.

What You Will Accomplish

You will create a simple workbook and save it as an Excel file.

Follow These Steps

  1. Create a Microsoft Windows® Presentation Foundation project.

  1. Add a reference to the following NuGet package:

    • Infragistics.WPF.Excel

    For more information on setting up the NuGet feed and adding NuGet packages, you can take a look at the following documentation: NuGet Feeds.

  1. Add a Button control and…​

    1. set the Content property to "Create a Workbook".

    2. attach an event handler to the Button control’s Click event.

    In XAML:

    <Button Content="Create a Workbook" Click="Button_Click" />
  1. Open the code-behind and add the following namespaces:

    In Visual Basic:

    Imports Infragistics.Documents.Excel

    In C#:

    using Infragistics.Documents.Excel;
  1. Add an event handler for the Button control’s Click event if a method stub has not been created for you.

    In Visual Basic:

    Sub Button_Click(sender As Object, e As RoutedEventArgs)
        'TODO: Instantiate a Workbook object
        'TODO: Add a worksheet to the workbook
        'TODO: Add text to the first cell
        'TODO: Save the workbook.
    End Sub

    In C#:

    void Button_Click(object sender, RoutedEventArgs e)
    {
        //TODO: Instantiate a Workbook object.
        //TODO: Add a worksheet to the workbook
        //TODO: Add text to the first cell
        //TODO: Save the workbook.
    }
  1. Instantiate a Workbook object.

    In Visual Basic:

    Dim workbook1 As New Workbook()

    In C#:

    Workbook workbook1 = new Workbook();
  1. Add a Worksheet object to the workbook.

    In Visual Basic:

    Dim worksheet1 As Worksheet = workbook1.Worksheets.Add("Sheet 1")

    In C#:

    Worksheet worksheet1 = workbook1.Worksheets.Add("Sheet 1");
  1. Set the first WorksheetCell object’s Value property to 42.

    In Visual Basic:

    worksheet1.Rows(0).Cells(0).Value = 42

    In C#:

    worksheet1.Rows[0].Cells[0].Value = 42;
  1. Save the workbook.

    In Visual Basic:

    workbook1.Save("Workbook1.xls")

    In C#:

    workbook1.Save("Workbook1.xls");
  1. Run the project and click the button.