SharpDocx Documentation

repository·master·Indexed 18 days ago

https://github.com/egonl/sharpdocx

A lightweight template engine for generating Microsoft Word documents using embedded C# code within a Word document view. Supports .NET Framework 3.5-4.8 and .NET Standard 2.0, including .NET Core 3.1, .NET 5.0, and .NET 6.0. Features include support for View Models, custom logic via C# snippets, and extensibility through inheritance.

Tokens
405
Snippets
2
Records
4
Agent score
14%

What's inside SharpDocx

  1. Overview of SharpDocx template engine

    master

    SharpDocx is a lightweight template engine for creating Microsoft Word documents. It uses a two-step process:

    1. Create a View: Design a Word document that contains embedded C# code snippets (e.g., <%= DateTime.Now %>). This document acts as your template.
    2. Generate Documents: Use the SharpDocx API to process the view and produce a final .docx file.

    It supports inserting text, tables, images, and more. It is compatible with .NET Framework 3.5-4.8 and .NET Standard 2.0 (including .NET Core 3.1, .NET 5.0, and .NET 6.0).

  2. Extend SharpDocx via inheritance

    master
    If the built-in support for text, tables, and images is insufficient for your requirements, you can extend the library's functionality by creating your own subclass of the document object.
  3. Generate a document from a view

    master

    To generate a document using a basic view (a Word document containing C# code), use DocumentFactory.Create to load the template and document.Generate to produce the output file.

    document.Generate("output.docx");
  4. Generate a document using a View Model

    master

    You can pass a custom object (a View Model) to your template to allow for complex logic, such as iterating over collections using <% foreach (var item in Model.MyList) { %>.

    To do this, pass the model instance to both DocumentFactory.Create and document.Generate.

    document.Generate("output.docx", myModel);