Exploring MiniExcel: A Lightweight .NET Library for Excel Operations

Exploring MiniExcel: A Lightweight .NET Library for Excel Operations

Overview

For data handling and reporting, Excel remains one of the most widely used tools. From simple data logging to complex financial modelling, Excel’s flexibility and functionality make it indispensable. For .NET developers, integrating Excel operations into applications often involves dealing with cumbersome libraries or interop services. Enter MiniExcel, a lightweight .NET library designed to make Excel operations simpler and more efficient. In this blog post, we’ll dive into what MiniExcel is, its key features, how to get started, and some practical examples to showcase its capabilities.

What is MiniExcel?

MiniExcel is a minimalistic, high-performance library for reading and writing Excel files in .NET applications. It is built to be easy to use, with a focus on efficiency and simplicity. Unlike some of the more bloated Excel libraries, MiniExcel avoids unnecessary dependencies and complex configurations, making it a great choice for developers who need to handle Excel files without overhead.

Key Features

  1. Lightweight and Fast: MiniExcel is designed to be minimal and performant, ensuring that your application doesn’t get bogged down by heavy libraries.
  2. Easy to Use: With a simple API, MiniExcel makes it straightforward to read and write Excel files.
  3. No External Dependencies: MiniExcel doesn’t rely on external COM objects or other dependencies, which simplifies deployment.
  4. Supports .NET Core and .NET Framework: MiniExcel is compatible with both .NET Core and .NET Framework, offering flexibility across different project types.
  5. Open Source: The library is open-source, with active community contributions and support.

Getting Started with MiniExcel

Installation

To get started with MiniExcel, you can install it via NuGet. Run the following command in your Package Manager Console:

Install-Package MiniExcel

Or, if you prefer using the .NET CLI:

dotnet add package MiniExcel

Basic Usage

Reading from an Excel File

Reading data from an Excel file is straightforward with MiniExcel. Here’s a simple example of how to read data from an Excel file:

using MiniExcelLibs;
using System.Collections.Generic;

// Define a class to represent the data structure
public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
}

// Read data from an Excel file
var products = MiniExcel.Query<Product>("products.xlsx").ToList();

foreach (var product in products)
{
    Console.WriteLine($"Id: {product.Id}, Name: {product.Name}, Price: {product.Price}");
}

In this example, the MiniExcel.Query method reads data from the specified Excel file and maps it to a list of Product objects.

Writing to an Excel File

Writing data to an Excel file is equally simple. Here’s how you can create an Excel file from a list of objects:

using MiniExcelLibs;
using System.Collections.Generic;

// Define a list of products
var products = new List<Product>
{
    new Product { Id = 1, Name = "Laptop", Price = 999.99m },
    new Product { Id = 2, Name = "Smartphone", Price = 499.99m }
};

// Write data to an Excel file
MiniExcel.SaveAs("products.xlsx", products);

In this example, the MiniExcel.SaveAs method writes the list of Product objects to the specified Excel file.

Advanced Features

Customising Cell Formatting

MiniExcel also allows for customising cell formatting to suit specific needs. While the library keeps things simple, you can still apply basic formatting:

using MiniExcelLibs;
using MiniExcelLibs.OpenXml;
using System.Collections.Generic;

var products = new List<Product>
{
    new Product { Id = 1, Name = "Laptop", Price = 999.99m },
    new Product { Id = 2, Name = "Smartphone", Price = 499.99m }
};

var configuration = new OpenXmlConfiguration
{
    Columns = new List<MiniExcelLibs.OpenXml.ExcelColumn>
    {
        new MiniExcelLibs.OpenXml.ExcelColumn { Index = 1, Width = 20 }, // Custom column width
        new MiniExcelLibs.OpenXml.ExcelColumn { Index = 2, Width = 30 }
    }
};

MiniExcel.SaveAs("products_custom.xlsx", products, configuration);

Handling Large Datasets

One of MiniExcel’s strengths is its ability to handle large datasets efficiently. By streaming data instead of loading it all into memory, MiniExcel ensures that even massive files can be processed without exhausting system resources.

using MiniExcelLibs;

// Stream data from a large Excel file
await foreach (var row in MiniExcel.QueryAsync(@"largefile.xlsx"))
{
    Console.WriteLine($"{row.A} {row.B} {row.C}");
}

Conclusion

MiniExcel is a powerful yet lightweight library for handling Excel operations in .NET applications. Its ease of use, high performance, and minimal dependencies make it an excellent choice for developers looking to integrate Excel functionalities without the usual overhead. Whether you’re building a small utility or a large-scale application, it can help you manage Excel files efficiently and effectively.

With its active community and ongoing development, it continues to evolve, offering new features and improvements. So, if you haven’t tried it yet, give it a spin and experience the simplicity and power of this remarkable library. Happy coding!

You can find MiniExcel here: – https://github.com/mini-software/MiniExcel

Stephen

Hi, my name is Stephen Finchett. I have been a software engineer for over 30 years and worked on complex, business critical, multi-user systems for all of my career. For the last 15 years, I have been concentrating on web based solutions using the Microsoft Stack including ASP.Net, C#, TypeScript, SQL Server and running everything at scale within Kubernetes.