MiniExcel/README.md

221 lines
6.4 KiB
Markdown
Raw Normal View History

2021-03-16 15:24:49 +08:00
[![NuGet](https://img.shields.io/nuget/v/MiniExcel.svg)](https://www.nuget.org/packages/MiniExcel) [![](https://img.shields.io/nuget/dt/MiniExcel.svg)](https://www.nuget.org/packages/MiniExcel) [![Build status](https://ci.appveyor.com/api/projects/status/b2vustrwsuqx45f4/branch/master?svg=true)](https://ci.appveyor.com/project/shps951023/miniexcel/branch/master)
2021-03-25 11:59:05 +08:00
A high performance and easy Excel(xlsx,csv) Micro-Helper that avoids OOM and without any third party dependencies to create file or dynamic/type mapping query etc..
2021-03-03 13:19:27 +08:00
### Features
2021-03-25 11:59:05 +08:00
- Avoid large file OOM(out of memoery) by IEnumerable Lazy loading `step by step getting one row cells` not until all rows read in memory
2021-03-24 17:11:46 +08:00
e.g: Comparison between MiniExcel Query and ExcelDataReader/EPPlus/ClosedXml of reading large Xlsx File
![miniexcel_lazy_load](https://user-images.githubusercontent.com/12729184/111034290-e5588a80-844f-11eb-8c84-6fdb6fb8f403.gif)
2021-03-21 01:02:26 +08:00
- Mini (Less than 100KB) and without any third party library dependencies
2021-03-24 17:11:46 +08:00
- Like Dapper dynamic/type mapping query style
2021-03-25 11:59:05 +08:00
- Create excel file or stream by AnonymousType/DapperRows/Enumrable/DataTable/Dictionary
- Support .NET Standard 2.0/.NET 4.6/.NET 5
2021-03-03 13:19:27 +08:00
### Installation
You can install the package [from NuGet](https://www.nuget.org/packages/MiniExcel)
2021-03-03 13:56:32 +08:00
### Release Notes
Please Check [Release Notes](https://github.com/shps951023/MiniExcel/tree/master/docs)
2021-03-25 15:31:45 +08:00
### Execute a query and map the results to a strongly typed IEnumerable [[Try it]](https://dotnetfiddle.net/fv58u3)
```C#
public class UserAccount
{
public Guid ID { get; set; }
public string Name { get; set; }
public DateTime BoD { get; set; }
public int Age { get; set; }
public bool VIP { get; set; }
public decimal Points { get; set; }
}
using (var stream = File.OpenRead(path))
var rows = stream.Query<UserAccount>();
```
![image](https://user-images.githubusercontent.com/12729184/111107423-c8c46b80-8591-11eb-982f-c97a2dafb379.png)
2021-03-25 15:31:45 +08:00
### Execute a query and map it to a list of dynamic objects without using head [[Try it]](https://dotnetfiddle.net/fv58u3)
2021-03-16 20:39:49 +08:00
* dynamic key is `A.B.C.D..`
| MiniExcel | 1 |
| -------- | -------- |
| Github | 2 |
```C#
using (var stream = File.OpenRead(path))
{
var rows = stream.Query().ToList();
Assert.Equal("MiniExcel", rows[0].A);
Assert.Equal(1, rows[0].B);
Assert.Equal("Github", rows[1].A);
Assert.Equal(2, rows[1].B);
}
```
2021-03-25 15:31:45 +08:00
### Execute a query with first header row [[Try it]](https://dotnetfiddle.net/fv58u3)
| Column1 | Column2 |
| -------- | -------- |
| MiniExcel | 1 |
| Github | 2 |
```C#
using (var stream = File.OpenRead(path))
{
var rows = stream.Query(useHeaderRow:true).ToList();
Assert.Equal("MiniExcel", rows[0].Column1);
Assert.Equal(1, rows[0].Column2);
Assert.Equal("Github", rows[1].Column1);
Assert.Equal(2, rows[1].Column2);
}
```
2021-03-21 18:26:00 +08:00
### Query First
```C#
using (var stream = File.OpenRead(path))
Assert.Equal("HelloWorld", stream.Query().First().A);
2021-03-21 18:26:00 +08:00
```
performance: MiniExcel/ExcelDataReader/ClosedXML/EPPlus
![queryfirst](https://user-images.githubusercontent.com/12729184/111072392-6037a900-8515-11eb-9693-5ce2dad1e460.gif)
2021-03-25 15:31:45 +08:00
### Create Excel Xlsx file [[Try it]](https://dotnetfiddle.net/fv58u3)
2021-03-25 15:11:16 +08:00
Anonymous or strongly type:
2021-03-14 13:26:40 +08:00
```C#
var path = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}.xlsx");
2021-03-14 20:56:10 +08:00
MiniExcel.SaveAs(path, new[] {
new { Column1 = "MiniExcel", Column2 = 1 },
new { Column1 = "Github", Column2 = 2}
});
```
Datatable:
2021-03-14 13:26:40 +08:00
```C#
var path = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}.xlsx");
var table = new DataTable();
{
table.Columns.Add("Column1", typeof(string));
table.Columns.Add("Column2", typeof(decimal));
table.Rows.Add("MiniExcel", 1);
table.Rows.Add("Github", 2);
}
2021-03-14 20:56:10 +08:00
MiniExcel.SaveAs(path, table);
```
Dapper:
```C#
using (var connection = GetConnection(connectionString))
{
var rows = connection.Query(@"select 'MiniExcel' as Column1,1 as Column2 union all select 'Github',2");
MiniExcel.SaveAs(path, rows);
}
```
`IEnumerable<IDictionary<string, object>>`
```C#
var values = new List<Dictionary<string, object>>()
{
new Dictionary<string,object>{{ "Column1", "MiniExcel" }, { "Column2", 1 } },
new Dictionary<string,object>{{ "Column1", "Github" }, { "Column2", 2 } }
};
MiniExcel.SaveAs(path, values);
```
Create File Result :
| Column1 | Column2 |
| -------- | -------- |
| MiniExcel | 1 |
| Github | 2 |
2021-03-25 15:11:16 +08:00
### SaveAs Stream [[Try it]](https://dotnetfiddle.net/JOen0e)
2021-03-14 13:26:40 +08:00
```C#
2021-03-25 15:11:16 +08:00
using (var stream = File.Create(path))
2021-03-14 13:26:40 +08:00
{
stream.SaveAs(values);
}
```
2021-03-14 22:41:21 +08:00
2021-03-14 22:40:14 +08:00
### SQLite & Dapper `Large Size File` SQL Insert Avoid OOM (out of memory)
```C#
using (var connection = new SQLiteConnection(connectionString))
{
connection.Open();
using (var transaction = connection.BeginTransaction())
using (var stream = File.OpenRead(path))
{
var rows = stream.Query();
foreach (var row in rows)
connection.Execute("insert into T (A,B) values (@A,@B)", new { row.A, row.B }, transaction: transaction);
transaction.Commit();
}
}
```
2021-03-14 22:41:21 +08:00
performance:
![image](https://user-images.githubusercontent.com/12729184/111072579-2dda7b80-8516-11eb-9843-c01a1edc88ec.png)
2021-03-14 20:45:33 +08:00
### ASP.NET Core 3.1 or MVC 5 Download Excel Xlsx API Demo
```C#
2021-03-14 20:45:33 +08:00
public class ExcelController : Controller
{
2021-03-14 20:45:33 +08:00
public IActionResult Download()
{
var values = new[] {
new { Column1 = "MiniExcel", Column2 = 1 },
new { Column1 = "Github", Column2 = 2}
};
var stream = new MemoryStream();
stream.SaveAs(values);
return File(stream,
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"demo.xlsx");
}
}
```
2021-03-24 17:11:46 +08:00
### Excel Type Auto Check
Default system will auto check file path or stream is from xlsx or csv, but if you need to specify type, it can use excelType parameter.
```C#
stream.SaveAs(excelType:ExcelType.CSV);
//or
stream.SaveAs(excelType:ExcelType.XLSX);
2021-03-25 15:11:16 +08:00
//or
stream.Query(excelType:ExcelType.CSV);
//or
stream.Query(excelType:ExcelType.XLSX);
2021-03-24 17:11:46 +08:00
```
2021-03-03 15:39:37 +08:00
### TODO
2021-03-03 13:56:32 +08:00
Please Check [Project · todo](https://github.com/shps951023/MiniExcel/projects/1?fullscreen=true)
2021-03-03 15:39:37 +08:00
### Limitations and caveats
2021-03-03 15:39:37 +08:00
- Same column name use last right one
2021-03-16 15:24:49 +08:00
- Must be a non-abstract type with a public parameterless constructor
2021-03-14 23:11:54 +08:00
2021-03-24 17:11:46 +08:00
### Reference
2021-03-14 23:11:54 +08:00
2021-03-24 17:11:46 +08:00
- [ExcelDataReader](https://github.com/ExcelDataReader/ExcelDataReader)
- [StackExchange/Dapper](https://github.com/StackExchange/Dapper)