Skip to content

Export Modifying Templates

Andrei Ignat edited this page Jan 9, 2016 · 9 revisions

#Customizing templates to export

##Templates definition

By default Exporter have his set of templates to export.

There are 3 templates:

1 ExportItem - this is a template for generate text for a single instance of an object from your list/csv/datatable/json For a List it is a template for a Person

2 ExportHeader - this is a template for generate text for a header of you list/csv/datatable/json For a List it is a template for properties of the Person - FirstName, LastName ...

3 ExportCollection - this is the main template . It shows the whole file - e.g. the template for the PDF file

##Templates customization First, you must export the templates to see them to customize:

    var export = new ExportExcel2007<Person>();
    File.WriteAllText("Excel2007Header.txt",export.ExportHeader);
    File.WriteAllText("Excel2007Item.txt", export.ExportItem);
    File.WriteAllText("Excel2007Collection.txt", export.ExportCollection);

Then you can modify and add whatever you want to them.Next step is to add them back

    export = new ExportExcel2007<Person>();
    File.WriteAllText("Excel2007Header.txt",export.ExportHeader);
    File.WriteAllText("Excel2007Item.txt", export.ExportItem);
    File.WriteAllText("Excel2007Collection.txt", export.ExportCollection);
    export.ExportHeader = File.ReadAllText("Excel2007Header.txt");
    export.ExportItem = File.ReadAllText("Excel2007Item.txt");
    export.ExportCollection = File.ReadAllText("Excel2007Collection.txt");
    data = export.ExportResult(new List<Person>() { p });
    File.WriteAllBytes("WithId.xlsx", data);
    Process.Start("WithId.xlsx");

##Example If you open the ExporterConsole from the github source, you will see how I can add an Number to the default template. This is to number the items to export( 1,2,3 and so on) The modification are:

file : Excel2007Header.txt

    <!-- begin added -->
    <c t='inlineStr'>
                <is>
                    <t>Number</t>
                </is>
            </c>
    <!-- end added -->

file: Excel2007Item.txt

<!-- begin added -->
	<c t='inlineStr'>
                <is>
                    <t>@ViewBag.NrItem 
                    </t>
                </is>
    </c>
	<!-- end added -->

file:Excel2007Collection.txt

@{
	//added number 
	int nrItem=0;
}
@foreach(var item in Model.Data){
   ViewBag.NrItem = ++nrItem;  //this  is new
   @Include(Model.NameOfT+"Excel2007Item",item);
} 

##Final note If you have a beautiful template , please send back to me - I have email on yahoo, on my name: ignatandrei. Thank you ;-)

Clone this wiki locally