Intro

Very simple sample that demonstrates how to use strings of data for an Excel report.


The BindCellData method is used to assign single values to data markers with ExcelTemplate.

To mark where single values on a template would go, place a single object data marker, formatted:%%=$variablename.

This sample requires OfficeWriter Enterprise Edition to be installed because the OfficeWriter Grouping and Nesting is only available in the Enterprise Edition of the product.

Code

public class StringDataSource
    {
        private string recipientName;
        private string recipientCompany;

        /// <summary>
        /// Build the report with ExcelTemplate
        /// </summary>
        public void GenerateReport()
        {

            this.recipientName = "Jon Smith";
            this.recipientCompany = "SoftArtisans";
            // Create an instance of SoftArtisans ExcelTemplate
            ExcelTemplate xlt = new ExcelTemplate();

            // Open the template workbook
                        string templatePath = @"..\..\ExcelTemplateFiles\StringDataSourceTemplate.xlsx";
            xlt.Open(templatePath);

            // Bind the variables to the template datamarkers
            // %%=$RecipientName

            xlt.BindCellData(recipientName, "RecipientName", xlt.CreateDataBindingProperties());

            // %%=$RecipientCompany
            xlt.BindCellData(recipientCompany, "RecipientCompany", xlt.CreateDataBindingProperties());

            // Process the template to populate it with the Data Source data
            xlt.Process();

            // Save the report by streaming to the client
            xlt.Save(@"..\..\ExcelOutputFiles\StringDataSource_output.xlsx");

        }
    }

Downloads