Giter VIP home page Giter VIP logo

excelmapper's Introduction

ExcelMapper

NuGet version Build status codecov.io netstandard2.0 net45

A library to map POCO objects to Excel files.

Features

  • Read and write Excel files
  • Uses the pure managed NPOI library instead of the Jet database engine for Excel access
  • Map to Excel files using header rows (column names) or column indexes (no header row)
  • Optionally skip blank lines when reading
  • Preserve formatting when saving back files
  • Optionally let the mapper track objects
  • Map columns to properties through convention, attributes or method calls
  • Use custom or builtin data formats for numeric and DateTime columns
  • Map formulas or formula results depending on property type

Read objects from an Excel file

var products = new ExcelMapper("products.xlsx").Fetch<Product>();

This expects the Excel file to contain a header row with the column names. Objects are read from the first worksheet. If the column names equal the property names (ignoring case) no other configuration is necessary. The format of the Excel file (xlsx or xls) is autodetected.

Map to specific column names

public class Product
{
  public string Name { get; set; }
  [Column("Number")]
  public int NumberInStock { get; set; }
  public decimal Price { get; set; }
}

This maps the column named Number to the NumberInStock property.

Map to column indexes

public class Product
{
    [Column(1)]
    public string Name { get; set; }
    [Column(3)]
    public int NumberInStock { get; set; }
    [Column(4)]
    public decimal Price { get; set; }
}

var products = new ExcelMapper("products.xlsx") { HeaderRow = false }.Fetch<Product>();

Note that column indexes don't need to be consecutive. When mapping to column indexes, every property needs to be explicitly mapped through the ColumnAttribute attribute or the AddMapping() method. You can combine column indexes with column names to specify an explicit column order while still using a header row.

Map through method calls

var excel = new ExcelMapper("products.xls");
excel.AddMapping<Product>("Number", p => p.NumberInStock);
excel.AddMapping<Product>(1, p => p.NumberInStock);
excel.AddMapping(typeof(Product), "Number", "NumberInStock");
excel.AddMapping(typeof(Product), 1, "NumberInStock");

Save objects

var products = new List<Product>
{
    new Product { Name = "Nudossi", NumberInStock = 60, Price = 1.99m },
    new Product { Name = "Halloren", NumberInStock = 33, Price = 2.99m },
    new Product { Name = "Filinchen", NumberInStock = 100, Price = 0.99m },
};

new ExcelMapper().Save("products.xlsx", products, "Products");

This saves to the worksheet named "Products". If you save objects after having previously read from an Excel file using the same instance of ExcelMapper the style of the workbook is preserved allowing use cases where an Excel template is filled with computed data.

Track objects

var products = new ExcelMapper("products.xlsx").Fetch<Product>().ToList();
products[1].Price += 1.0m;
excel.Save("products.out.xlsx");

Ignore properties

public class Product
{
    public string Name { get; set; }
    [Ignore]
    public int Number { get; set; }
    public decimal Price { get; set; }
}

// or

var excel = new ExcelMapper("products.xlsx");
excel.Ignore<Product>(p => p.Price);

Use specific data formats

public class Product
{
    [DataFormat(0xf)]
    public DateTime Date { get; set; }

    [DataFormat("0%")]
    public decimal Number { get; set; }
}

You can use both builtin formats and custom formats. The default format for DateTime cells is 0x16 ("m/d/yy h:mm").

Map formulas or results

Formula columns are mapped according to the type of the property they are mapped to: for string properties, the formula itself (e.g. "=A1+B1") is mapped, for other property types the formula result is mapped.

Custom mapping

If you have specific requirements for mapping between cells and objects, you can use custom conversion methods. Here, cells that contain the string "NULL" are mapped to null:

public class Product
{
    public DateTime? Date { get; set; }
}

excel.AddMapping<Product>("Date", p => p.Date)
    .SetCellUsing((c, o) =>
    {
        if (o == null) c.SetCellValue("NULL"); else c.SetCellValue((DateTime)o);
    })
    .SetPropertyUsing(v =>
    {
        if ((v as string) == "NULL") return null;
        return Convert.ChangeType(v, typeof(DateTime), CultureInfo.InvariantCulture);
    });

Header row and data row range

You can specify the row number of the header row using the property HeaderRowNumber (default is 0). The range of rows that are considered rows that may contain data can be specified using the properties MinRowNumber (default is 0) and MaxRowNumber (default is int.MaxValue). The header row doesn't have to fall within this range, e.g. you can have the header row in row 5 and the data in rows 10-20.

excelmapper's People

Contributors

dependabot-preview[bot] avatar mganss avatar

Watchers

 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.