The best way to work with Exel in .NET is MiniExcel?

Valentyn Osidach đź“š
.Net Programming
Published in
3 min readFeb 13, 2024

GitHub/MiniExcel

Introduction

MiniExcel is simple and efficient to avoid OOM’s .NET processing Excel tool.
At present, most popular frameworks need to load all the data into the memory to facilitate operation, but it will cause memory consumption problems. MiniExcel tries to use the algorithm from a stream to reduce the original 1000 MB occupation to a few MB to avoid OOM(out of memory).

Features

  • Low memory consumption, avoid OOM (out of memory) and full GC
  • Support real-time operation of each row of data
  • Support LINQ deferred execution, it can do low-consumption, fast paging, and other complex queries
  • Lightweight, without Microsoft Office installed, no COM+, DLL size is less than 150KB
  • Easy API style to read/write/fill Excel

How to use it?

We have two Sheets (“Office”, ”Remote”)
Some data


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

Get data from the first sheet in Excel

var rows = MiniExcel.Query<UserAccount>("data.xlsx");

// or

using (var stream = File.OpenRead("data.xlsx")) {
var rows = stream.Query<UserAccount>();
}

Get data from the first sheet by name

var rows = MiniExcel.Query<UserAccount>("data.xlsx", "Remote");

// or

using (var stream = File.OpenRead("data.xlsx")) {
var rows = stream.Query<UserAccount>("Remote");
}

Get all sheets

var sheetNames = MiniExcel.GetSheetNames(path);

foreach (var sheetName in sheetNames)
{
var rows = MiniExcel.Query(path, sheetName: sheetName);
}

Start reading data from the cell

MiniExcel.Query(path,useHeaderRow:true,startCell:"B3");

Result:

More features

  • Create Excel from values MiniExcel.SaveAs(path, reader);
  • Create Excel using DataReader by Dapper ExecuteReader
  • Create Excel using Dapper Query
  • SaveAs to MemoryStream
  • Create Multiple Sheets
  • Apply TableStyles Options
  • Create images in rows
  • Use auto filter
  • Merge cells
  • Skip null values
  • Fill Data To Excel Template
  • Excel Column Name/Index/Ignore Attribute
  • Support custom formats
  • Multiple column names mapping to the same property.
  • Set column width
  • Support csv reading
  • Map enums by name

Performance

Import/Query Excel

Export/Query Excel

Memory usage

Other Stories

--

--

Valentyn Osidach đź“š
.Net Programming

I am a software developer from Ukraine with more than 7 years of experience. I specialize in C# and .NET and love sharing my development knowledge. 👨🏻‍💻