This topic describes a typical workflow for creating a template spreadsheet with ReoGridEditor and displaying it in a .NET application with the ReoGrid control.

Using an Excel file as a Template

It is also possible to create a template spreadsheet using Microsoft Excel instead of ReoGridEditor. Excel files can also be displayed by ReoGrid. For details, see the Excel file format.

Make a template spreadsheet with ReoGridEditor

When you need to use ReoGrid to display a spreadsheet in a .NET application, the following steps represent a recommended practice: 100

  1. Design a spreadsheet template using ReoGrid Editor
  2. Save as XML
  3. Add as a project resource
  4. Load in the .NET application
  5. Fill in data

1. Design a template using ReoGrid Editor

Download the release package and run ReoGridEditor.exe. 70

Using this editor, you can design a spreadsheet template with text, borders, cell styles, formulas, merged cells, and print settings. It is typically used to build a template spreadsheet for ReoGrid applications.

Here is an example of a finished template: 71

2. Save as XML

Select the menu ‘File’ -> ‘Save’ or press the ‘Save’ button on the toolbar to save this spreadsheet in ReoGrid XML format. 72

Enter a file name: 73

3. Add as a project resource

  1. Open Visual Studio, and create a new project or open your existing project.
  2. Add references to ReoGrid (see installation).
  3. Switch to the ‘Resource’ tab in the project properties window. 74
  4. Drag the file saved in step 2 into the resource list: 75

Result: 76

4. Loading the template file

Create a new Windows form and place a ReoGrid instance on it. Rename the ReoGrid control instance to ‘grid’, and write the following code in the Form_Load event:

using (var ms = new System.IO.MemoryStream(
  ReoGridTester.Properties.Resources.template))
{
  grid.Load(ms, IO.FileFormat.ReoGridFormat);
}

The grid.Load method reads all data from the template spreadsheet from the resource binary stream. Running the application shows the form as below:

77

5. Fill in data

Continue by adding code in the Form_Load event to fill in data:

// get first worksheet instance
var sheet = grid.CurrentWorksheet;

// fill records
sheet["B6"] = new object[,] {
  { "ReoGrid", "sample", "#103, Mars", "02/05/2014", "No" },
  { "User 1", "[email protected]", "-", "01/01/2014", "Yes" },
  { "Guest", "no-email", "Unknown", "01/01/2014", "Yes" }
};

// fill create time
sheet["E2"] = "Create Time: " + DateTime.Now.ToString("MM/dd/yyyy");

Run the program: 79

Presetting cell formats

It is possible to specify the cell data format in the template file.

  1. Open the Editor, right-click on the target cell or range, and choose ‘Format Cells…’ 80
  2. Choose the ‘Format’ tab, select ‘DateTime’, and select the ‘7/13/1980 12:00 AM’ pattern. 81
  3. Click ‘OK’.
  4. Save the template. 5. Update the embedded resource in the project. 6. Run the program. The cells are formatted and displayed as below: 82

Define a named range

A good practice is to define a range with a given name. A named range helps identify where data should be filled.

  1. Select a range on the spreadsheet in the Editor: 101

  2. With the selection active, select the menu ‘Formula’ -> ‘Define named range…’ 103

  3. Enter ‘mydata’ as the name, then click ‘OK’. 104

  4. The name will be displayed in the address field at the top-left of the editor. 105

  5. Save this template and refresh the resource template in your project.

  6. Update the code to use the named range for filling data:

// filling data with named range
sheet["mydata"] = new object[,] {
  { "ReoGrid", "sample", "#103, Mars", "02/05/2014", "No" },
  { "User 1", "[email protected]", "-", "01/01/2014", "Yes" },
  { "Guest", "no-email", "Unknown", "01/01/2014", "Yes" }
};

The spreadsheet will be displayed as below: 106

Was this article helpful?