Skip to content

Commit

Permalink
feat: update README.
Browse files Browse the repository at this point in the history
  • Loading branch information
yingtingxu committed Sep 21, 2017
1 parent 15c8be4 commit 23eac14
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 3 deletions.
83 changes: 81 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Using `Fluent API` to configure POCO excel behaviors, and then provides IEnumera
The first features will be very useful for English not their mother language developers.

# IMPORTAMT
1. The repo fork from my [NPOI.Extension](https://github.com/xyting/NPOI.Extension), and remove all the attributes based features, and will only support `Fluent API`.
1. The repo fork from my [NPOI.Extension](https://github.com/xyting/NPOI.Extension), and remove all the attributes based features (but can be extended, see following demo), and will only support `Fluent API`.
2. All the issues found in [NPOI.Extension](https://github.com/xyting/NPOI.Extension) will be and only be fixed by [FluentExcel](https://github.com/Arch/FluentExcel), so, please update your codes use `FluentExcel`.

# Overview
Expand Down Expand Up @@ -136,14 +136,93 @@ namespace samples
}
```

## Export POCO to excel & Load IEnumerable<T> from excel.
## Saving IEnumerable<T> to excel.

```csharp
var excelFile = @"/Users/rigofunc/Documents/sample.xlsx";

// save to excel file
reports.ToExcel(excelFile);
```

## Loading IEnumerable<T> from excel.

```csharp
// load from excel
var loadFromExcel = Excel.Load<Report>(excelFile);
```

# Extensions

The following demo show how to extend the exist functionalities by extension methods. `NOTE:` the initial idea from @tupunco.

```csharp
public class Report
{
[Display(Name = "城市")]
public string City { get; set; }
[Display(Name = "楼盘")]
public string Building { get; set; }
[Display(Name = "区域")]
public string Area { get; set; }
[Display(Name = "成交时间")]
public DateTime HandleTime { get; set; }
[Display(Name = "经纪人")]
public string Broker { get; set; }
[Display(Name = "客户")]
public string Customer { get; set; }
[Display(Name = "房源")]
public string Room { get; set; }
[Display(Name = "佣金(元)")]
public decimal Brokerage { get; set; }
[Display(Name = "收益(元)")]
public decimal Profits { get; set; }
}
```

Defines the extension methods.

```csharp
public static class FluentConfigurationExtensions
{
public static FluentConfiguration<TModel> FromAnnotations<TModel>(this FluentConfiguration<TModel> fluentConfiguration) where TModel : class
{
var properties = typeof(TModel).GetProperties();
foreach (var property in properties)
{
var pc = fluentConfiguration.Property(property);

var display = property.GetCustomAttribute<DisplayAttribute>();
if (display != null)
{
pc.HasExcelTitle(display.Name);
if (display.GetOrder().HasValue)
{
pc.HasExcelIndex(display.Order);
}
}
else
{
pc.HasExcelTitle(property.Name);
}

var format = property.GetCustomAttribute<DisplayFormatAttribute>();
if (format != null)
{
pc.HasDataFormatter(format.DataFormatString
.Replace("{0:", "")
.Replace("}", ""));
}

if (pc.Index < 0)
{
pc.HasAutoIndex();
}
}

return fluentConfiguration;
}
}
```


2 changes: 1 addition & 1 deletion samples/Report.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ public class Report
[Display(Name = "收益(元)")]
public decimal Profits { get; set; }
}
}
}

0 comments on commit 23eac14

Please sign in to comment.