MiniExcel supports exporting various data structures to an Excel document using the SaveAs method. Supported input types include strongly typed objects, anonymous objects, IEnumerable<IDictionary<string, object>>, IDataReader, and DataTable.
// From strongly typed objects
var values = new[]
{
new { Name = "MiniExcel", Value = 1 },
new { Name = "Github", Value = 2 }
};
MiniExcel.SaveAs(yourPath, values);
// From anonymous objects
public class TestType
{
public string Name { get; set; }
public int Value { get; set; }
}
TestType[] values = [
new TestType { Name = "MiniExcel", Value = 1 },
new TestType { Name = "Github", Value = 2 }
];
MiniExcel.SaveAs(yourPath, values);
// From a IEnumerable<IDictionary<string, object>>
new List<Dictionary<string, object>>() dicts = [
new Dictionary<string, object> { { "Name", "MiniExcel" }, { "Value", 1 } },
new Dictionary<string, object> { { "Name", "Github" }, { "Value", 2 } }
];
MiniExcel.SaveAs(yourPath, dicts);
// Directly from a IDataReader
using var connection = yourConnectionProvider.GetConnection();
connection.Open();
using var cmd = connection.CreateCommand();
cmd.CommandText = """
SELECT 'MiniExcel' AS "Name", 1 AS "Value"
UNION ALL
SELECT 'Github', 2
""";
using var reader = cmd.ExecuteReader();
MiniExcel.SaveAs(yourPath, reader);
// From a DataTable
var table = new DataTable();
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Value", typeof(int));
table.Rows.Add("MiniExcel", 1);
table.Rows.Add("Github", 2);
MiniExcel.SaveAs(path, table);