Use MiniExcel.ExcelManager<T> to map rows directly to a POCO without manual column mapping.
public class Product
{
public int Id { get; set; }
public string Name { get; set; } = null!;
public decimal Price { get; set; }
}
public async Task<IEnumerable<Product>> ImportProducts(byte[] excelBytes)
{
IExcelService<Product> excel = new Regira.Office.Excel.MiniExcel.ExcelManager<Product>();
var file = excelBytes.ToBinaryFile();
var sheets = await excel.Read(file);
return sheets.FirstOrDefault()?.Data ?? [];
}
Build multiple ExcelSheet objects and pass them to Create().
public async Task<IMemoryFile> ExportReport(IEnumerable<Order> orders, IEnumerable<Product> products)
{
IExcelService excel = new Regira.Office.Excel.EPPlus.ExcelManager();
var orderSheet = new ExcelSheet
{
Name = "Orders",
Data = orders.Select(o => (object)new Dictionary<string, object?>
{
["Id"] = o.Id,
["Customer"] = o.CustomerName,
["Total"] = o.Total,
["Date"] = o.OrderDate
}).ToList()
};
var productSheet = new ExcelSheet
{
Name = "Products",
Data = products.Select(p => (object)new Dictionary<string, object?>
{
["Id"] = p.Id,
["Name"] = p.Name,
["Price"] = p.Price
}).ToList()
};
return await excel.Create([orderSheet, productSheet]);
}
Use TransformData to round decimals or format values during write.
var excel = new Regira.Office.Excel.EPPlus.ExcelManager(new()
{
DateFormat = "dd/MM/yyyy",
TransformData = (cellAddress, key, value) => key switch
{
"Price" => Math.Round(Convert.ToDecimal(value), 2),
"Discount" => $"{value}%",
_ => value
}
});
IMemoryFile file = await excel.Create([sheet]);
Supply a headers array to receive only the columns you need.
IExcelService excel = new Regira.Office.Excel.ClosedXML.ExcelManager();
var file = excelBytes.ToBinaryFile();
var sheets = await excel.Read(file, headers: ["Name", "Email", "Phone"]);
foreach (var row in sheets.First().Data!.Cast<IDictionary<string, object>>())
{
Console.WriteLine($"{row["Name"]} — {row["Email"]}");
}
Read an existing workbook, modify the data, and produce a new file.
public async Task<IMemoryFile> ApplyDiscount(byte[] sourceBytes, decimal discountPct)
{
IExcelService excel = new Regira.Office.Excel.MiniExcel.ExcelManager();
var sheets = (await excel.Read(sourceBytes.ToBinaryFile())).ToList();
foreach (var sheet in sheets)
{
foreach (var row in sheet.Data!.Cast<IDictionary<string, object?>>())
{
if (row.TryGetValue("Price", out var price) && price is decimal d)
row["Price"] = Math.Round(d * (1 - discountPct / 100), 2);
}
}
return await excel.Create(sheets);
}