Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
protected void PopulateTemplate()
{
            xlt = new ExcelTemplate(); //Create a new ExcelTemplate object

            //Pass the existing ExcelApplication object and open Workbook
            //to the ExcelTemplate object
            xlt.Open(xla, wb);

            //RemoveExtraDataMarkers makes all data markers optional
            //so ExcelWriter will not throw an error if any data markers
            //aren't bound to data
            xlt.RemoveExtraDataMarkers = true;

            //For each country, retrieve and bind data to the worksheet
            DataBindingProperties dataBindProps;

            for (int i = 1; i < selectedCountries.Count; i++)
            {
                string country = selectedCountries[i];

                //Create a new DataBindProperties object
                dataBindProps = xlt.CreateDataBindingProperties();

                //Since the data markers for the copied sheets are all the same, 
                //specify the worksheet that the data should be bound to
                dataBindProps.WorksheetName = country;

                //Retrieve the data and column names for the header on the sheet
                string[] headerValues = { "FY 2008", "Foreign Trade Division", country };
                string[] headerNames = { "FiscalYear", "TradeDivision", "Country" };

                //Bind the header row data to the worksheet
                xlt.BindRowData(headerValues, headerNames, "Header", dataBindProps);

                //Get the data for the top 5 sales and all the sales within the year.
                //In this example, GetCSVData method calls on the GenericParsing library
                //to parse and export data from the CSV files located in the //data directory
                DataTable dts = GetCSVData(Page.MapPath("//data//" + country + "5.csv"));
                DataTable dts2 = GetCSVData(Page.MapPath("//data//" + country + "All.csv"));

                //Call ExcelTemplate.BindData to bind the data to the template
                xlt.BindData(dts, "Top", dataBindProps);
                xlt.BindData(dts2, "Details", dataBindProps);
            }
             //Process and stream the final report back to the user
            xlt.Process();
            xlt.Save(Page.Response, "Output.xlsx", false);
        }

17. Now you may run your code. Just call GenerateTemplate() and PopulateTemplate() to customize and populate your template.

...