Export Data to a Predefined Excel Template in C#
Tags: csharp, dotnet, excel, template
Published: 2020-05-28 12:10:36

---
title: Export Data to a Predefined Excel Template in C#
published: true
date: 2020-05-20 11:00:53 UTC
tags: csharp, dotnet, excel, template
canonical_url: https://www.syncfusion.com/blogs/post/export-data-to-a-predefined-excel-template-in-c.aspx
cover_image: https://www.syncfusion.com/blogs/wp-content/uploads/2020/05/Export-data-to-predefined-Excel-template-in-C-sharp.png
---

Excel templates are spreadsheets that have predesigned formats. Data exported to such spreadsheets will be formatted according to the template files. In this blog, we are going to see how to export data to a predefined Excel template in C# using a **template marker** in [Essential XlsIO](https://www.syncfusion.com/excel-framework/net/excel-library).

## Template marker

A [template marker](https://help.syncfusion.com/file-formats/xlsio/working-with-template-markers) is a special marker symbol used in the worksheet cells of an Excel template file. It binds to the class objects and fills multiple records from various data sources such as [data table](https://help.syncfusion.com/file-formats/xlsio/working-with-template-markers#bind-from-datatable)s, [collection objects](https://help.syncfusion.com/file-formats/xlsio/working-with-template-markers#bind-from-collection-objects-with-images), [arrays](https://help.syncfusion.com/file-formats/xlsio/working-with-template-markers#bind-from-array), and [nested data](https://www.syncfusion.com/kb/8535/how-to-implement-nested-template-marker-in-xlsio) into a worksheet.

For example, the following screenshot shows how to use the marker variable. The symbol “%” is used as marker for the class _SalesList_ and its properties _SalesPerson_ and _SalesJulyDec_. The template file has some cell formatting.

![Input template with markers](https://www.syncfusion.com/blogs/wp-content/uploads/2020/05/Input-template-with-markers.png)<figcaption>Input template with markers</figcaption>

### Syntax

Each marker starts with a prefix character (by default, “%”). The marker is followed by a variable name and then properties, which are delimited by a character (by default, “;”.)

![Syntax](https://dev-to-uploads.s3.amazonaws.com/i/duijel4i1i8ppe1rqwqd.png)

For the previous Excel template with markers applied, the exported data will be as shown in this screenshot.

![Output file with data exported using markers](https://www.syncfusion.com/blogs/wp-content/uploads/2020/05/Output-file-with-data-exported-using-markers.png)<figcaption>Output file with data exported using markers</figcaption>

It is that simple to export data to a predefined Excel template. In this sample, I just read an XML file, prepared a data table, and exported the data with three lines of code. Of course, you need to write a few lines of other code to create an Excel workbook. The code example is given here.

```csharp
using Syncfusion.XlsIO;
using System.Data;
using System.IO;
using System.Reflection;
 
namespace TemplateMarker
{
    class Program
    {
        static void Main(string[] args)
        {
            //Code to read XML data to create a DataTable
            Assembly assembly = typeof(Program).GetTypeInfo().Assembly;
            Stream dataStream = assembly.GetManifestResourceStream("TemplateMarker.Data.customers.xml");
            DataSet customersDataSet = new DataSet();
            customersDataSet.ReadXml(dataStream, XmlReadMode.ReadSchema);
            DataTable northwindDt = customersDataSet.Tables[0];
 
            using (ExcelEngine excelEngine = new ExcelEngine())
            {
                IApplication application = excelEngine.Excel;
                application.DefaultVersion = ExcelVersion.Excel2016;
 
                //Open an existing spreadsheet, which will be used as a template for generating the new spreadsheet.
                //After opening, the workbook object represents the complete in-memory object model of the template spreadsheet.
                IWorkbook workbook;
 
                //Open existing Excel template
                Stream cfFileStream = assembly.GetManifestResourceStream("TemplateMarker.Data.TemplateMarker.xlsx");
                workbook = excelEngine.Excel.Workbooks.Open(cfFileStream);
 
                //The first worksheet in the workbook is accessed.
                IWorksheet worksheet = workbook.Worksheets[0];
 
                //Create Template Marker processor.
                //Apply the marker to export data from datatable to worksheet.
                ITemplateMarkersProcessor marker = workbook.CreateTemplateMarkersProcessor();
                marker.AddVariable("SalesList", northwindDt);
                marker.ApplyMarkers();
 
                //Saving and closing the workbook
                workbook.SaveAs("TemplateMarkerOutput.xlsx");
 
                //Close the workbook
                workbook.Close();
            }
        }
    }
}
```

**Note:** You can change the marker prefix and delimiter characters with the [_MarkerPrefix_](https://help.syncfusion.com/cr/cref_files/file-formats/Syncfusion.XlsIO.Base~Syncfusion.XlsIO.ITemplateMarkersProcessor~MarkerPrefix.html) and [_ArgumentSeparator_](https://help.syncfusion.com/cr/cref_files/file-formats/Syncfusion.XlsIO.Base~Syncfusion.XlsIO.ITemplateMarkersProcessor~ArgumentSeparator.html) properties of the [_ITemplateMarkersProcessor_](https://help.syncfusion.com/cr/file-formats/Syncfusion.XlsIO.Base~Syncfusion.XlsIO.ITemplateMarkersProcessor.html) instance respectively.

### How does a template marker work?

Markers are applied in an Excel template to the required cells. This includes the data source name and the field name of interest. During data binding, a search is conducted for the data source name and the field name in the Excel workbook and the corresponding data from the data source is bound to the marker. Cells in the worksheet can be filled with a single data source or with multiple records. The format of this data can be changed using the arguments of the markers. Let me explain what I mean when I say arguments, and what types there are.

### What is an argument in a template marker?

The [syntax of a template marker](https://help.syncfusion.com/file-formats/xlsio/working-with-template-markers#template-marker-syntax) has three parts: _marker variable_, _property_, and _arguments_. Arguments are separated by semi colon (;). The following are the arguments are supported in template markers:

- [vertical](#vertical)
- [horizontal](#horizontal)
- [insert](#insert)
- [jump](#jump)
- [copyrange](#copy-range)
- [size](#fit-to-cell)

Let’s see these arguments in brief with screenshots that illustrate the output.

#### vertical

The [_vertical_](https://www.syncfusion.com/kb/8384/how-to-use-vertical-argument-in-template-markers-using-xlsio) argument specified in a template marker fills data in vertically. By default, the data-fill direction is vertical. So, it is not necessary to give a _vertical_ argument.

The following screenshot shows the input template with a vertical argument.

![Input template with vertical argument](https://www.syncfusion.com/blogs/wp-content/uploads/2020/05/Input-template-with-vertical-argument.png)<figcaption>Input template with vertical argument</figcaption>

The next screenshot shows the output with a _vertical_ argument.

![Data filled with vertical argument](https://www.syncfusion.com/blogs/wp-content/uploads/2020/05/Data-filled-with-vertical-argument.png)<figcaption>Data filled with vertical argument</figcaption>

#### horizontal

The [_horizontal_](https://www.syncfusion.com/kb/8383/how-to-use-horizontal-argument-in-template-markers-using-xlsio) argument specified in a template marker fills data in horizontally. The following screenshot shows the input template with a _horizontal_ argument.

![Input template with horizontal marker](https://www.syncfusion.com/blogs/wp-content/uploads/2020/05/Input-template-with-horizontal-marker.png)<figcaption>Input template with horizontal marker</figcaption>

The next screenshot shows the output with a _horizontal_ argument.

![Data filled with horizontal argument](https://www.syncfusion.com/blogs/wp-content/uploads/2020/05/Data-filled-with-horizontal-argument.png)<figcaption>Data filled with horizontal argument</figcaption>

#### insert

The [_insert_](https://www.syncfusion.com/kb/8501/how-to-use-insert-argument-in-template-markers-using-xlsio) argument inserts new rows or columns while exporting. This option is useful if data is to be exported between other rows.

For example, in the following screenshot there is content in the seventh row. But we need to export data from the fifth row. Without the _insert_ argument, if data is exported, the content in the seventh row will overlap. To avoid that, the _insert_ argument exports data by inserting new rows, which moves the content from the seventh row to the bottom. By default, rows will be inserted vertically.

![Input template with insert argument](https://www.syncfusion.com/blogs/wp-content/uploads/2020/05/Input-template-with-insert-argument.png)<figcaption>Input template with insert argument</figcaption>

In addition, the insert argument enables two parameters _copystyles_ and _copymerges_. These parameters will copy existing cells styles and merged cells into the new rows that are being inserted while exporting.

The following screenshot shows the output after export with the insert argument along with the _copystyles_ and _copymerges_ parameters.

![Output of data exported with row insertion](https://www.syncfusion.com/blogs/wp-content/uploads/2020/05/Output-of-data-exported-with-row-insertion.png)<figcaption>Output of data exported with row insertion</figcaption>

**Export data horizontally with column insertion**

You can also insert columns if data is being exported horizontally. The following screenshot shows the input template with _insert_ and _horizontal_ arguments.

![Input template with insert and horizontal arguments](https://www.syncfusion.com/blogs/wp-content/uploads/2020/05/Input-template-with-insert-and-horizontal-arguments.png)<figcaption>Input template with insert and horizontal arguments</figcaption>

![Output of data exported horizontally and with column insertion](https://www.syncfusion.com/blogs/wp-content/uploads/2020/05/Output-of-data-exported-horizontally-and-with-column-insertion.png)<figcaption>Output of data exported horizontally and with column insertion</figcaption>

#### jump

If you want to export data to alternative rows or columns, or by skipping certain rows or columns, the [_jump_](https://www.syncfusion.com/kb/8455/how-to-use-jump-argument-in-template-markers-using-xlsio) argument will be useful.

In this case, the cell references must be added to the _jump_ argument, and can be a relative reference or an absolute reference. You must know the differences between these references, because we will be using a relative reference in the following sections of the article.

![Relative Reference](https://dev-to-uploads.s3.amazonaws.com/i/1v046em2e3yvipt7wdv9.png)

The following screenshot shows the input template with the _jump_ argument and with relative reference, which exports data into alternative rows. Initially, the data will be exported to the fourth row, then to the sixth row, and then to the eighth row, etc.  There won’t be any change in the column level, as there is no index given to the jump.

![Input template with jump argument](https://www.syncfusion.com/blogs/wp-content/uploads/2020/05/Input-template-with-jump-argument.png)<figcaption>Input template with jump argument</figcaption>

The output of data export with the _jump_ argument.

![Output file with jump argument](https://www.syncfusion.com/blogs/wp-content/uploads/2020/05/Output-file-with-jump-argument.png)<figcaption>Output file with jump argument</figcaption>

**Note:** It is mandatory to provide a jump argument to all the rows and columns. If not provided, data in that row or column will be exported as usual.

#### copyrange

The [_copyrange_](https://www.syncfusion.com/kb/8456/how-to-use-copyrange-argument-in-template-markers-using-xlsio) argument copies specified cells into all other rows while exporting. In this case, the data in the cells to be copied can be added in the template itself. So, while exporting the data, these cells will be copied to the rows that are exported.

The next screenshot shows the input template with data in the cells D4 and E4. The _copyrange_ argument at cell A4 has the relative reference to copy the fourth and fifth column values of the current row into other rows.

**Note:** To learn more about relative and absolute references, you can refer to the table mentioned in the jump argument section.

![Input template with CopyRange argument](https://www.syncfusion.com/blogs/wp-content/uploads/2020/05/Input-template-with-CopyRange-argument.png)<figcaption>Input template with CopyRange argument</figcaption>

The following screenshot shows the output, where the data in the cells D4 and E4 are copied to all the rows.

![Output file with CopyRange argument](https://www.syncfusion.com/blogs/wp-content/uploads/2020/05/Output-file-with-CopyRange-argument.png)<figcaption>Output file with CopyRange argument</figcaption>

#### size, position, and fittocell

There are ways to export data with images. While exporting with images, set the image size to fit to the cell, to a specified size, or at a position. To fulfill that requirement, XlsIO provides these arguments:

- size
- position
- fittocell

##### size

The [_size_](https://www.syncfusion.com/kb/8450/how-to-use-size-position-fittocell-arguments-in-template-markers-using-xlsio) argument specified in a template marker applies to images that are exported to worksheets. The actual size of images that are exported may differ. To export them into cells to fit for clear visualization, this argument helps in setting the height and width of images.

Here, the _height_ parameter is optional. The value of _width_ is applied when _height_ is not specified.

The following screenshot shows the input template with the _size_ argument at the cell A4.

![Input template with size argument](https://www.syncfusion.com/blogs/wp-content/uploads/2020/05/Input-template-with-size-argument.png)<figcaption>Input template with size argument</figcaption>

The next screenshot shows the output with images exported in column A with the specified _size_.

![Data filled with size argument](https://www.syncfusion.com/blogs/wp-content/uploads/2020/05/Data-filled-with-size-argument.png)<figcaption>Data filled with size argument</figcaption>

##### position

The _position _argument sets alignment of images to the _left_, _center_, _right_, _top_, _middle_, or _bottom_ while exporting. If a position is not given to the images column, it will be considered top-left by default.

The following screenshot shows the input template with the _position_ argument at the cell A4.

![Input template with position argument](https://www.syncfusion.com/blogs/wp-content/uploads/2020/05/Input-template-with-position-argument.png)<figcaption>Input template with position argument</figcaption>

The next screenshot shows the output with images exported in column A as right aligned.

![Data filled with position argument](https://www.syncfusion.com/blogs/wp-content/uploads/2020/05/Data-filled-with-position-argument.png)<figcaption>Data filled with position argument</figcaption>

##### fittocell

The _fittocell_ argument fits images within cells while exporting. Here, the image height and width are not necessary to include.

The following screenshot shows the input template with the _fittocell_ argument.

![Input template with fittocell argument](https://www.syncfusion.com/blogs/wp-content/uploads/2020/05/Input-template-with-fittocell-argument.png)<figcaption>Input template with fittocell argument</figcaption>

The next screenshot shows the output with images fit to the cells.

![Data filled with fittocell argument](https://www.syncfusion.com/blogs/wp-content/uploads/2020/05/Data-filled-with-fittocell-argument.png)<figcaption>Data filled with fittocell argument</figcaption>

### Advanced features of template markers

We have seen how to use template markers and their arguments to customize data export to any desired layout. This is not all template markers can do. Template markers have more advanced features to highlight, where you can export data with:

- [Formulas](#formula)
- [Images](#images)
- [Conditional formatting](#conditional-formatting)
- [Nested collection objects](#nested-collection-objects)

Let’s see these advanced features one by one with code examples.

#### Export data to Excel template with formulas

Be it the simple addition of a long row of numbers, a sales quote, or complex financial modeling, formulas play a vital role in computing data. You can also use formulas to build columns with your exported data.

I am going to have a column that will add two of my exported columns. Let’s see the steps to achieve this.

**Step 1:** Add the following argument to any column that is a part of the computation. The **RC4** here refers to the range (current row and fourth column) where the formula is given and to be repeated in subsequent rows.

```
%NumbersTable.Column2;copyrange:RC4
```

![Input template with markers to import data with images using template markers](https://www.syncfusion.com/blogs/wp-content/uploads/2020/05/Input-template-with-markers-to-import-data-with-images-using-template-markers.png)<figcaption>Input template with markers to import data with images using template markers</figcaption>

**Step 2:** All set! Now export the data.

```csharp
//Create Template Marker Processor
ITemplateMarkersProcessor marker = workbook.CreateTemplateMarkersProcessor();
 
//Add marker variable
marker.AddVariable("NumbersTable", GetTable());
 
//Process the markers in the template
marker.ApplyMarkers();
```

Now, after exporting, the final output will be like this one.

![Data exported into Excel worksheet with formulas](https://www.syncfusion.com/blogs/wp-content/uploads/2020/05/Data-exported-into-Excel-worksheet-with-formulas.png)<figcaption>Data exported into Excel worksheet with formulas</figcaption>

A complete working example along with the [input file](http://www.syncfusion.com/downloads/support/directtrac/general/ze/Formulas1984099616) can be downloaded from [export-data-with-formulas.zip](http://www.syncfusion.com/downloads/support/directtrac/general/ze/Using_Formulas_on_import-1869293148).

If you are interested in learning more about how to work with formulas using XlsIO, you can go through the [working with formulas](https://help.syncfusion.com/file-formats/xlsio/working-with-formulas) documentation.

#### Export data to Excel template with images

You can bind image data from your data sources. You can not only import images but their specified sizes and positions, too.

Let’s see the steps to achieve this.

**Step 1:** Add a marker variable with a bit of additional information, like size and position of image.

![Input template with markers to export data with images using template markers](https://www.syncfusion.com/blogs/wp-content/uploads/2020/05/Input-template-with-markers-to-export-data-with-images-using-template-markers.png)<figcaption>Input template with markers to export data with images using template markers</figcaption>

**Step 2:** Now export the data.

```csharp
//Create Template Marker Processor
ITemplateMarkersProcessor marker = workbook.CreateTemplateMarkersProcessor();
 
//Get first 5 items in the collection and bind them to the marker
marker.AddVariable("SalesList", GetCustomerAsObjects().Take<Customer>(5).ToArray());
 
//Process the markers in the template
marker.ApplyMarkers();
```

Here is the exported data in a neatly formatted Excel worksheet using a template marker.

![Data exported with images in a formatted layout using template markers](https://www.syncfusion.com/blogs/wp-content/uploads/2020/05/Data-exported-with-images-in-a-formatted-layout-using-template-markers.png)<figcaption>Data exported with images in a formatted layout using template markers</figcaption>

A complete working example along with the [input file](http://www.syncfusion.com/downloads/support/directtrac/general/ze/TemplateMarkerImages1646181408) can be downloaded from [export-data-with-images.zip](http://www.syncfusion.com/downloads/support/directtrac/general/ze/Control_data_formatting-394790291).

Go through the documentation to learn more on [importing data with image](https://help.syncfusion.com/file-formats/xlsio/working-with-template-markers#bind-from-business-objects-with-images)s using **template markers** in **XlsIO**.

#### Export data to Excel template with conditional formatting

[Conditional formatting](https://support.office.com/en-us/article/use-formulas-with-conditional-formatting-fed60dfa-1d3f-4e13-9ecb-f1951ff89d7f?ui=en-US&rs=en-US&ad=US) provides another way to figure out patterns in data, which is difficult from examining the raw information, similar to charts or sparklines.

For example, I have a sales report with two sets of six-months’ sales figures, Jan-June and July-Dec, in two different columns. It would be difficult to compare the sales for different sales reps without analysis. We can add conditional formatting to show the data variance in the sales.

Let’s see the steps to achieve this.

**Step 1:** Add marker variable to bind columns.

![Input template with markers and conditional formats](https://www.syncfusion.com/blogs/wp-content/uploads/2020/05/Input-template-with-markers-and-conditional-formats.png)<figcaption>Input template with markers and conditional formats</figcaption>

**Step 2:** Apply conditional formatting to the marker variable rows and export.

```csharp
//Create Template Marker Processor
ITemplateMarkersProcessor marker = workbook.CreateTemplateMarkersProcessor();
 
//Applying conditional formats
#region Data Bar
IConditionalFormats conditions = marker.CreateConditionalFormats(worksheet["C5"]);
IConditionalFormat condition = conditions.AddCondition();
 
//Set data bar and icon set for the same cell
//Set the format type
condition.FormatType = ExcelCFType.DataBar;
IDataBar dataBar = condition.DataBar;
 
//Set the constraint
dataBar.MinPoint.Type = ConditionValueType.LowestValue;
dataBar.MaxPoint.Type = ConditionValueType.HighestValue;
 
//Set color for Bar
dataBar.BarColor = Color.FromArgb(156, 208, 243);
 
//Hide the value in data bar
dataBar.ShowValue = false;
#endregion
 
#region Color Scale
conditions = marker.CreateConditionalFormats(worksheet["D5"]);
condition = conditions.AddCondition();
 
condition.FormatType = ExcelCFType.ColorScale;
IColorScale colorScale = condition.ColorScale;
 
//Sets 3 - color scale
colorScale.SetConditionCount(3);
 
colorScale.Criteria[1].FormatColorRGB = Color.FromArgb(244, 210, 178);
colorScale.Criteria[1].Type = ConditionValueType.Percentile;
colorScale.Criteria[1].Value = "50";
 
colorScale.Criteria[2].FormatColorRGB = Color.FromArgb(245, 247, 171);
colorScale.Criteria[2].Type = ConditionValueType.Percentile;
colorScale.Criteria[2].Value = "100";
#endregion
 
//Add marker variable
marker.AddVariable("SalesList", GetCustomerAsObjects());
 
//Process the markers in the template
marker.ApplyMarkers();
```

Here is how the data would look after the export.

![Exported data with conditional formatting using template markers](https://www.syncfusion.com/blogs/wp-content/uploads/2020/05/Exported-data-with-conditional-formatting-using-template-markers.png)<figcaption>Exported data with conditional formatting using template markers</figcaption>

A complete working example along with the [input file](http://www.syncfusion.com/downloads/support/directtrac/general/ze/TemplateMarkerImages851206786) can be downloaded from [export-data-with-conditional-formats.zip](http://www.syncfusion.com/downloads/support/directtrac/general/ze/Imported_data_with_Conditional_Format-1154265375).

To learn more about applying other advanced conditional formats, please go through the [template marker with conditional formatting](https://help.syncfusion.com/file-formats/xlsio/working-with-template-markers#template-marker-with-conditional-formatting) documentation.

#### Export data to Excel template with nested collection objects

You may handle hierarchical data in nested collection objects but may not be able to view the data in its structure to analyze it. Syncfusion Excel Library lets you export data from nested collections to an Excel worksheet with more flexible options to analyze data in its hierarchical structure. Here, the template marker plays a grand role in exporting such nested data in different layouts and grouping the exported data.

Data import can be done with these layout options:

- **Default** : Parent records imported in the first row of its collection.
- **Merge** : Parent records imported in merged rows.
- **Repeat** : Parent records imported in all the rows.

The following steps shows how to export in these layout options.

**Step 1** : Opens the input Excel template.

**Step 2** : Reads the XML data to get the hierarchical data.

**Step 3** : Assigns the values read from XML file to respective collection objects.

**Step 4** : Applies the marker to the Excel worksheet.

**Step 5** : Saves the workbook.

```
using Syncfusion.XlsIO;using System.Collections.Generic;using System.IO;using System.Xml.Serialization;namespace Template\_Marker\_Nested\_Collection{ class Program { static void Main(string[] args) { ApplyLayout(); } static void ApplyLayout() { using (ExcelEngine excelEngine = new ExcelEngine()) { IApplication application = excelEngine.Excel; application.DefaultVersion = ExcelVersion.Excel2016; IWorkbook workbook = excelEngine.Excel.Workbooks.Open("../../Data/NestedColl-Template-Default.xlsx"); IWorksheet worksheet = workbook.Worksheets[0]; //Create Template Marker Processor ITemplateMarkersProcessor marker = workbook.CreateTemplateMarkersProcessor(); //Add collection to the marker variables where the name should match with input template marker.AddVariable("BrandObject", GetVehicleDetails()); //Process the markers in the template marker.ApplyMarkers(); string fileName = @"ExportData-DefaultLayout.xlsx"; workbook.SaveAs(fileName); workbook.Close(); } } #region Helper Methods //Helper Method static private IList<Brand> GetVehicleDetails() { XmlSerializer deserializer = new XmlSerializer(typeof(BrandObjects)); //Read data from XML file. TextReader textReader = new StreamReader(@"..\..\Data\ExportData.xml"); BrandObjects brands = (BrandObjects)deserializer.Deserialize(textReader); //Initialize parent collection to add data from XML file. List<Brand> list = new List<Brand>(); string brandName = brands.BrandObject[0].BrandName; string vehicleType = brands.BrandObject[0].VahicleType; string modelName = brands.BrandObject[0].ModelName; //Parent class Brand brand = new Brand(brandName); brand.VehicleTypes = new List<VehicleType>(); VehicleType vehicle = new VehicleType(vehicleType); vehicle.Models = new List<Model>(); Model model = new Model(modelName); brand.VehicleTypes.Add(vehicle); list.Add(brand); foreach (BrandObject brandObj in brands.BrandObject) { if (brandName == brandObj.BrandName) { if (vehicleType == brandObj.VahicleType) { vehicle.Models.Add(new Model(brandObj.ModelName)); continue; } else { vehicle = new VehicleType(brandObj.VahicleType); vehicle.Models = new List<Model>(); vehicle.Models.Add(new Model(brandObj.ModelName)); brand.VehicleTypes.Add(vehicle); vehicleType = brandObj.VahicleType; } continue; } else { brand = new Brand(brandObj.BrandName); vehicle = new VehicleType(brandObj.VahicleType); vehicle.Models = new List<Model>(); vehicle.Models.Add(new Model(brandObj.ModelName)); brand.VehicleTypes = new List<VehicleType>(); brand.VehicleTypes.Add(vehicle); vehicleType = brandObj.VahicleType; list.Add(brand); brandName = brandObj.BrandName; } } textReader.Close(); return list; } #endregion }}
```

Let’s see these options in detail along with input templates and output screenshots.

##### Layout options

###### Default layout option

This option exports parent records in the first row of its collection.

![Input template for default layout option](https://www.syncfusion.com/blogs/wp-content/uploads/2020/05/Input-template-for-default-layout-option.png)<figcaption>Input template for default layout option</figcaption>

The following screenshot represents the output document with the _default_ layout option.

![Spreadsheet output with default layout option](https://www.syncfusion.com/blogs/wp-content/uploads/2020/05/Spreadsheet-output-with-default-layout-option.png)<figcaption>Spreadsheet output with default layout option</figcaption>

###### Merge layout option

This option exports parent records with merged rows.

![Input template for merge layout option](https://www.syncfusion.com/blogs/wp-content/uploads/2020/05/Input-template-for-merge-layout-option-1.png)<figcaption>Input template for merge layout option</figcaption>

The following screenshot represents the output document with the _merge_ layout option.

![Spreadsheet output with merge layout option](https://www.syncfusion.com/blogs/wp-content/uploads/2020/05/Spreadsheet-output-with-merge-layout-option.png)<figcaption>Spreadsheet output with merge layout option</figcaption>

###### Repeat layout option

This option repeats the parent records exported in all the rows.

![Input template for repeat layout option](https://www.syncfusion.com/blogs/wp-content/uploads/2020/05/Input-template-for-repeat-layout-option.png)<figcaption>Input template for repeat layout option</figcaption>

The following screenshot represents the output document with the _repeat_ layout option.

![Spreadsheet output with repeat layout option](https://www.syncfusion.com/blogs/wp-content/uploads/2020/05/Spreadsheet-output-with-repeat-layout-option.png)<figcaption>Spreadsheet output with repeat layout option</figcaption>

##### Grouping options

###### Import data with grouping option

Hierarchical data imported into Excel worksheets must be shown in its structure to be analyzed more flexibly. In addition, if the data is grouped according to its level, it is easier to analyze. XlsIO lets you import hierarchical data from nested collections and group them while exporting data using template markers.

The following are the options supported to group on import:

**Expand** : Imported data will be grouped and expanded.

**Collapse** : Imported data will be grouped and collapsed at first level, by default.

In addition,  **CollapseLevel**  will group and collapse the levels, up to the maximum of eight levels.

![Input template for default layout and group options](https://www.syncfusion.com/blogs/wp-content/uploads/2020/05/Input-template-for-default-layout-and-group-options.png)<figcaption>Input template for default layout and group options</figcaption>

The following screenshot represents the output document of grouped data imported from a nested collection and collapsed at level two.

![Spreadsheet output with default layout and group options](https://www.syncfusion.com/blogs/wp-content/uploads/2020/05/Spreadsheet-output-with-default-layout-and-group-options.png)<figcaption>Spreadsheet output with default layout and group options</figcaption>

## GitHub samples

You can download the examples of exporting data to Excel in C# [here](https://github.com/SyncfusionExamples/export-data-to-excel-template-in-c-sharp).

## Conclusion

As you can see, [**Syncfusion Excel Library**](https://www.syncfusion.com/excel-framework/net) (XlsIO) provides various features to export data to a predefined Excel template using template markers in C#. Use them effectively to generate Excel reports with high performance and to process large data. Take a moment to peruse the [documentation](https://help.syncfusion.com/file-formats/xlsio/working-with-data#importing-data-to-worksheets), where you’ll find other options and features, all with accompanying code samples. Using the library, you can also export Excel data to [PDF](https://help.syncfusion.com/file-formats/xlsio/excel-to-pdf-conversion)s, [image](https://help.syncfusion.com/file-formats/xlsio/worksheet-to-image-conversion)s, [data table](https://help.syncfusion.com/file-formats/xlsio/working-with-data#exporting-from-worksheet-to-data-table)s, [CSV](https://www.syncfusion.com/kb/9092/convert-excel-file-to-csv-in-c-vb-net), [TSV](https://www.syncfusion.com/kb/8556/does-xlsio-support-tsv-files), HTML, [collections of objects](https://help.syncfusion.com/file-formats/xlsio/working-with-data#exporting-from-worksheet-to-clr-objects), [ODS](https://help.syncfusion.com/file-formats/xlsio/excel-to-ods-conversion) file format, and more.

If you are new to our Excel Library, it is highly recommended that you follow our [Getting Started guide](https://help.syncfusion.com/file-formats/xlsio/getting-started).

Are you already a Syncfusion user? You can download the product setup [here](https://www.syncfusion.com/account/downloads/studio/). If you’re not yet a Syncfusion user, you can download a free, 30-day trial [here](https://www.syncfusion.com/downloads/fileformats/confirm).

If you have any questions about these features, please let us know in the comments below. You can also contact us through our [support forum](https://www.syncfusion.com/forums?_ga=2.160385213.108886952.1555906495-214292665.1551328372) or [Direct-Trac](https://www.syncfusion.com/support/directtrac/?_ga=2.160385213.108886952.1555906495-214292665.1551328372) or [feedback portal](https://www.syncfusion.com/feedback/). We are happy to assist you!

## Related blogs

- [6 Easy Ways to Export Data to Excel in C#](https://www.syncfusion.com/blogs/post/6-easy-ways-to-export-data-to-excel-in-c-sharp.aspx)
- [How to Export Data SQL Server to Excel Table in C#](https://dev.to/syncfusion/how-to-export-data-from-sql-server-to-excel-table-in-c-fo7)
- [Export Data from a Collection to Excel and Group It in C#](https://dev.to/karthickramasamy08/export-data-from-collection-to-excel-and-group-it-in-c-1j65-temp-slug-6071060)

The post [Export Data to a Predefined Excel Template in C#](https://www.syncfusion.com/blogs/post/export-data-to-a-predefined-excel-template-in-c.aspx) appeared first on [Syncfusion Blogs](https://www.syncfusion.com/blogs).