foreword
ExcelMapper is an open source component based on the MIT protocol, which reads and writes Excel by manipulating the data model in C#. It provides a very neat API, even reading or writing Excel data with one line of code.
Core functions
- Read and write Excel files
- Use a purely managed NPOI library instead of the Jet database engine (NPOI User Group)
- Map to an Excel file with a header row (column names) or column index (no header row)
- Map nested objects (parent/child objects)
- Optionally skip blank lines while reading
- Preserve formatting when saving back to file
- (Optional) Let the mapper keep track of the object
- Map columns to properties by convention, property, or method call
- Use custom or built-in data formats for numeric and datetime columns
- Map formula or formula result based on attribute type
- Mapping JSON
- get/save dynamic object
- usage record
- Provide a custom object factory
GitHub URL: github.com/mganss/Exce…
1. .NET Core uses ExcelMapper
1. Use Nuget to search and install ExcelMapper
2. Read the object from the Excel file
var products = new ExcelMapper("products.xlsx").Fetch<Product>();
复制代码
public class Product
{
public string Name { get; set; }
public int NumberInStock { get; set; }
public decimal Price { get; set; }
}
复制代码
3. Attribute mapping
3.1 Mapping to specific column names
public class Product
{
public string Name { get; set; }
[Column("Number")]
public int NumberInStock { get; set; }
public decimal Price { get; set; }
}
复制代码
3.2 Mapping to column index
public class Product
{
[Column(1)]
public string Name { get; set; }
[Column(Letter="C")]
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 do not need to be contiguous. When mapping to a column index, each property needs to be explicitly mapped through a property or method. You can combine column indices with column names to specify an explicit column order while still using the header row.
3.3 Mapping by method call
In addition to adding features to entity classes, ExcelMapper also supports manual mapping using code, as follows
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), ExcelMapper.LetterToIndex("A"), "NumberInStock");
复制代码
3.4 Multiple mappings
You can map a single column to multiple properties, but you need to understand what should happen when mapping from the object back to Excel. To specify a single property to map back to Excel, add a property that maps to all other properties of the same column. Alternatively, this method can be used when mapping via method calls.
public class Product
{
public decimal Price { get; set; }
[Column("Price", MappingDirections.ExcelToObject)]
public string PriceString { get; set; }
}
// or
excel.AddMapping<Product>("Price", p => p.PriceString).FromExcelOnly();
复制代码
Column attributes are inherited by default, and adding attributes to attributes in Base would result in multiple mappings and derived classes for a single overriding attribute. To prevent this, set the property to false on the property in the base class.
3.5 动态映射
你可以不定义实体类,直接使用 dynamic 类型获取数据,如下
var products = new ExcelMapper("products.xlsx").Fetch(); // -> IEnumerable<dynamic>
products.First().Price += 1.0;
复制代码
3.6 忽略属性
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);
复制代码
3.7 特定数据格式
public class Product
{
[DataFormat(0xf)]
public DateTime Date { get; set; }
[DataFormat("0%")]
public decimal Number { get; set; }
}
复制代码
更多属性使用清参考官网
4.写入 Excel 文件
1、纯写入
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");
复制代码
2、读取写入
var products = new ExcelMapper("products.xlsx").Fetch<Product>().ToList();
products[1].Price += 1.0m;
excel.Save("products.out.xlsx");
复制代码
3、JSON 支持
你可以非常方便的把 Excel 中的数据映射到 Json 类型中,通过使用 Json 特性或者 AsJson 方法,如下
public class ProductJson
{
[Json]
public Product Product { get; set; }
}
// or
var excel = new ExcelMapper("products.xls");
excel.AddMapping<ProductJson>("Product", p => p.Product).AsJson();
复制代码