Giter Club home page Giter Club logo

apodemus.npoi.xwpfmapper's Introduction

NPOI.XWPFMapper

License Build NuGet Badge

This is a library for mapping objects to Word document tables with the NPOI library.

Usage

You can take a look at the NPOI.XWPFMapper.Example project for a working example.

NPOI.XWPFMapper uses the interface IXWPFMappable and the attribute XWPFPropertyAttribute to define which classes should be mappable and which members should be nested or just strings.

internal class ExampleClass : IXWPFMappable // Implement the IXWPFMappable interface to make a class mappable
{
    public XWPFTableAlignment XWPFTableAlignment { get; set; }

    // Add the XWPFPropertyattribute to make a member mappable.
    [XWPFProperty("Color")] 
    public ExampleEnum Enum { get; set; }

    [XWPFProperty("Name")]
    public string Name { get; set; }

    // This child class also implements IXWPFMappable and will become a nested table.
    [XWPFProperty("Address", XWPFTableAlignment.Column)]
    public ExampleChildClass Address { get; set; }

    // Without the XWPFPropertyAttribute a member will be ignored by the mapping.
    public string IgnoredMember { get; set; }
}

internal class ExampleChildClass : IXWPFMappable
{
    public XWPFTableAlignment XWPFTableAlignment { get; set; }

    [XWPFProperty("Street")]
    public string Address { get; set; }
    [XWPFProperty("Place")]
    public string City { get; set; }
    [XWPFProperty("Country")]
    public string CountryCode { get; set; }
}

In addition to this, there is also the XWPFTableWrapper, which is a class that manages an XWPFTable. It only accepts a single Type to be mapped (excluding its members of course).

XWPFDocument document = new XWPFDocument();

ExampleClass exampleData = new ExampleClass()
{
    Enum = ExampleEnum.Red,
    Name = "This is a test",
    IgnoredMember = "This member will be ignored",
    Address = new ExampleChildClass()
    {
        Address = "Burgemeester Schönfeldplein",
        City = "Winschoten",
        CountryCode = "NL"
    }
};

// XWPFTableWrapper requires a Type argument and an XWPFDocument object to work
// In addition you can optionally set the direction of the table with an enum XWPFTableAlignment (default is Row)

XWPFTableWrapper<ExampleClass> wrapper = new XWPFTableWrapper<ExampleClass>(document, XWPFTableAlignment.Column);
wrapper.Insert(exampleData);

XWPFTableWrapper's XWPFTable is public, so you can access it from XWPFTableWrapper.

Table alignment

Table alignment for nested tables can be set at different points, they are prioritised in the followng order:

  • at the XWPFPropertyAttribute
  • at XWPFTableAlignment of the nested object that implements IXWPFMappable
  • inherited from a parent's XWPFTableAlignment

XWPFTableAlignment.Row

Color Name Address
Red This is a test Van Nelleweg 1, Rotterdam

XWPFTableAlignment.Column

Color Red
Name This is a test
Address Van Nelleweg 1, Rotterdam

🏳️‍⚧️ Trans Rights are human rights!

apodemus.npoi.xwpfmapper's People

Contributors

zt-freak avatar

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.