Migrated repository
Go to file
2021-03-16 15:27:03 +08:00
docs appveyor 2021-03-16 15:24:37 +08:00
drafts ExcelDataReader Drafts 2021-03-16 15:27:03 +08:00
samples/xlsx ExcelDataReader Drafts 2021-03-16 15:27:03 +08:00
src List<ExtendedFormat> _styles 2021-03-16 15:02:04 +08:00
tests appveyor 2021-03-16 15:07:10 +08:00
.gitattributes .gitattributes 2021-03-03 13:26:37 +08:00
.gitignore ignore packages 2021-03-14 20:27:48 +08:00
appveyor.yml support appveyor 2021-03-16 14:58:14 +08:00
LICENSE.md Apache License 2.0 2021-03-10 08:30:47 +08:00
README.md appveyor 2021-03-16 15:24:49 +08:00

NuGet Build status

Features

  • Avoid large file OOM(out of memoery) by IEnumerable Lazy Step By Step getting one row cells not until all rows read in memory
    e.g: Comparison of MiniExcel Query and ExcelDataReader/EPPlus/ClosedXml of reading large Xlsx File miniexcel_lazy_load
  • Support .NET Standard 2.0/.NET 4.6/.NET 5
  • Mini without any third party library dependencies
  • Support dynamic/type mapping query and create by AnonymousType/DapperRows/List/Array/Set/Enumrable/DataTable/Dictionary
  • Dapper query style

Installation

You can install the package from NuGet

Release Notes

Please Check Release Notes

Execute a query and map the results to a strongly typed IEnumerable

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

Execute a query and map it to a list of dynamic objects without using head

MiniExcel 1
Github 2
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);
}

Execute a query with first header row

Column1 Column2
MiniExcel 1
Github 2
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);
}

Query Mapping Type

Create Excel Xlsx file by ICollection Anonymous Type/Datatable

var path = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}.xlsx");
MiniExcel.SaveAs(path, new[] {
    new { Column1 = "MiniExcel", Column2 = 1 },
    new { Column1 = "Github", Column2 = 2}
});

Datatable:

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);
}

MiniExcel.SaveAs(path, table);

Create File Result :

Column1 Column2
MiniExcel 1
Github 2

SaveAs Stream

using (var stream = new FileStream(path, FileMode.CreateNew))
{
    stream.SaveAs(values);
}

Query First

using (var stream = File.OpenRead(path))
    Assert.Equal("HelloWorld", stream.QueryFirst().A);

performance: MiniExcel/ExcelDataReader/ClosedXML/EPPlus
queryfirst

SQLite & Dapper Large Size File SQL Insert Avoid OOM (out of memory)

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();
    }
}

performance: image

ASP.NET Core 3.1 or MVC 5 Download Excel Xlsx API Demo

public class ExcelController : Controller
{
    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");
    }
}

TODO

Please Check Project · todo

Limitations and caveats

  • Custom datetime/timespan format can't mapping to DateTime/TimeSpan type
  • Same column name use last right one
  • Must be a non-abstract type with a public parameterless constructor

Reference