TL;DR Can I generate Word documents with .NET like XAML ItemTemplates?
I'm finding it pretty hard to come up with a solution that meets all my requirements, so I thought I'll throw this out to stackoverflow in the hope someone can guide me. Much thanks in advance.
Simply put, I need to generate Word document based on data from a database.
My ideal solution: I want it to work like how DataTemplates work in xaml. I've found heaps of examples and solutions where the Template represents a static document, with bits of (single) dynamic content which needs to be replaced.
e.g. WordDocGenerator
The thing is, I need a solution where I can specify a template for each level of the hierarchy, and the document generator will use these item level templates to build a final document based on a Document level template.
My specific requirements are:
- Preferably .NET solution
- No need for office to be installed on the server
- A template exists that encapsulates purely the 'View' of the document (doesn't necessarily have to be a Word template) which a user can modify at will (within boundaries of course). This is very important, as the user needs to control the presentation by just modifying the Word template directly.
- Preferably at each level of data hierarchy there will be an accompanying template.
- Headers and footers
- Table of Contents
Let's say the data hierarchy is like this
class Country
{
public string Name { get; set; }
public IList<City> { get; set; }
}
class City
{
public string Name { get; set; }
public IList<Suburb> { get; set;}
}
class Suburb
{
public string Name { get; set; }
public int Postcode { get; set; }
}
In my mind the solution will be a function call, which accepts a list of countries.
// returns path to generated document
public static string GenerateDocument(IList<Country> countries);
This function will accept the list of countries, and for each country
- use a prepared template for Countries to present the country data.
- for each City in country, use prepared template for Cities to present the City data inside the Country template
- for each Suburb in city, use prepared template for Suburbs to present the Suburb data inside the City template.
Finally, these generated document 'pieces' will be accumulated into one final Word Document using a Document level template, which will specify the Title page, headers/footers, TOC.
docx
documents are ZIP archives of plain old XML. The sky is the limit, you can manipulate the XML however you want without even needing to have Office installed, and no need for a third-party library; just set up bookmarks in the Word template around the sections that you want to act as Country-, City-, Suburb-subtemplates, open up thedocx
manually to see what that looks like in XML, then programatically open the template XML, pull the bookmarked sections, and replicate them for as many countries/cities/etc. you have. – vladrdocx
as a zip archive you'll seeword\document.xml
, and in there you'll see e.g.<w:bookmarkStart w:id="0" w:name="country_template"/><w:p...><...>This is country #Country</...></w:p><w:bookmarkStart w:id="1" w:name="city_template"/><w:p...><...>This is city #City</...></w:p><w:bookmarkStart w:id="2" w:name="suburb_template"/><w:p...><...>This is suburb #Suburb</...></w:p><w:bookmarkEnd w:id="2"/><w:bookmarkEnd w:id="1"/><w:bookmarkEnd w:id="0"/>
– vladr