Giter VIP home page Giter VIP logo

npoi.wordtemplatemapper's Introduction

NPOI.WordTemplateMapper

This is a Word document mapper which can map a disctionary of objects to a document.

usage

Example usage

using NPOI.WordTemplateMapper.XWPF;
using NPOI.XWPF.UserModel;

// Assemble the data
List<object> theList = new() {
    new { A = "Chirp", B = "Meow", C = "Woof", D = "Moo", E = "Bonjour", F = "Quack"},
    new { A = "Monday", B = "Tuesday", C = "Wednesday", D = "Thursday", E = "Friday", F = "Saturday"},
    new { A = "Red", B = "Green", C = "Blue", D = "Yellow", E = "Magenta", F = "Cyan"},
    new { A = "Belgium", B = "Denmark", C = "Spain", D = "Austria", E = "Monaco", F = "Angola"},
    new { A = PokeType.Water, B = PokeType.Grass, C = PokeType.Fire, D = PokeType.Ghost, E = PokeType.Electric, F = PokeType.Fighting},
};
Dictionary<string, object> data = new()
{
    { "{{List}}", theList },
    { "{{CoolSinger}}", "Rick Astley" },
    { "{{ListTotal}}", 42069 },
    { "{{Pokemon}}", new { Name = "Pikachu", PokeType = PokeType.Electric } },
    { "{{Phone}}", new { Brand = new { Name = "Huawei"}, Model = "Mate 50 Pro" } },
    { "{{Actions}}", new string[] { "give you up", "let you down", "run around and desert you" } },
    { "{{Wow}}", new Array[] { new object[] { new { Yay = "waaaa" } } } }
};

// Get the Document
string template = @"A.docx";
using FileStream fileStream = File.OpenRead(template);
XWPFDocument document = new(fileStream);

// Map document
XWPFDocumentMapper documentMapper = new();

documentMapper.MapDocument(document, data);

// Create new document
string generateFile = @"output.docx";
using FileStream outputStream = File.Create(generateFile);
document.Write(outputStream);

Mapping

Mapping keys must start and end with non alphanumeric characters, preferebly {{two accolades like these}}.

Mapping table rows

Add your key to the title of the table. This can be accessed under "Table Properties" in Microsoft Word. This key will be cleaned up during mapping, so won't need to worry about accessibility.

npoi.wordtemplatemapper's People

Contributors

zt-freak avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

gaozejie

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.